Skip to content

refactor(nitro): move to fetch handler in renderer - #35422

Merged
danielroe merged 12 commits into
mainfrom
refactor/fetch-handler
Jun 25, 2026
Merged

danielroe merged 12 commits into
mainfrom
refactor/fetch-handler

Conversation

@danielroe

Copy link
Copy Markdown
Member

🔗 Linked issue

📚 Description

in preparation for using nitro environments, this makes the ssr renderer a fetch handler rather than an event handler

@github-actions github-actions Bot added 5.x ♻️ refactor Code restructuring without behavior change labels Jun 23, 2026
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
@pkg-pr-new

pkg-pr-new Bot commented Jun 23, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@nuxt/kit

npm i https://pkg.pr.new/@nuxt/kit@35422

@nuxt/nitro-server

npm i https://pkg.pr.new/@nuxt/nitro-server@35422

nuxt

npm i https://pkg.pr.new/nuxt@35422

@nuxt/rspack-builder

npm i https://pkg.pr.new/@nuxt/rspack-builder@35422

@nuxt/schema

npm i https://pkg.pr.new/@nuxt/schema@35422

@nuxt/vite-builder

npm i https://pkg.pr.new/@nuxt/vite-builder@35422

@nuxt/webpack-builder

npm i https://pkg.pr.new/@nuxt/webpack-builder@35422

commit: c9c2821

@codspeed

codspeed Bot commented Jun 23, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 21 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing refactor/fetch-handler (c9c2821) with main (9083718)

Open in CodSpeed

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@danielroe
danielroe force-pushed the refactor/fetch-handler branch from cce2c25 to 2ef7ffe Compare June 23, 2026 13:06
@danielroe
danielroe marked this pull request as ready for review June 23, 2026 13:39
@coderabbitai

This comment has been minimized.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4420abe and 2ef7ffe.

📒 Files selected for processing (10)
  • packages/nitro-server/src/index.ts
  • packages/nitro-server/src/runtime/handlers/island.ts
  • packages/nitro-server/src/runtime/handlers/renderer.ts
  • packages/nitro-server/src/runtime/utils/cache.ts
  • packages/nitro-server/src/runtime/utils/renderer/app.ts
  • packages/nitro-server/src/runtime/utils/renderer/payload.ts
  • packages/nuxt/src/app/composables/router.ts
  • packages/nuxt/src/app/nuxt.ts
  • packages/nuxt/src/pages/runtime/plugins/router.ts
  • test/bundle.test.ts

Comment on lines +59 to +62
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
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🩺 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(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess this could be either FastResponse or HTTPResponse (it is even lighter but have to check if works)

@danielroe
danielroe merged commit 3269b86 into main Jun 25, 2026
30 checks passed
@danielroe
danielroe deleted the refactor/fetch-handler branch June 25, 2026 06:11
This was referenced Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

5.x ♻️ refactor Code restructuring without behavior change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants