-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
fix(nuxt): do not expose app components until fully resolved #32993
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
|
WalkthroughThe app resolution in packages/nuxt/src/core/app.ts now collects layouts, middleware, plugins and configs into local, explicitly typed accumulators rather than mutating app.* during traversal. Normalisation, de-duplication and the “add back unspecified plugins” logic operate on these local arrays/objects. After resolution the final middleware, plugins, configs and layouts are merged into the public NuxtApp via a single Object.assign(app, { middleware, plugins, configs, layouts }) before extension. A new test (packages/nuxt/test/app.test.ts) verifies resolveApp populates non-empty middleware, plugins and layouts when app writes are proxied. No public API changes. Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. 📜 Recent review detailsConfiguration used: CodeRabbit UI 💡 Knowledge Base configuration:
You can enable these sources in your CodeRabbit configuration. 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
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
🧹 Nitpick comments (4)
packages/nuxt/src/core/app.ts (3)
217-220: Duplicate normalisation runs; consider consolidating to a single passYou normalise/dedupe middleware and plugins once before assignment, and again after the hook. That’s extra work every resolve and can be surprising for future maintainers.
Options:
- Keep the post-hook pass (based on app.* as suggested) and drop this pre-hook pass, or
- Keep this pre-hook pass to provide fully-resolved values to 'app:resolve' and drop the post-hook pass.
Given the potential for 'app:resolve' to mutate arrays, keeping only the post-hook pass is cleaner, but either way avoid both.
For example, to drop the pre-hook pass:
- middleware = uniqueBy(await resolvePaths(nuxt, [...middleware].reverse(), 'path'), 'name').reverse() - plugins = uniqueBy(await resolvePaths(nuxt, plugins, 'src'), 'src')
195-207: Plugin aggregation and normalisation approach is soundGathering from layered configs and templated files before normalisation is clear. Forced casting in
normalizePlugin(plugin as NuxtPlugin)is a minor smell but acceptable here if upstream types are broad.If you want to avoid the cast, consider narrowing
config.pluginsat the source or adding a small type guard for plugin-like objects.
210-215: Re-introducing unspecified plugins preserves user orderIterating reversed and unshifting maintains original user-specified order at the front. One nit: the inner
some(p => p.src === plugin.src)shadows the outerp. Rename for readability.- if (!plugins.some(p => p.src === plugin.src)) { + if (!plugins.some(existing => existing.src === plugin.src)) { plugins.unshift(plugin) }packages/nuxt/test/app.test.ts (1)
269-301: Good guard against exposing partial app structuresThe Proxy-based test effectively ensures that layouts/middleware/plugins are only set with non-empty values, validating the “assign once when resolved” contract.
Two optional improvements:
- Track write counts per property to ensure exactly one assignment occurs for each of 'layouts', 'middleware', 'plugins' (stricter regression guard).
- Add a companion test that mutates app.* in an 'app:resolve' hook (e.g. pushing a plugin) and asserts the mutation persists post-resolve. This would catch the overwrite issue flagged in core.
Example augmentation for write counts:
- const app = new Proxy(_app, { + const writes: Record<string, number> = { layouts: 0, middleware: 0, plugins: 0 } + const app = new Proxy(_app, { get (target, p, receiver) { return Reflect.get(target, p, receiver) }, set (target, p, newValue, receiver) { if (p === 'middleware' || p === 'plugins') { expect(newValue).not.toEqual([]) + writes[p as string]!++ } if (p === 'layouts') { expect(newValue).not.toEqual({}) + writes[p as string]!++ } return Reflect.set(target, p, newValue, receiver) }, }) await resolveApp(nuxt, app) + expect(writes).toEqual({ layouts: 1, middleware: 1, plugins: 1 })If you’d like, I can draft the hook-mutation test as well.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (2)
packages/nuxt/src/core/app.ts(4 hunks)packages/nuxt/test/app.test.ts(1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx}
📄 CodeRabbit Inference Engine (.github/copilot-instructions.md)
Follow standard TypeScript conventions and best practices
Files:
packages/nuxt/src/core/app.tspackages/nuxt/test/app.test.ts
**/*.{test,spec}.{ts,tsx,js,jsx}
📄 CodeRabbit Inference Engine (.github/copilot-instructions.md)
Write unit tests for core functionality using
vitest
Files:
packages/nuxt/test/app.test.ts
🧠 Learnings (2)
📓 Common learnings
Learnt from: GalacticHypernova
PR: nuxt/nuxt#26468
File: packages/nuxt/src/components/plugins/loader.ts:24-24
Timestamp: 2024-11-05T15:22:54.759Z
Learning: 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: GalacticHypernova
PR: nuxt/nuxt#26468
File: packages/nuxt/src/components/plugins/loader.ts:24-24
Timestamp: 2024-11-05T15:22:54.759Z
Learning: 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/core/app.ts
🧬 Code Graph Analysis (2)
packages/nuxt/src/core/app.ts (3)
packages/nuxt/src/core/utils/names.ts (1)
hasSuffix(16-18)packages/nuxt/src/core/utils/index.ts (2)
hasSuffix(1-1)uniqueBy(4-16)packages/kit/src/resolve.ts (1)
findPath(68-86)
packages/nuxt/test/app.test.ts (2)
packages/nuxt/src/core/nuxt.ts (1)
loadNuxt(731-842)packages/nuxt/src/core/app.ts (2)
createApp(14-22)resolveApp(137-239)
🔇 Additional comments (4)
packages/nuxt/src/core/app.ts (4)
230-231: Single final assignment prevents exposing partial app stateAssigning layouts, middleware, plugins, and configs in one go avoids leaking partially populated values mid-resolution. This aligns with the PR intent and should reduce race-y consumers seeing transient empties.
160-172: Layout precedence relies on iteration order; sanity-check desired behaviourUsing
layouts[name] ||= { name, file }means the first occurrence wins. Given you iteratelayerConfigs(not reversed), confirm this preserves the desired precedence across layers for layouts (tests indicate it does). If the intended behaviour is “later layers override earlier ones”, be surelayerConfigsis ordered accordingly.
176-191: Middleware aggregation looks goodCollecting middleware across reversed layers and deferring normalisation until the end prevents partial writes and preserves override semantics via later de-duplication.
222-227: App config collection is straightforwardCollecting
app.configpaths per layer withfindPathand deferring de-duplication makes sense in the new pattern.
@nuxt/kit
nuxt
@nuxt/rspack-builder
@nuxt/schema
@nuxt/vite-builder
@nuxt/webpack-builder
commit: |
CodSpeed Performance ReportMerging #32993 will not alter performanceComparing Summary
|
🔗 Linked issue
resolves #30267
📚 Description
while I've not been able to reproduce the exact issue, this should resolve the problem by ensuring we don't expose in-progress app component values until they are fully populated/resolved.
it still might be possible for there to be issues if
appis accessed after theapp:resolvedhook, but I think that should not occur...