Skip to content

perf(nuxi): skip loading Nuxt in preview when default output dir exists#1274

Closed
zlotnika wants to merge 1 commit intonuxt:mainfrom
zlotnika:fix/preview-skip-loadnuxt
Closed

perf(nuxi): skip loading Nuxt in preview when default output dir exists#1274
zlotnika wants to merge 1 commit intonuxt:mainfrom
zlotnika:fix/preview-skip-loadnuxt

Conversation

@zlotnika
Copy link
Copy Markdown

@zlotnika zlotnika commented Apr 3, 2026

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.

Since nuxt build/nuxt generate always runs before nuxt preview, .output/nitro.json will already exist. This checks for it at the default path first and only falls back to the full loadNuxt resolution when it doesn't exist (i.e. custom nitro.output.dir).

Note: if a project switches from a custom output dir back to default and has a stale .output from a previous build, this could pick up the wrong output. In practice this is unlikely since rebuilding typically cleans the old output.

`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>
@zlotnika zlotnika requested a review from danielroe as a code owner April 3, 2026 22:14
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 3, 2026

  • nuxt-cli-playground

    npm i https://pkg.pr.new/create-nuxt@1274
    
    npm i https://pkg.pr.new/nuxi@1274
    
    npm i https://pkg.pr.new/@nuxt/cli@1274
    

commit: bcd41f2

@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@47c6773). Learn more about missing BASE report.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 3, 2026

Merging this PR will not alter performance

✅ 2 untouched benchmarks


Comparing zlotnika:fix/preview-skip-loadnuxt (bcd41f2) with main (47c6773)

Open in CodSpeed

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 3, 2026

📝 Walkthrough

Walkthrough

The preview command has been refactored to optimize the location and loading of nitro.json. It now computes a default output path (<cwd>/.output/nitro.json) and checks for its existence before loading Nuxt. If the default path contains nitro.json, it is used directly; otherwise, Nuxt is loaded to resolve an output directory via the nitro:init hook. This replaces the previous unconditional Nuxt-load behavior that searched both resolved and default output directories. The error message reporting the failure to find nitro.json has been updated to reference only the default output path.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main optimization: skipping Nuxt loading when the default output directory exists, directly matching the core change in the preview command.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description check ✅ Passed The pull request description clearly explains the motivation (avoiding false-positive warnings from modules checking env vars), the implementation approach (checking for default output first), and acknowledges edge cases.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with 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.

❤️ Share

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

Copy link
Copy Markdown

@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.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 47c6773 and bcd41f2.

📒 Files selected for processing (1)
  • packages/nuxi/src/commands/preview.ts

Comment on lines +47 to +49
if (existsSync(defaultOutput)) {
nitroJSONPath = defaultOutput
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

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.

Comment on lines 80 to 83
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}`,
)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

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').

@zlotnika zlotnika closed this Apr 3, 2026
@zlotnika zlotnika deleted the fix/preview-skip-loadnuxt branch April 3, 2026 22:53
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.

2 participants