Skip to content

Make prerendering +server.js responses play nicer with undici's Response #9566

Description

@Conduitry

Describe the problem

If I have a +server.js endpoint that ends in a return fetch(...), this works fine in development, but if during building I attempt to prerender this endpoint's response, I get an error about headers being immutable from within undici. Presumably, SvelteKit is trying to change the response headers that it's getting from the +server.js function, and presumably the Response object that undici returns has had its headers frozen.

Describe the proposed solution

I think SvelteKit, at least when prerendering, should attempt to create a new Response object when it's adding its headers to the response.

Part of me has the potentially bad idea of running all undici responses through the code listed below in 'Alternatives considered'. Presumably someone, if they attempted to mutate such a Response in their handle hook after calling resolve would run into this same issue, even when not prerendering. (I've not actually tested this.) But perhaps at that point it's their responsibility to make sure they're handling this appropriately?

Alternatives considered

Right now, I'm running the responses through

new Response(response.body, {
		status: response.status,
		statusText: response.statusText,
		headers: new Headers(response.headers),
})

at the bottom of each relevant GET function, which works fine, but it took me a while to figure out what was going on and to arrive at this as a solution.

Importance

nice to have

Additional Information

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions