perf(nuxi): skip loading Nuxt in preview when default output dir exists#1274
perf(nuxi): skip loading Nuxt in preview when default output dir exists#1274
Conversation
`nuxt preview` calls `loadNuxt({ ready: true })` to resolve the output
directory, which runs all user module `setup()` functions. Modules that
check for env vars (like `@nuxtjs/supabase` checking `SUPABASE_URL`)
emit false-positive warnings because those values are already baked into
the build output and aren't needed at preview time.
Check for `.output/nitro.json` at the default path first and only fall
back to the full `loadNuxt` resolution when it doesn't exist.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
commit: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1274 +/- ##
=======================================
Coverage ? 44.41%
=======================================
Files ? 48
Lines ? 1137
Branches ? 324
=======================================
Hits ? 505
Misses ? 516
Partials ? 116 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
📝 WalkthroughWalkthroughThe preview command has been refactored to optimize the location and loading of Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 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.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/nuxi/src/commands/preview.ts`:
- Around line 80-83: The error message logged when nitroJSONPath is not found
should include the actual path(s) that were checked (the value of nitroJSONPath
or its resolved lookup) in addition to the defaultOutput; update the
logger.error call in preview.ts (the block referencing nitroJSONPath,
defaultOutput and colors.cyan('nitro.json')) to interpolate or append the real
checked path(s) (nitroJSONPath or its resolved candidate) so the message shows
both the searched defaultOutput and the actual path(s) attempted, keeping the
existing descriptive text about running colors.cyan('nuxi build').
- Around line 47-49: The fast-path that sets nitroJSONPath when
existsSync(defaultOutput) should not short-circuit when the CLI `--extends`
option is present; modify the logic around existsSync(defaultOutput) and the
later override (the code setting nitroJSONPath on the explicit override path) so
that if an extends value is provided (the parsed option used by preview, e.g.,
the `--extends` flag variable), you skip the short-circuit and allow the
explicit nitroJSONPath override to take effect. In practice, check the parsed
extends option (the same variable used later in the preview command) before
assigning nitroJSONPath from defaultOutput and apply the same conditional to the
later 63-64 fast-path to preserve the layered build when `--extends` is passed.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: eabc2fca-4127-4487-88e5-db3a0852ed69
📒 Files selected for processing (1)
packages/nuxi/src/commands/preview.ts
| if (existsSync(defaultOutput)) { | ||
| nitroJSONPath = defaultOutput | ||
| } |
There was a problem hiding this comment.
Preserve --extends on the .output fast path.
Line 47 short-circuits before the explicit override on Line 63 is ever applied. If cwd already has a .output/nitro.json, nuxi preview --extends ... can boot that artifact instead of the layered build the caller asked for.
💡 One minimal way to keep the optimization for the common case
- if (existsSync(defaultOutput)) {
+ if (existsSync(defaultOutput) && !ctx.args.extends) {
nitroJSONPath = defaultOutput
}
else {
const { loadNuxt } = await loadKit(cwd)
@@
})
- if (resolvedOutputDir && existsSync(resolvedOutputDir)) {
+ if (resolvedOutputDir && existsSync(resolvedOutputDir)) {
nitroJSONPath = resolvedOutputDir
}
+ else if (existsSync(defaultOutput)) {
+ nitroJSONPath = defaultOutput
+ }
}Also applies to: 63-64
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/nuxi/src/commands/preview.ts` around lines 47 - 49, The fast-path
that sets nitroJSONPath when existsSync(defaultOutput) should not short-circuit
when the CLI `--extends` option is present; modify the logic around
existsSync(defaultOutput) and the later override (the code setting nitroJSONPath
on the explicit override path) so that if an extends value is provided (the
parsed option used by preview, e.g., the `--extends` flag variable), you skip
the short-circuit and allow the explicit nitroJSONPath override to take effect.
In practice, check the parsed extends option (the same variable used later in
the preview command) before assigning nitroJSONPath from defaultOutput and apply
the same conditional to the later 63-64 fast-path to preserve the layered build
when `--extends` is passed.
| if (!nitroJSONPath) { | ||
| logger.error( | ||
| `Cannot find ${colors.cyan('nitro.json')}. Did you run ${colors.cyan('nuxi build')} first? Search path:\n${nitroJSONPaths.join('\n')}`, | ||
| `Cannot find ${colors.cyan('nitro.json')}. Did you run ${colors.cyan('nuxi build')} first? Search path:\n${defaultOutput}`, | ||
| ) |
There was a problem hiding this comment.
Report the path that was actually checked.
Once the fallback runs, a miss may come from a non-default nitro.json location, but this error still prints only defaultOutput. That makes the failure harder to debug because the reported search path no longer matches the lookup logic.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/nuxi/src/commands/preview.ts` around lines 80 - 83, The error
message logged when nitroJSONPath is not found should include the actual path(s)
that were checked (the value of nitroJSONPath or its resolved lookup) in
addition to the defaultOutput; update the logger.error call in preview.ts (the
block referencing nitroJSONPath, defaultOutput and colors.cyan('nitro.json')) to
interpolate or append the real checked path(s) (nitroJSONPath or its resolved
candidate) so the message shows both the searched defaultOutput and the actual
path(s) attempted, keeping the existing descriptive text about running
colors.cyan('nuxi build').
nuxt previewcallsloadNuxt({ ready: true })to resolve the output directory, which runs all user modulesetup()functions. Modules that check for env vars (like@nuxtjs/supabasecheckingSUPABASE_URL) emit false-positive warnings because those values are already baked into the build output and aren't needed at preview time.Since
nuxt build/nuxt generatealways runs beforenuxt preview,.output/nitro.jsonwill already exist. This checks for it at the default path first and only falls back to the fullloadNuxtresolution when it doesn't exist (i.e. customnitro.output.dir).Note: if a project switches from a custom output dir back to default and has a stale
.outputfrom a previous build, this could pick up the wrong output. In practice this is unlikely since rebuilding typically cleans the old output.