fix(compactRoutes): key NuxtPage by interpolated pattern, not full path#4014
fix(compactRoutes): key NuxtPage by interpolated pattern, not full path#4014Togetic wants to merge 1 commit into
Conversation
With `experimental.compactRoutes`, the route-locale-detect plugin set `meta.key` to the full resolved path, so the `<NuxtPage>` key changed on every child navigation and Vue remounted the whole nested subtree — parent state was lost and `onBeforeMount`/`onBeforeUnmount` re-fired — on non-default locales only (the default-locale tree isn't compact, so Nuxt's stable interpolated key was used there). Key by the record's own interpolated pattern instead, matching Nuxt's default `<NuxtPage>` keying. Nested parents now stay mounted across child navigation and still remount when the locale param actually changes (e.g. `/de/x` -> `/fr/x`), preserving the original intent from nuxt-modules#3957. Fixes nuxt-modules#4001
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughIn Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ 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 |
🔗 Linked issue
Fixes #4001
📚 Description
With
experimental.compactRoutes, theroute-locale-detectplugin keys compact routes by the full resolved path:Nuxt uses
meta.keyas the<NuxtPage>component key, so the key changes on every child navigation (e.g./de/foo/bar→/de/foo/baz). Vue then treats the parent record as a different instance and remounts the whole nested subtree — state held in a parent component across child navigation is wiped, andonBeforeMount/onBeforeUnmountre-run unexpectedly.This only reproduces on non-default locales: the default-locale tree isn't compact (no
meta.key), so Nuxt falls back to the record's own interpolated pattern, which is stable across child navigation.The upstream intent — remount when the locale param actually changes, e.g.
/de/x→/fr/x(introduced in #3957) — is legitimate, but keying by the full path over-fires on every child navigation.Fix: key by the record's own interpolated pattern instead, matching Nuxt's default
<NuxtPage>keying:For a compact record
/:locale(en|de|fr)/foo, the key resolves to/de/foofor any child offoounder/de— stable across/de/foo/bar↔/de/foo/baz, while still changing to/fr/fooon a locale switch, so the intended locale-change remount is preserved.✅ Verification
pnpm test:types,pnpm lint, andpnpm test:unitall pass./de/x→/fr/x) still remounts as intended.📝 Note on tests
compactRoutesis currently covered only at the route-table level (test/pages/route_localization.test.ts); there's no e2e fixture exercising compact-route runtime navigation, andmeta.keyis assigned inside the runtime plugin. I'm happy to add a dedicated fixture + spec (nested pages asserting the parent stays mounted across child navigation and remounts on a locale switch) if you'd like that folded into this PR.Summary by CodeRabbit