fix(nuxt): skip plugin initialization during server-side error render#35320
fix(nuxt): skip plugin initialization during server-side error render#35320stephanelgrg wants to merge 2 commits into
Conversation
@nuxt/kit
@nuxt/nitro-server
nuxt
@nuxt/rspack-builder
@nuxt/schema
@nuxt/vite-builder
@nuxt/webpack-builder
commit: |
|
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 (2)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughThis pull request modifies the server-side entry point to detect and handle error-render scenarios. When the server context contains an existing error (via Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/nuxt/src/app/entry.ts`:
- Around line 40-42: The current early-return when isErrorRender prevents
applyPlugins(nuxt, plugins) from running and breaks router/link-dependent error
pages; ensure plugin initialization still runs during SSR error render by
removing or altering the isErrorRender guard so applyPlugins(nuxt, plugins) is
always invoked (or at minimum ensure the router plugin and any plugins that
register nuxtApp.hooks.hookOnce('app:created', ...) and provide $router are
executed before error rendering); locate the conditional around applyPlugins in
entry.ts and either move applyPlugins outside the isErrorRender branch or
explicitly call the router-related plugin setup prior to rendering errors so
useRouter() and NuxtLink remain available.
🪄 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: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: f4cc0093-dcec-43a3-8341-6dedb72cabf1
📒 Files selected for processing (1)
packages/nuxt/src/app/entry.ts
| if (!isErrorRender) { | ||
| await applyPlugins(nuxt, plugins) | ||
| } |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Description: Check if error pages depend on plugin-provided features
# Find error page components and check for router/nuxt-link usage
echo "=== Checking error pages for router/link usage ==="
fd -e vue -e ts -e js --full-path 'error' --exec rg -l 'NuxtLink|useRouter|useRoute' {}
# Check for plugin files that register critical hooks
echo -e "\n=== Checking plugins for app:created hooks ==="
fd -e ts -e js --full-path 'plugins' --exec rg -n "hooks\.(hook|hookOnce)\s*\(\s*['\"]app:created" {}
# Look for server-only plugins that might be the source of recursion
echo -e "\n=== Finding server-only plugins ==="
fd '\.server\.(ts|js)$' --full-path 'plugins'Repository: nuxt/nuxt
Length of output: 594
Avoid breaking SSR error rendering by skipping all plugins
Skipping applyPlugins(nuxt, plugins) during SSR error render prevents plugin-provided setup from running—particularly the router plugin logic that registers nuxtApp.hooks.hookOnce('app:created', ...) and provides $router. Error-page-related code already depends on router/link functionality (e.g. test/fixtures/basic/app/pages/navigate-to-validate-custom-error.vue uses NuxtLink/router APIs, and packages/nuxt/src/app/composables/error.ts uses useRouter()), so error rendering can fail or misbehave without plugin initialisation.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@packages/nuxt/src/app/entry.ts` around lines 40 - 42, The current
early-return when isErrorRender prevents applyPlugins(nuxt, plugins) from
running and breaks router/link-dependent error pages; ensure plugin
initialization still runs during SSR error render by removing or altering the
isErrorRender guard so applyPlugins(nuxt, plugins) is always invoked (or at
minimum ensure the router plugin and any plugins that register
nuxtApp.hooks.hookOnce('app:created', ...) and provide $router are executed
before error rendering); locate the conditional around applyPlugins in entry.ts
and either move applyPlugins outside the isErrorRender branch or explicitly call
the router-related plugin setup prior to rendering errors so useRouter() and
NuxtLink remain available.
Merging this PR will not alter performance
Comparing Footnotes
|
danielroe
left a comment
There was a problem hiding this comment.
this would mean you couldn't access vue router, useRoute, etc., which are instantiated in plugins
in general this indicates a bug in user code - and we can't know for sure which plugin to skip, if any
|
Yes you are right, for the 1st run as there's no error yet all the plugins are going to run. I'll think about it and keep you posted ✌️ |
|
I mean, we need probably need plugins to run to render the error page. the ideal here would be to warn the user if this happens in development (with an explanation of which plugin, perhaps?) and ideally abort an infinite loop with a better error in production... |
|
💯 I'll check multiple things and come-back to you to know in which direction to go. But what I've in mind is this:
Maybe I should proceed by baby steps (to move forward) 🐾
This is so interesting, I love it! 🤩 |
🔗 Linked issue
Resolves #20909
📚 Description
When a server-only plugin calls $fetch() for a server route and that route throws, Nuxt would still initialize plugins during the server error render. That caused the plugin to run again while rendering the error response, leading to an infinite recursion loop.
Fix
Detect server-side error renders in entry.ts.
Skip applyPlugins(nuxt, plugins) when ssrContext.payload.error or ssrContext.error is present
Preserve normal hook invocation and error handling for error rendering