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

Global context & data is lost between custom server and Next.js router/renderer #54782

Closed
1 task done
coltonehrman opened this issue Aug 30, 2023 · 4 comments
Closed
1 task done
Labels
bug Issue was opened via the bug report template. locked Output (export/standalone) Related to the the output option in `next.config.js`. Pages Router Related to Pages Router. Runtime Related to Node.js or Edge Runtime with Next.js.

Comments

@coltonehrman
Copy link
Contributor

coltonehrman commented Aug 30, 2023

Verify canary release

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

Provide environment information

    Operating System:
      Platform: linux
      Arch: x64
      Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
    Binaries:
      Node: 16.17.0
      npm: 8.15.0
      Yarn: 1.22.19
      pnpm: 7.1.0
    Relevant Packages:
      next: 13.4.20-canary.12
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.3
    Next.js Config:
      output: N/A

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

App Router, Data fetching (gS(S)P, getInitialProps), Middleware / Edge (API routes, runtime), Standalone mode (output: "standalone")

Link to the code that reproduces this issue or a replay of the bug

https://codesandbox.io/p/sandbox/relaxed-phoebe-jncmvg

To Reproduce

  1. Use next version 13.4.13 or higher
  2. Setup a custom server.
  3. Use nconf (a singleton module)
  4. Modify nconf store in custom server.
  5. Try to access the modified nconf object in Next.js context. (ie: pages/, app/, gSSP, API route, etc.)

Describe the Bug

All global context and data is lost between the custom server context and the Next.js context.

As you can see below, the global (singleton) module was cached and worked across the custom server and Next.js gSSP in 13.4.12, but doesn't work in versions 13.4.13 or higher.

Custom server code

Screenshot 2023-08-31 at 9 57 18 AM

Next.js 13.4.12

Screenshot 2023-08-31 at 9 53 55 AM

Next.js 13.4.19

Screenshot 2023-08-31 at 9 54 32 AM

Link to related issue:

Expected Behavior

Global data stays intact between both contexts.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@coltonehrman coltonehrman added the bug Issue was opened via the bug report template. label Aug 30, 2023
@github-actions github-actions bot added Pages Router Related to Pages Router. Runtime Related to Node.js or Edge Runtime with Next.js. Output (export/standalone) Related to the the output option in `next.config.js`. labels Aug 30, 2023
@danieljee
Copy link

I don't know if this is a similar issue but a singleton module loses context in every request in route handlers. It gets reinstantiated every time.

@shaunwarman
Copy link

We think the fix is to leverage
https://nextjs.org/docs/app/api-reference/next-config-js/serverComponentsExternalPackages

LimEunSeop added a commit to LimEunSeop/silverflame that referenced this issue Nov 6, 2023
- vercel/next.js#54782 에 따르면, 싱글턴 모듈은 요청 시 유실될 수 있으므로, 매번 생성될 수 있도록 하는것이 좋다고는 하다.
- 또 다른 관련문서: vercel/next.js#55660 vercel/next.js#56018
@coltonehrman
Copy link
Contributor Author

Resolved with #54782 (comment)

Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. 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 Nov 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked Output (export/standalone) Related to the the output option in `next.config.js`. Pages Router Related to Pages Router. Runtime Related to Node.js or Edge Runtime with Next.js.
Projects
None yet
Development

No branches or pull requests

3 participants