Skip to content

Comments

fix(vite): invalidate shared modules in SSR environment on HMR#4044

Open
Nazaire wants to merge 1 commit intonitrojs:mainfrom
Nazaire:fix/ssr-hmr-shared-module-invalidation
Open

fix(vite): invalidate shared modules in SSR environment on HMR#4044
Nazaire wants to merge 1 commit intonitrojs:mainfrom
Nazaire:fix/ssr-hmr-shared-module-invalidation

Conversation

@Nazaire
Copy link

@Nazaire Nazaire commented Feb 19, 2026

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

  • 🐞 Bug fix (a non-breaking change that fixes an issue)

📚 Description

In src/build/vite/plugin.ts, the hotUpdate hook skipped server-side
invalidation for any module that also exists in a client environment:

if (mod.id && !clientEnvs.some((env) => env.moduleGraph.getModuleById(mod.id!))) {
  hasServerOnlyModule = true;
  env.moduleGraph.invalidateModule(mod, invalidated, timestamp, false);
}

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

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

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 Nazaire requested a review from pi0 as a code owner February 19, 2026 04:45
@vercel
Copy link

vercel bot commented Feb 19, 2026

@Nazaire is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link

coderabbitai bot commented Feb 19, 2026

📝 Walkthrough

Walkthrough

This 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

Cohort / File(s) Summary
HMR Module Invalidation
src/build/vite/plugin.ts
Updated hotUpdate detection logic to invalidate all modules with valid IDs and trigger full reload accordingly, fixing incomplete invalidation of shared server-side modules during development.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title follows conventional commits format with 'fix' prefix and clearly describes the bug fix for invalidating shared modules in SSR on HMR.
Description check ✅ Passed The description is directly related to the changeset, clearly explaining the bug, the problematic code, and the fix applied to the module invalidation logic.
Linked Issues check ✅ Passed The PR successfully addresses issue #4043 by ensuring server-side module invalidation for shared modules during HMR, preventing stale SSR renders and hydration mismatches.
Out of Scope Changes check ✅ Passed All changes in src/build/vite/plugin.ts are directly scoped to fixing the server-side module invalidation issue; no extraneous changes are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/build/vite/plugin.ts (1)

251-253: clientEnvs is now dead code — remove it.

After removing the clientEnvs.some(...) exclusion guard, the clientEnvs variable 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.

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.

bug(vite): shared modules not invalidated in SSR environment on HMR

1 participant