Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.5k
refactor(nuxt): unitywindow.__NUXT__ structure#33867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
refactor(nuxt): unitywindow.__NUXT__ structure#33867
Conversation
|
@nuxt/kit@nuxt/nitro-servernuxt@nuxt/rspack-builder@nuxt/schema@nuxt/vite-builder@nuxt/webpack-buildercommit: |
WalkthroughThe changes refactor payload handling and multi-app support across the Nuxt framework. The renderer utilities are updated to consistently wrap payloads under an appId key structure. Client-side payload retrieval is simplified by removing multiApp conditional logic and using a unified approach to access inline and external payloads. The entry point and app initialisation code are simplified by removing multiApp branching for SSR detection. The Window.NUXT type is narrowed to enforce a nested record structure, and config resolution is updated to use a fixed runtime path instead of conditional multiApp logic. Possibly related PRs
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Tip ✨ Issue Enrichment is now available for GitHub issues!CodeRabbit can now help you manage issues more effectively:
Disable automatic issue enrichmentTo disable automatic issue enrichment, add the following to your issue_enrichment:auto_enrich:enabled:false Thanks for usingCodeRabbit! 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
packages/nitro-server/src/runtime/utils/renderer/payload.ts(2 hunks)packages/nuxt/src/app/composables/payload.ts(2 hunks)packages/nuxt/src/app/entry.ts(2 hunks)packages/nuxt/src/app/nuxt.ts(2 hunks)packages/nuxt/src/app/types/augments.ts(1 hunks)packages/nuxt/src/core/templates.ts(1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx,vue}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Follow standard TypeScript conventions and best practices
Files:
packages/nuxt/src/app/nuxt.tspackages/nuxt/src/app/types/augments.tspackages/nitro-server/src/runtime/utils/renderer/payload.tspackages/nuxt/src/app/entry.tspackages/nuxt/src/core/templates.tspackages/nuxt/src/app/composables/payload.ts
**/*.{ts,tsx,js,jsx,vue}
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.{ts,tsx,js,jsx,vue}: Use clear, descriptive variable and function names
Add comments only to explain complex logic or non-obvious implementations
Keep functions focused and manageable (generally under 50 lines), and extract complex logic into separate domain-specific files
Remove code that is not used or needed
Use error handling patterns consistently
Files:
packages/nuxt/src/app/nuxt.tspackages/nuxt/src/app/types/augments.tspackages/nitro-server/src/runtime/utils/renderer/payload.tspackages/nuxt/src/app/entry.tspackages/nuxt/src/core/templates.tspackages/nuxt/src/app/composables/payload.ts
🧠 Learnings (5)
📓 Common learnings
Learnt from: GalacticHypernovaRepo: nuxt/nuxt PR: 26468File: packages/nuxt/src/components/plugins/loader.ts:24-24Timestamp: 2024-11-05T15:22:54.759ZLearning: In `packages/nuxt/src/components/plugins/loader.ts`, the references to `resolve` and `distDir` are legacy code from before Nuxt used the new unplugin VFS and will be removed.📚 Learning: 2024-11-05T15:22:54.759Z
Learnt from: GalacticHypernovaRepo: nuxt/nuxt PR: 26468File: packages/nuxt/src/components/plugins/loader.ts:24-24Timestamp: 2024-11-05T15:22:54.759ZLearning: In `packages/nuxt/src/components/plugins/loader.ts`, the references to `resolve` and `distDir` are legacy code from before Nuxt used the new unplugin VFS and will be removed.Applied to files:
packages/nuxt/src/app/nuxt.tspackages/nuxt/src/app/entry.tspackages/nuxt/src/core/templates.ts
📚 Learning: 2024-12-12T12:36:34.871Z
Learnt from: huang-julienRepo: nuxt/nuxt PR: 29366File: packages/nuxt/src/app/components/nuxt-root.vue:16-19Timestamp: 2024-12-12T12:36:34.871ZLearning: In `packages/nuxt/src/app/components/nuxt-root.vue`, when optimizing bundle size by conditionally importing components based on route metadata, prefer using inline conditional imports like:```jsconst IsolatedPage = route?.meta?.isolate ? defineAsyncComponent(() => import('#build/isolated-page.mjs')) : null```instead of wrapping the import in a computed property or importing the component unconditionally.Applied to files:
packages/nuxt/src/app/nuxt.tspackages/nuxt/src/app/entry.tspackages/nuxt/src/core/templates.tspackages/nuxt/src/app/composables/payload.ts
📚 Learning: 2025-11-25T11:42:16.132Z
Learnt from: CRRepo: nuxt/nuxt PR: 0File: .github/copilot-instructions.md:0-0Timestamp: 2025-11-25T11:42:16.132ZLearning: Applies to **/*.vue : Use `<script setup lang="ts">` and the composition API when creating Vue componentsApplied to files:
packages/nuxt/src/app/entry.ts
📚 Learning: 2025-11-25T11:42:16.132Z
Learnt from: CRRepo: nuxt/nuxt PR: 0File: .github/copilot-instructions.md:0-0Timestamp: 2025-11-25T11:42:16.132ZLearning: Applies to **/*.{ts,tsx,vue} : Follow standard TypeScript conventions and best practicesApplied to files:
packages/nuxt/src/app/entry.ts
🧬 Code graph analysis (2)
packages/nitro-server/src/runtime/utils/renderer/payload.ts (1)
packages/nuxt/src/app/nuxt.ts (1)
NuxtSSRContext(61-86)
packages/nuxt/src/app/composables/payload.ts (4)
test/utils.ts (1)
parsePayload(92-94)packages/nuxt/src/app/nuxt.ts (1)
NuxtPayload(88-99)packages/nuxt/src/app/index.ts (1)
NuxtPayload(4-4)packages/nitro-server/src/runtime/utils/cache.ts (1)
payloadCache(7-7)
🔇 Additional comments (8)
packages/nuxt/src/app/nuxt.ts (2)
26-26:LGTM!The import simplification correctly removes the
multiAppflag that's no longer needed with the unified payload structure.
337-354:LGTM!The client-side payload access is now consistently using
nuxtApp._idto key intowindow.__NUXT__, aligning with the unified structure. This simplifies the logic by removing the previous multiApp conditional branching.packages/nuxt/src/app/types/augments.ts (1)
29-32:Type narrowing may affect external consumers.This type change removes the looser
Record<string, any>branch from the union. While this correctly reflects the new unified structure, any third-party code or plugins that accessedwindow.__NUXT__properties directly (without the appId key) may now encounter type errors.Consider whether this warrants a mention in migration notes or release changelog, as external consumers may need to update their code to access
window.__NUXT__[appId]instead ofwindow.__NUXT__directly.packages/nuxt/src/app/entry.ts (1)
60-65:LGTM!The SSR detection logic is well-structured:
- The fallback chain (
data-nuxt-dataattribute →__NUXT_DATA__id) provides backwards compatibility.- The
isSSRcheck correctly prioritises the payload'sserverRenderedflag before falling back to the DOM element'sdata-ssrattribute.packages/nuxt/src/app/composables/payload.ts (1)
121-137:LGTM with a note on merge precedence.The refactored payload composition correctly:
- Uses the same element selection pattern as
entry.tsfor consistency.- Separates inline and external payload sources with appId-scoped extraction.
- Guards against missing data with the early return.
The merge order (
inlineAppData→externalData→window.__NUXT__[appId]) means runtime-set values onwindow.__NUXT__will override both inline and external payloads, which appears intentional for supporting dynamic updates.packages/nitro-server/src/runtime/utils/renderer/payload.ts (3)
13-26:LGTM!The payload response correctly wraps the payload under the
appIdkey, and both JSON and module export formats consistently use this wrapped structure.
28-50:LGTM!The JSON script rendering properly:
- Wraps payload data under
appId.- Maintains backwards compatibility by only setting
id="__NUXT_DATA__"when not in multiApp mode.- Uses
Object.assignto merge config into the existing appId object, preserving any previously set data.
52-69:LGTM!The script rendering correctly:
- Combines data and config into
appPayloadbefore serialisation.- Uses spread syntax in extraction mode to safely merge potentially undefined imported payloads.
- Consistently assigns to
window.__NUXT__[appId]in both extraction and non-extraction paths.
| return[ | ||
| 'export const useRuntimeConfig = () => ', | ||
| (!nuxt.options.future.multiApp | ||
| ?'window?.__NUXT__?.config || window?.useNuxtApp?.().payload?.config' | ||
| :`window?.__NUXT__?.[${appId}]?.config || window?.useNuxtApp?.(${appId}).payload?.config`) | ||
| `window?.__NUXT__?.[${appId}]?.config || window?.useNuxtApp?.(${appId}).payload?.config` | ||
| ||{}, | ||
| ].join('\n') | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Syntax error in template string construction.
The string concatenation appears malformed. Line 376 doesn't properly close the string before the|| {} fallback on line 377, which would result in|| {} being evaluated at template generation time rather than at runtime.
Apply this diff to fix the string construction:
export const clientConfigTemplate: NuxtTemplate = { filename: 'nitro.client.mjs', getContents: ({ nuxt }) => { const appId = JSON.stringify(nuxt.options.appId) return [ 'export const useRuntimeConfig = () => ',- `window?.__NUXT__?.[${appId}]?.config || window?.useNuxtApp?.(${appId}).payload?.config`- || {},+ `window?.__NUXT__?.[${appId}]?.config || window?.useNuxtApp?.(${appId}).payload?.config || {}`, ].join('\n') }, }📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| return[ | |
| 'export const useRuntimeConfig = () => ', | |
| (!nuxt.options.future.multiApp | |
| ?'window?.__NUXT__?.config || window?.useNuxtApp?.().payload?.config' | |
| :`window?.__NUXT__?.[${appId}]?.config || window?.useNuxtApp?.(${appId}).payload?.config`) | |
| `window?.__NUXT__?.[${appId}]?.config || window?.useNuxtApp?.(${appId}).payload?.config` | |
| ||{}, | |
| ].join('\n') | |
| }, | |
| return[ | |
| 'export const useRuntimeConfig = () => ', | |
| `window?.__NUXT__?.[${appId}]?.config || window?.useNuxtApp?.(${appId}).payload?.config || {}`, | |
| ].join('\n') | |
| }, |
🤖 Prompt for AI Agents
In packages/nuxt/src/core/templates.ts around lines 374 to 379, the templatestring building the exported useRuntimeConfig function is malformed so the "||{}" fallback is evaluated at generation time instead of inside the generatedcode; fix it by ensuring the entire runtime expression (including the || {}) ispart of the returned string — close the template literal around the window/useNuxtApp expression and include the || {} inside that string (or build thefinal string via concatenation) so the fallback executes at runtime.CodSpeed Performance ReportMerging#33867 willimprove performances by 11.4%Comparing Summary
Benchmarks breakdown
|
🔗 Linked issue
#32486
📚 Description