Skip to content

refactor: simplify endpoint/page routing logic and extend to HEAD - #16126

Merged
elliott-with-the-longest-name-on-github merged 2 commits into
prefer-pages-when-no-get-endpointfrom
prefer-pages-when-no-get-endpoint-refactor
Jun 22, 2026
Merged

refactor: simplify endpoint/page routing logic and extend to HEAD#16126
elliott-with-the-longest-name-on-github merged 2 commits into
prefer-pages-when-no-get-endpointfrom
prefer-pages-when-no-get-endpoint-refactor

Conversation

@elliott-with-the-longest-name-on-github

Copy link
Copy Markdown
Contributor

Follow-up to #16125. No behavioral change to the GET fix — this refactors the endpoint load + decide flow for readability and extends the "prefer page" behavior to HEAD requests.

Refactor

Restructures the endpoint loading and decision logic in respond.js into a single block with a positive endpoint_can_handle check, replacing:

  • the await-in-ternary pattern (hard to scan)
  • the duplicated !route.page check across load and use conditions
  • the double-negative condition for the HEAD-falls-back-to-GET case

The "load, then maybe discard" intent is now explicit and colocated.

HEAD fix

Extends the "prefer page" behavior to HEAD requests. Previously, a HEAD request with accept: */* to a page+endpoint route where the endpoint had neither HEAD nor GET would get 405 from render_endpoint, even though the page could handle HEAD. Now the page is rendered instead.

render_endpoint already falls back to GET for HEAD when GET exists — that case is preserved via the endpoint_can_handle check.

Also extends the Vary: Accept header to HEAD responses for page+endpoint routes, since HEAD can now be routed to either.

Tests

  • HEAD request prefers page when endpoint has no HEAD/GET/fallback
  • POST to post-only endpoint with sibling page still hits the endpoint
  • Fallback handler used instead of page when endpoint has no GET but has fallback (new fallback-with-page test route)
  • Content negotiation: accept: application/json hits endpoint, accept: text/html hits page (using existing routing/content-negotiation route)

- Restructure the endpoint load + decide flow into a single block with
  a positive `endpoint_can_handle` check, replacing the double-negative
  condition and await-in-ternary pattern
- Extend the 'prefer page' behavior to HEAD requests: an endpoint with
  no HEAD, no GET (which render_endpoint falls back to for HEAD), and no
  fallback now renders the page instead of returning 405
- Add Vary: Accept header to HEAD responses for page+endpoint routes
- Add tests for HEAD, POST, fallback, and content negotiation cases
- Add fallback-with-page test route
@changeset-bot

changeset-bot Bot commented Jun 22, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 7487d4e

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

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

Click here to learn what changesets are, and how to add one.

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

@svelte-docs-bot

Copy link
Copy Markdown

@dummdidumm dummdidumm left a comment

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.

fix the lint and this can go in 👍

@elliott-with-the-longest-name-on-github
elliott-with-the-longest-name-on-github merged commit c87c143 into prefer-pages-when-no-get-endpoint Jun 22, 2026
16 of 17 checks passed
@elliott-with-the-longest-name-on-github
elliott-with-the-longest-name-on-github deleted the prefer-pages-when-no-get-endpoint-refactor branch June 22, 2026 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants