Skip to content

fix(server-core): share hono context storage across duplicated module copies - #8776

Merged
keepview merged 1 commit into
mainfrom
fix/server-core-global-context-storage
Jul 23, 2026
Merged

keepview merged 1 commit into
mainfrom
fix/server-core-global-context-storage

Conversation

@keepview

Copy link
Copy Markdown
Contributor

Summary

pnpm peer-variant duplication can load two copies of @modern-js/server-core in one process. Since the hono request context lives in a module-scoped AsyncLocalStorage, the host writes the context through one copy while @modern-js/plugin-bff's decorators read through the other, failing with:

Can't call useContext out of server scope

This PR keys the request-context storage on globalThis via Symbol.for, so every loaded copy shares one AsyncLocalStorage:

  • createStorage gains an optional globalKey parameter — without it the behavior is byte-for-byte unchanged (other/generic uses unaffected; context.ts is the only call site today);
  • request isolation is untouched: ALS isolates by async call chain, not by instance count — single-copy processes already shared one module-level instance;
  • transition note: a process mixing a fixed copy with an older copy still splits for the old copy (no worse than today); downstreams keeping their own scope-bridge workarounds should drop them only after all resolved server-core copies include this fix.

Related Links

  • Root cause of the downstream scope-bridge workaround in the EdenX fusion branch (internal).

Checklist

  • I have added changeset via pnpm run change.
  • I have updated the documentation.
  • I have added tests to cover my changes.

@changeset-bot

changeset-bot Bot commented Jul 14, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1e1815e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 118 packages
Name Type
@modern-js/server-core Patch
@modern-js/plugin-bff Patch
@modern-js/plugin-data-loader Patch
@modern-js/plugin-i18n Patch
@modern-js/render Patch
@modern-js/plugin-polyfill Patch
@modern-js/prod-server Patch
@modern-js/server-runtime Patch
@modern-js/server Patch
@modern-js/server-utils Patch
@modern-js/app-tools Patch
server-config Patch
bff-api-app Patch
bff-client-app Patch
bff-indep-client-app Patch
bff-hono Patch
deploy-server Patch
pure-esm-project Patch
basic-app-rstest Patch
i18n-app-csr-html-lang Patch
i18n-app Patch
i18n-app-ssr-html-lang Patch
i18n-app-ssr Patch
i18n-custom-i18n-wrapper Patch
i18n-mf-app-provider Patch
i18n-mf-component-provider Patch
i18n-mf-consumer Patch
i18n-routes Patch
i18n-routes-ssr Patch
@modern-js/plugin-ssg Patch
server-monitors Patch
ssg-fixtures-web-server Patch
ssr-base-test Patch
ssr-base-fallback-test Patch
ssr-useid-test Patch
@modern-js/adapter-rstest Patch
@modern-js/plugin-styled-components Patch
@modern-js/image Patch
@modern-js/runtime Patch
@integration-test/alias-set Patch
app-document Patch
async-entry-test Patch
tmp Patch
integration-clean-dist-path Patch
integration-compatibility Patch
integration-custom-dist-path Patch
custom-file-system-entry Patch
integration-custom-template Patch
deploy Patch
dev-server Patch
integration-disable-html Patch
entries-app-builder Patch
app-custom-entries Patch
app-custom-routes-runtime Patch
app-custom Patch
app-entry Patch
app-route Patch
app-entry-server Patch
@integration-test/image-component Patch
main-entry-name Patch
nonce Patch
react-compiler-test Patch
routes-match Patch
routes Patch
app-rsbuild-hooks Patch
rsc-csr-app Patch
rsc-csr-routes Patch
rsc-ssr-app Patch
rsc-ssr-routes Patch
basic-app-rstest-browser Patch
runtime-custom-plugin Patch
runtime-custom-config-plugin Patch
select-mul-entry-test Patch
select-one-entry-test Patch
server-json-script Patch
server-prod Patch
server-routes Patch
@source-code-build/app Patch
ssg-fixtures-mega-list-routes Patch
ssg-fixtures-nested-routes Patch
ssg-fixtures-simple Patch
ssr-base-async-entry-test Patch
ssr-base-async-pre-entry-test Patch
ssr-base-json-test Patch
init Patch
ssr-base-loadable Patch
ssr-partial-test Patch
rsc-closing-tags-test Patch
ssr-script-loading Patch
ssr-streaming-inline-test Patch
ssr-streaming-lazy-test Patch
ssr-streaming-test Patch
styled-components-stream Patch
styled-components-string Patch
integration-tailwindcss-v2 Patch
integration-tailwindcss-v3 Patch
integration-tailwindcss-v4-tools Patch
integration-tailwindcss-v4 Patch
tmp-dir Patch
write-to-dist Patch
@modern-js/bundle-diff-benchmark Patch
@modern-js/main-doc Patch
@modern-js/tsconfig Patch
@modern-js/builder Patch
@modern-js/bff-core Patch
@modern-js/bff-runtime Patch
@modern-js/create-request Patch
@modern-js/create Patch
@modern-js/i18n-utils Patch
@modern-js/plugin Patch
@modern-js/runtime-utils Patch
@modern-js/sandpack-react Patch
@modern-js/types Patch
@modern-js/utils Patch
@modern-js/rslib Patch
@scripts/prebundle Patch
@scripts/rstest-config Patch
@scripts/release-node Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify

netlify Bot commented Jul 14, 2026

Copy link
Copy Markdown

Deploy Preview for modernjs-byted ready!

Name Link
🔨 Latest commit 1e1815e
🔍 Latest deploy log https://app.netlify.com/projects/modernjs-byted/deploys/6a60ac48936b310008d5bc1a
😎 Deploy Preview https://deploy-preview-8776--modernjs-byted.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 99 (no change from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 100 (no change from production)
PWA: -
View the detailed breakdown and full score reports
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

pnpm peer-variant duplication can load two copies of @modern-js/server-core
in one process; the host writes the request context through one copy's
module-scoped AsyncLocalStorage while plugin-bff reads through the other,
failing with "Can't call useContext out of server scope". Key the context
storage on globalThis via Symbol.for so every copy shares one store.
@keepview
keepview force-pushed the fix/server-core-global-context-storage branch from 8395e9e to 1e1815e Compare July 22, 2026 11:40
@keepview
keepview merged commit 3981b6b into main Jul 23, 2026
10 checks passed
@keepview
keepview deleted the fix/server-core-global-context-storage branch July 23, 2026 06:30
This was referenced Aug 5, 2026
keepview added a commit that referenced this pull request Sep 15, 2026
…SR bundle

The externals entry (#7236) kept `useHonoContext()` in loaders on the same
AsyncLocalStorage as the server. Since #8776 that storage is registered on
`globalThis[Symbol.for('@modern-js/server-core/hono-context')]`, so every
loaded copy of server-core/server-runtime shares it and the externals entry
no longer serves a purpose.

What it caused instead: applications depend on a re-export wrapper (for
example `@edenx/server-runtime`), so the SSR bundle carried a bare
`require("@modern-js/server-runtime")` on a transitive dependency. Under
pnpm's strict layout that name does not resolve from the application root,
deploy-time dependency tracing dropped it silently, and the bundle failed at
runtime with `Cannot find module '@modern-js/server-runtime'`, degrading the
whole page to client rendering unless something else in the artifact happened
to require the wrapper.

Remove the plugin and add a deploy-test assertion that no output script
requires the package any more.

Co-Authored-By: Riff
Claude-Session: https://claude.ai/code/session_0114E5DwvH9oSbRb7ezmyLJb
keepview added a commit that referenced this pull request Sep 15, 2026
…SR bundle

The externals entry (#7236) kept `useHonoContext()` in loaders on the same
AsyncLocalStorage as the server. Since #8776 that storage is registered on
`globalThis[Symbol.for('@modern-js/server-core/hono-context')]`, so every
loaded copy of server-core/server-runtime shares it and the externals entry
no longer serves a purpose.

What it caused instead: applications depend on a re-export wrapper (for
example `@edenx/server-runtime`), so the SSR bundle carried a bare
`require("@modern-js/server-runtime")` on a transitive dependency. Under
pnpm's strict layout that name does not resolve from the application root,
deploy-time dependency tracing dropped it silently, and the bundle failed at
runtime with `Cannot find module '@modern-js/server-runtime'`, degrading the
whole page to client rendering unless something else in the artifact happened
to require the wrapper.

Remove the plugin and add a deploy-test assertion that no output script
requires the package any more.

Co-Authored-By: Riff
Claude-Session: https://claude.ai/code/session_0114E5DwvH9oSbRb7ezmyLJb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Sponsor
SponsoredKunjungi sekarang
Promo