Skip to content

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

Open
teemingc wants to merge 29 commits into
version-3from
fix-split-reroute
Open

fix: run reroute in a middleware function before split functions#16590
teemingc wants to merge 29 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 df404ce:

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

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: df404ce

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
@teemingc
teemingc changed the base branch from version-3 to chore-vercel-split-test July 31, 2026 14:32
@@ -0,0 +1,6 @@
---
'@sveltejs/adapter-netlify': major
'@sveltejs/adapter-vercel': major

@teemingc teemingc Jul 31, 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.

using a major here to make it more noticeable that folks will have this extra middleware function deployed

Comment thread documentation/docs/25-build-and-deploy/90-adapter-vercel.md Outdated
Comment thread packages/adapter-vercel/index.js
@teemingc teemingc added this to the 3.0 milestone Aug 3, 2026
Base automatically changed from chore-vercel-split-test to version-3 August 3, 2026 20:55
teemingc added a commit that referenced this pull request Aug 3, 2026
Split out from #16590 which adds a
test to ensure we don't regress from getting `reroute` to work with
multiple Vercel functions

I've initialised a project at
https://vercel.com/svelte/platform-tests-split but will need help
checking if its configured correctly. Will also need someone with
permissions to add the secret deployment ID to this repo for
`secrets.VERCEL_PROJECT_ID_SPLIT`
@teemingc teemingc added needs-ecosystem-ci This PR needs to have an ecosystem CI run started prior to its last commit before merging. and removed needs-ecosystem-ci This PR needs to have an ecosystem CI run started prior to its last commit before merging. labels Aug 3, 2026

### Individual functions and `reroute`

If the `split` option is set to `true` for a route, or at the adapter level, the [`reroute`](hooks#reroute) function will be deployed as a middleware that runs before any individual function.

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.

is this right, or does it also apply to apps that are implicitly split because of differing configs?

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.

Yeah, the latter; as long as there's more than one function

Comment thread packages/adapter-vercel/index.js Outdated
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>
'@sveltejs/kit': minor
---

feat: add `applyReroute` and `builder.getReroutePath` helpers for running `reroute` in a middleware before the main handler

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.

I'm not 100% sure about this API. It's called getReroutePath but it returns a path to the universal hooks.js file which also includes transport (and whatever else we add in future) so at the very least it should be named getUniversalHooksPath or something.

But we probably only want to expose the reroute logic, which leaves me wondering if it makes sense to do something like this instead:

import { applyReroute } from 'WHATEVER';

export default function middleware(request) {
	return applyReroute(request);
}

@teemingc teemingc Aug 3, 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.

Mm would it be a good idea to have a builder.generateReroute() which bundles up the reroute logic and returns a string instead of writing to disk? and it returns undefined if the adapter doesn't need to care about reroute?

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