kit icon indicating copy to clipboard operation
kit copied to clipboard

fix: prefer pages over endpoints when prerendering

Open teemingc opened this issue 1 year ago • 5 comments

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:

  • [x] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • [x] This message body should clearly illustrate what problems it solves.
  • [ ] Ideally, include a test that fails without this PR but passes with it. No test because it would break the prerender app build.

Tests

  • [x] Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • [x] If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • [x] Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

teemingc avatar Nov 14 '24 10:11 teemingc

🦋 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

changeset-bot[bot] avatar Nov 14 '24 10:11 changeset-bot[bot]

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

this is an automated message

Rich-Harris avatar Nov 14 '24 10:11 Rich-Harris

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

Rich-Harris avatar Nov 14 '24 13:11 Rich-Harris

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

teemingc avatar Nov 15 '24 08:11 teemingc

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

teemingc avatar Nov 20 '24 06:11 teemingc

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

Agreed that this PR is the wrong fix. Other people are having a similar issue with bot indexing https://github.com/sveltejs/kit/issues/11794

teemingc avatar Aug 05 '25 03:08 teemingc