Skip to content

fix: run reroute in a middleware function before split functions - #16590

Closed
teemingc wants to merge 42 commits into
version-3from
fix-split-reroute
Closed

fix: run reroute in a middleware function before split functions#16590
teemingc wants to merge 42 commits into
version-3from
fix-split-reroute

Conversation

@teemingc

@teemingc teemingc commented Jul 30, 2026

Copy link
Copy Markdown
Member

fixes #11879

See #12296 for context

This PR adds a middleware that runs reroute beforehand so that, when there are multiple functions, the correct one is invoked instead of not matching any function matchers.

The Vercel middleware uses a simplified version of the rewrite helper from @vercel/functions to avoid pulling in the whole bundle.

The Netlify middleware resolves the URL then passes it to the next handler.

There's currently an issue with Vercel rewrites discarding the SvelteKit form action in the URL because the query parameter doesn't have a value vercel/vercel#12902

TODOS

  • implement Vercel middleware
  • add split test app on Vercel

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

  • 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
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • 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

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

@pkg-svelte-dev

pkg-svelte-dev Bot commented Jul 30, 2026

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from 29e21be:

pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/29e21be8e7c7f23875442bb75ba1bb8050b5bd3c

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/kit/pr/16590

@changeset-bot

changeset-bot Bot commented Jul 30, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 29e21be

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

This PR includes changesets to release 3 packages
Name Type
@sveltejs/adapter-netlify Major
@sveltejs/adapter-vercel Major
@sveltejs/kit Minor

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

@teemingc teemingc added needs-platform-tests This PR needs to run platform tests in order to merge. pkg:adapter-vercel Pertaining to the Vercel adapter pkg:adapter-netlify labels Jul 30, 2026
@svelte-docs-bot

Copy link
Copy Markdown

@teemingc
teemingc marked this pull request as ready for review July 30, 2026 20:45
Comment thread .github/workflows/platform-tests-vercel.yml
@teemingc
teemingc marked this pull request as draft July 30, 2026 20:51
Comment thread packages/kit/src/runtime/server/respond.js Outdated
@teemingc
teemingc marked this pull request as ready for review July 31, 2026 11:05
Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
Comment thread documentation/docs/25-build-and-deploy/80-adapter-netlify.md Outdated
Comment thread documentation/docs/25-build-and-deploy/90-adapter-vercel.md Outdated
Co-authored-by: Tee Ming <chewteeming01@gmail.com>
Comment thread .changeset/mighty-groups-taste.md Outdated
Comment thread packages/kit/src/core/adapt/builder.js
let new_request;

// `reroute` hooks receive pathnames, so they must not run for route-ID resolution requests
if (remote_id || is_route_id_resolution_request) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Don't really like that we have to duplicate the "should run reroute" check logic between here and respond.js but I'm not really sure what's the best way to dedupe it right now

/**
* @param {URL} url
*/
export function get_remote_id(url) {

@teemingc teemingc Aug 4, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Had to move this to a less packed module. Right now Rolldown can't treeshake anything that dynamically imports a module. This causes things like await import('otel') or await import('node:async_hooks') in any module to completely break treeshaking. We don't want otel bundled into the reroute middleware.

Comment thread .changeset/mighty-groups-taste.md Outdated
@teemingc
teemingc marked this pull request as draft August 4, 2026 18:38
@teemingc
teemingc marked this pull request as ready for review August 5, 2026 14:38
@teemingc
teemingc requested a review from Rich-Harris August 5, 2026 15:00
new_request = new Request(request);
} else {
const { url: normalized_url, denormalize } = normalizeUrl(url);
const resolved_path = await reroute({ url: normalized_url, fetch });

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.

Unfortunately I don't think this is right — normally fetch would be the internal fetch implementation that handles root-relative paths and applies handleFetch (and forwards cookies etc)

@teemingc teemingc Aug 5, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ugh, you're right and that complicates things... I wonder if we take a different approach and try to handle this in the catch-all serverless function instead.

@teemingc
teemingc marked this pull request as draft August 5, 2026 18:02
@teemingc teemingc closed this Aug 6, 2026
Rich-Harris added a commit that referenced this pull request Aug 20, 2026
…16665)

alternative to #16590

fixes #11879

This PR makes the assumption that a catch-all serverless function is
deployed with no routes in its manifest (the alternative was an explicit
flag). It then exports a helper which should be called in such a
serverless function after `server.respond` so that it can invoke the
correct handler instead of simply returning a 404 since it has no route
information in its SSR manifest.

The advantage of this approach compared to deploying a middleware that
only runs reroute is that it's simpler implementation-wise and it allows
the user's `handleFetch`, `init` hooks, etc. to run correctly without
any additional effort from us.

The disadvantages are that:
- `reroute` will run twice: once in the catch-all function then again in
the correct function. We _could_ choose to skip the second `reroute`
based on some `header` value or context (if any) that can be passed
between serverless functions. Otherwise, I chose to omit this so that
the `reroute` hook can't be skipped from outside influence (someone
setting the header themselves without running `reroute` first)
- Slightly bigger serverless function bundle than a middleware which
just runs `reroute`. Not that big of an issue I think...

---

### 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.
- [x] Ideally, include a test that fails without this PR but passes with
it.

### Tests
- [ ] 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.

---------

Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-platform-tests This PR needs to run platform tests in order to merge. pkg:adapter-netlify pkg:adapter-vercel Pertaining to the Vercel adapter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

reroute hook breaks when deployed on Vercel if the app is deployed as multiple functions

2 participants