fix(vite): invalidate shared modules in SSR environment on HMR#4044
fix(vite): invalidate shared modules in SSR environment on HMR#4044Nazaire wants to merge 1 commit intonitrojs:mainfrom
Conversation
Modules present in both client and server environments were skipped during server-side invalidation due to an overly strict guard. This caused SSR to render stale HTML after HMR updates, leading to React hydration mismatches.
|
@Nazaire is attempting to deploy a commit to the Nitro Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughThis change modifies Nitro's Vite plugin hotUpdate hook to invalidate server-side module caches for all modules with valid IDs, rather than only server-only modules. Previously, shared modules (e.g., React components used in SSR) were not invalidated during development, causing hydration mismatches. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/build/vite/plugin.ts (1)
251-253:clientEnvsis now dead code — remove it.After removing the
clientEnvs.some(...)exclusion guard, theclientEnvsvariable is computed but never read. It should be deleted.♻️ Proposed fix
- const clientEnvs = Object.values(server.environments).filter( - (env) => env.config.consumer === "client" - ); let hasServerModule = false;🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/build/vite/plugin.ts` around lines 251 - 253, Remove the now-unused clientEnvs variable declaration in src/build/vite/plugin.ts: delete the const clientEnvs = Object.values(server.environments).filter((env) => env.config.consumer === "client"); line (it is dead after the clientEnvs.some(...) guard was removed) so no unused variable remains in the surrounding function/block.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/build/vite/plugin.ts`:
- Around line 251-253: Remove the now-unused clientEnvs variable declaration in
src/build/vite/plugin.ts: delete the const clientEnvs =
Object.values(server.environments).filter((env) => env.config.consumer ===
"client"); line (it is dead after the clientEnvs.some(...) guard was removed) so
no unused variable remains in the surrounding function/block.
Modules present in both client and server environments were skipped during server-side invalidation due to an overly strict guard. This caused SSR to render stale HTML after HMR updates, leading to React hydration mismatches.
🔗 Linked issue
Fixes #4043
Related to #4020
❓ Type of change
📚 Description
In
src/build/vite/plugin.ts, thehotUpdatehook skipped server-sideinvalidation for any module that also exists in a client environment:
The !clientEnvs.some(...) guard means: if a module is present in a client
environment, don't invalidate it in the server environment. The intent was
presumably to defer to Vite's normal client HMR — but the server-side copy of
that module still needs invalidation for SSR to re-render correctly.
The fix removes the exclusion guard so that all modules with an id are
invalidated in the server module graph on HMR, regardless of whether they also
appear in a client environment.
📝 Checklist