sveltejs / kit

web development, streamlined
https://svelte.dev/docs/kit
MIT License
18.76k stars 1.95k forks source link

fix: prefer pages over endpoints when prerendering #12998

Open eltigerchino opened 1 week ago

eltigerchino commented 1 week ago

fixes https://github.com/sveltejs/kit/issues/10735

This PR adds an Accept header to the prerender requests, preferring text/html first then whatever with */*. Previously, it would always end up as */* during content negotiation, which invokes the endpoint handler, causing a 405 method not allowed if the GET handler doesn't exist.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

Tests

Changesets

Edits

changeset-bot[bot] commented 1 week ago

🦋 Changeset detected

Latest commit: 7ab5f9762e5eb0df8cb27acd0bf8bd596f54123e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | ------------- | ----- | | @sveltejs/kit | Patch |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Rich-Harris commented 1 week ago

preview: https://svelte-dev-git-preview-kit-12998-svelte.vercel.app/

this is an automated message

Rich-Harris commented 1 week ago

Is this the right fix? Feels like maybe 405 is the wrong response for a GET request with */* if a page exists. Thoughts?

eltigerchino commented 1 week ago

Is a test still TODO or is it basically impossable to test?

I chose not to add a test because it's only reproducible by breaking the prerender app build. But I could add a test that just sends a request to the route and see if we get a 405.

Is this the right fix? Feels like maybe 405 is the wrong response for a GET request with */* if a page exists. Thoughts?

An alternative is to await the route.endpoint() call earlier so we can check which methods it has but I'm not sure what kind of impact that will have. Either that, or prefer pages over endpoints during content negotiation (or earlier).

https://github.com/sveltejs/kit/blob/435984bf61b047d1e1a8efe88354ca7ac4e9109f/packages/kit/src/runtime/server/respond.js#L451

eltigerchino commented 6 days ago

related to content negotiation https://github.com/sveltejs/kit/issues/11794