Skip to content

WIP fix(ssr): invalidate nested ESM chunks in dev mode#8467

Open
GiveMe-A-Name wants to merge 6 commits intomainfrom
fix/ssr-esm-cache-invalidation-8373
Open

WIP fix(ssr): invalidate nested ESM chunks in dev mode#8467
GiveMe-A-Name wants to merge 6 commits intomainfrom
fix/ssr-esm-cache-invalidation-8373

Conversation

@GiveMe-A-Name
Copy link
Copy Markdown
Member

@GiveMe-A-Name GiveMe-A-Name commented Mar 21, 2026

Summary

  • fix SSR dev ESM cache invalidation for rspack chunk imports by patching dynamic chunk URLs with a timestamp
  • load the patched SSR bundle through a temporary ESM file so nested chunk imports also bypass Node's module cache
  • add focused utils coverage for dev ESM chunk reloading and unique temp bundle path generation

Problem

In dev SSR mode for ESM projects, top-level bundle invalidation was not enough. After editing a page or component, the SSR entry bundle could be refreshed, but nested rspack chunk imports still came from the ESM cache, which caused stale SSR output and hydration mismatch on refresh.

Solution

compatibleRequire now detects the rspack dynamic chunk import pattern in development ESM mode, rewrites those chunk imports to append a timestamp query, writes a temporary patched bundle file, imports it, and then removes the temp file.

This keeps the existing production path unchanged while ensuring nested SSR chunk imports are invalidated in development.

Tests

  • pnpm test:ut -- packages/toolkit/utils/tests/compatRequire.test.ts
  • pnpm test:framework -- integration/ssr/tests/esm-cache-invalidation.test.ts
  • pnpm lint packages/toolkit/utils/src/cli/require.ts packages/toolkit/utils/tests/compatRequire.test.ts

#8373

- Add requireFromString function to bypass ESM cache in dev mode
- Use mtime-based caching to avoid frequent recompilation
- Add fallback mechanism when requireFromString fails

Note: This fix addresses the main bundle loading, but there is
a remaining issue with rspack's internal chunk loading
(__webpack_require__.e) that requires rspack to fix internally.

Related: web-infra-dev/rspack#13304
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 21, 2026

🦋 Changeset detected

Latest commit: f9c1720

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

This PR includes changesets to release 113 packages
Name Type
@modern-js/utils Patch
@modern-js/builder Patch
@modern-js/plugin-bff Patch
@modern-js/plugin-data-loader Patch
@modern-js/plugin-ssg Patch
@modern-js/plugin-i18n Patch
@modern-js/runtime Patch
@modern-js/render Patch
@modern-js/bff-core Patch
@modern-js/server-core Patch
@modern-js/create-request Patch
@modern-js/plugin-polyfill Patch
@modern-js/prod-server Patch
@modern-js/server Patch
@modern-js/server-utils Patch
@modern-js/app-tools Patch
@modern-js/i18n-utils Patch
@modern-js/plugin Patch
@modern-js/runtime-utils Patch
@scripts/release-node Patch
server-config Patch
rsc-ssr-app 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-ssr Patch
i18n-mf-app-provider Patch
i18n-mf-component-provider Patch
i18n-mf-consumer Patch
i18n-routes-ssr Patch
ssg-fixtures-mega-list-routes Patch
ssg-fixtures-nested-routes Patch
ssg-fixtures-simple Patch
ssg-fixtures-web-server Patch
i18n-app-csr-html-lang Patch
i18n-app Patch
i18n-app-ssr-html-lang Patch
i18n-custom-i18n-wrapper Patch
i18n-routes Patch
@modern-js/plugin-styled-components 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
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
routes-match Patch
routes Patch
app-rsbuild-hooks Patch
rsc-csr-app Patch
rsc-csr-routes 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-monitors Patch
server-prod Patch
server-routes Patch
@source-code-build/app Patch
ssr-base-async-entry-test Patch
ssr-base-json-test Patch
ssr-base-test Patch
ssr-esm-cache-invalidation-test Patch
ssr-base-fallback-test Patch
init Patch
ssr-base-loadable Patch
ssr-partial-test Patch
ssr-script-loading Patch
ssr-streaming-inline-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 Patch
tmp-dir Patch
write-to-dist Patch
@modern-js/bundle-diff-benchmark Patch
@modern-js/server-runtime Patch
@modern-js/adapter-rstest Patch
@modern-js/image Patch
entries-app-builder Patch
@modern-js/create Patch
@modern-js/main-doc Patch
@modern-js/tsconfig Patch
@modern-js/bff-runtime Patch
@modern-js/sandpack-react Patch
@modern-js/types Patch
@modern-js/rslib Patch
@scripts/prebundle Patch
@scripts/rstest-config 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
Copy link
Copy Markdown

netlify bot commented Mar 21, 2026

Deploy Preview for modernjs-byted ready!

Name Link
🔨 Latest commit f9c1720
🔍 Latest deploy log https://app.netlify.com/projects/modernjs-byted/deploys/69c0a39f18c9d8000944d0a8
😎 Deploy Preview https://deploy-preview-8467--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: 98 (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

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

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 21, 2026

Rsdoctor Bundle Diff Analysis

📊 Quick Summary
Project Total Size Change
bundle-diff 2.9 MB 0

Generated by Rsdoctor GitHub Action

@zllkjc zllkjc closed this Mar 30, 2026
@GiveMe-A-Name GiveMe-A-Name reopened this Mar 30, 2026
@GiveMe-A-Name GiveMe-A-Name changed the title fix(ssr): invalidate nested ESM chunks in dev mode WIP fix(ssr): invalidate nested ESM chunks in dev mode Mar 30, 2026
@web-infra-dev web-infra-dev deleted a comment from zllkjc Mar 30, 2026
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