Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

standalone + appDir breaks _app with getInitialProps #48918

Closed
1 task done
klarstrup opened this issue Apr 27, 2023 · 3 comments · Fixed by #49116
Closed
1 task done

standalone + appDir breaks _app with getInitialProps #48918

klarstrup opened this issue Apr 27, 2023 · 3 comments · Fixed by #49116
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template. Output (export/standalone) Related to the the output option in `next.config.js`. Pages Router Related to Pages Router.

Comments

@klarstrup
Copy link

klarstrup commented Apr 27, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

    Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.3.0: Thu Jan  5 20:48:54 PST 2023; root:xnu-8792.81.2~2/RELEASE_ARM64_T6000
    Binaries:
      Node: 18.15.0
      npm: 9.5.0
      Yarn: 1.22.19
      pnpm: 8.3.1
    Relevant packages:
      next: 13.3.5-canary.5
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

App directory (appDir: true), Data fetching (gS(S)P, getInitialProps), Standalone mode (output: "standalone")

Link to the code that reproduces this issue

https://github.com/klarstrup/next.js-repro-repo/tree/standalone-appdir-_app-getInitialProps

To Reproduce

  • Enable appDir
  • Set output mode to standalone
  • Create pages/_app.js
  • Add minimal _app such as
import App from "next/app";

function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />;
}

MyApp.getInitialProps = (appContext) => App.getInitialProps(appContext);

export default MyApp;
  • Install deps, build and run standalone server:
npm i
npm run build
node .next/standalone/server.js
  • Request any URL from said server(http://localhost:3000/whatevs)

Describe the Bug

The exports of react seem to be unavailable in the environment that _app runs in with getInitialProps present, causing errors for any access to React methods. Above repro gives us a stacktrace like so:

TypeError: Cannot read properties of null (reading 'useContext')
    at exports.useContext (~/code/my-app/.next/standalone/node_modules/next/dist/compiled/react/cjs/react.production.min.js:28:118)
    at Head (~/code/my-app/.next/standalone/.next/server/pages/_error.js:282:44)
    at renderWithHooks (~/code/my-app/.next/standalone/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5658:16)
    at renderIndeterminateComponent (~/code/my-app/.next/standalone/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5731:15)
    at renderElement (~/code/my-app/.next/standalone/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5946:7)
    at renderNodeDestructiveImpl (~/code/my-app/.next/standalone/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive (~/code/my-app/.next/standalone/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderNode (~/code/my-app/.next/standalone/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6259:12)
    at renderChildrenArray (~/code/my-app/.next/standalone/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6211:7)
    at renderNodeDestructiveImpl (~/code/my-app/.next/standalone/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6141:7)

Expected Behavior

I would expert a pages/_app file with getInitialProps to behave as documented, like it does without appDir: true.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@klarstrup klarstrup added the bug Issue was opened via the bug report template. label Apr 27, 2023
@github-actions github-actions bot added area: app App directory (appDir: true) Pages Router Related to Pages Router. Output (export/standalone) Related to the the output option in `next.config.js`. labels Apr 27, 2023
@khuezy
Copy link
Contributor

khuezy commented Apr 27, 2023

There's definitely something strange w/ 13.3.2-canary. My standalone app throws this error now:

errorMessage | Error: Cannot find module 'react/jsx-runtime'
Require stack:
- /var/task/node_modules/next/dist/server/require-hook.js
- /var/task/node_modules/next/dist/server/next-server.js

I'm also getting the same Cannot read properties of undefined (reading 'current') for canary.9+

NOTE: I'm not using getInitialProps at all. EDIT: oh wait... that's the build _app.js file doing it.

@klarstrup
Copy link
Author

Still broken as of 13.3.5-canary.5

shuding added a commit that referenced this issue May 4, 2023
Fixes #49055, fixes #48918. 

App dir will always require the server to run in the workers mode, so it
can keep a separate Node.js process for pages. This PR updates the
standalone server to initialize a "standalone server" (which works
similar to `start-server`), and changes the tracked files to include
Jest worker.
@github-actions
Copy link
Contributor

github-actions bot commented Jun 3, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template. Output (export/standalone) Related to the the output option in `next.config.js`. Pages Router Related to Pages Router.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants