Repository navigation
refactor(nitro): move to fetch handler in renderer - #35422
Conversation
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
@nuxt/kit
@nuxt/nitro-server
nuxt
@nuxt/rspack-builder
@nuxt/schema
@nuxt/vite-builder
@nuxt/webpack-builder
commit: |
Merging this PR will not alter performance
Comparing Footnotes
|
cce2c25 to
2ef7ffe
Compare
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 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/nitro-server/src/runtime/handlers/island.ts`:
- Around line 65-73: The current implementation throws an HTTPError when
ssrContext['~renderResponse'] has a status >= 400, which drops the original
Response body and headers. Instead, route all ~renderResponse instances through
the returnIslandResponse function regardless of status code to preserve the
native Response contract. Remove the conditional check that throws the HTTPError
for status >= 400, and simply call returnIslandResponse(event, response) for all
~renderResponse cases.
In `@packages/nitro-server/src/runtime/handlers/renderer.ts`:
- Line 24: Create a new helper function `returnRenderResponse` that accepts an
H3Event and Response object, then use this helper at all direct return
statements (lines 229, 504, 512, 591, and 603) instead of returning the Response
directly. The helper should merge the headers from event.res with the response
headers using the mergeHeaders utility, and return a new Response with the
merged headers, original status, statusText, and body preserved. This ensures
that headers and status mutated on event.res during render hooks (like
Set-Cookie from app:rendered) are not lost on early responses.
In `@packages/nitro-server/src/runtime/utils/renderer/app.ts`:
- Around line 59-62: The rethrowWithResponseHeaders function attempts to mutate
the headers property on the error object without first checking if error is
actually an object, which will cause a TypeError if userland code throws a
non-object value like null, a string, or a number. Add a guard condition at the
beginning of the function to check if error is an object before attempting to
set error.headers, and if it is not an object, simply rethrow the error as-is
without modification. This ensures that non-object throws are passed through
unchanged and prevents the TypeError from masking the original failure.
🪄 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: 82938f06-0cab-4ff3-95d0-eda3776b2645
📒 Files selected for processing (10)
packages/nitro-server/src/index.tspackages/nitro-server/src/runtime/handlers/island.tspackages/nitro-server/src/runtime/handlers/renderer.tspackages/nitro-server/src/runtime/utils/cache.tspackages/nitro-server/src/runtime/utils/renderer/app.tspackages/nitro-server/src/runtime/utils/renderer/payload.tspackages/nuxt/src/app/composables/router.tspackages/nuxt/src/app/nuxt.tspackages/nuxt/src/pages/runtime/plugins/router.tstest/bundle.test.ts
| export function rethrowWithResponseHeaders (event: H3Event, error: any): never { | ||
| error.headers = mergeHeaders(error.headers instanceof Headers ? error.headers : new Headers(error.headers), event.res.headers) | ||
| throw error | ||
| } |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Guard non-object throws before mutating error.headers.
Line 60 assumes error is object-like. If userland throws null/string/number, this path throws a new TypeError and masks the original failure.
Suggested patch
export function rethrowWithResponseHeaders (event: H3Event, error: any): never {
+ if (!error || (typeof error !== 'object' && typeof error !== 'function')) {
+ throw error
+ }
error.headers = mergeHeaders(error.headers instanceof Headers ? error.headers : new Headers(error.headers), event.res.headers)
throw error
}🤖 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/nitro-server/src/runtime/utils/renderer/app.ts` around lines 59 -
62, The rethrowWithResponseHeaders function attempts to mutate the headers
property on the error object without first checking if error is actually an
object, which will cause a TypeError if userland code throws a non-object value
like null, a string, or a number. Add a guard condition at the beginning of the
function to check if error is an object before attempting to set error.headers,
and if it is not an object, simply rethrow the error as-is without modification.
This ensures that non-object throws are passed through unchanged and prevents
the TypeError from masking the original failure.
| body: `<!DOCTYPE html><html><head><meta http-equiv="refresh" content="0; url=${encodedLoc}"></head></html>`, | ||
| headers: { location: encodedHeader }, | ||
| } | ||
| nuxtApp.ssrContext!['~renderResponse'] = new Response( |
There was a problem hiding this comment.
I guess this could be either FastResponse or HTTPResponse (it is even lighter but have to check if works)
🔗 Linked issue
📚 Description
in preparation for using nitro environments, this makes the ssr renderer a fetch handler rather than an event handler