Skip to content

feat: server side route resolution - #13379

Merged
dummdidumm merged 75 commits into
mainfrom
server-side-routing
Feb 3, 2025
Merged

feat: server side route resolution#13379
dummdidumm merged 75 commits into
mainfrom
server-side-routing

Conversation

@dummdidumm

@dummdidumm dummdidumm commented Jan 24, 2025

Copy link
Copy Markdown
Member

This PR adds server side route resolution to SvelteKit. This means that instead of loading the whole routing manifest in the client, and doing the route resolution there, the server runtime is invoked for each route request.

How this works:

  • user clicks on <a href="/foo/bar">..</a>
  • server call to _app/routes[route].js, so in this case _app/routes/foo/bar.js
  • SvelteKit server runtime does route resolution (does this match a route, taking reroutes into account etc) on the server and returns a response that is a JavaScript file containing the route information in a format that can be parsed on the client

What this enables:

  • Projects with massive routes can use this to not send so many kilobytes up front to the client, because the client routing manifest would no longer be sent
  • You can hide what routes you have
  • Because the server is hit for every route resolution, you can put things like edge middleware in front and be confident it is always called, and for example do rewrites (for example for A/B testing) in there

TODO:

  • add an option for this (router.resolution = 'client' | 'server') or sth like that
  • validate option such that you cannot combine it with hash routing or another bundle strategy than "split"
  • use said option to generate a boolean so that we can treeshake code on the client / not load the client manifest / etc
  • tests
  • docs
  • enhance adapter API such that it's possible to deploy this as a separate route, so for example it can be deployed to the edge on Vercel (wasn't actually needed, you can do it with the existing APIs)

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.

…use in prod the imports are different / not resolved through vite / wrong; also it's better this way); better hydration without an unnecessary roundtrip
@changeset-bot

changeset-bot Bot commented Jan 24, 2025

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 27a8243

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

This PR includes changesets to release 2 packages
Name Type
@sveltejs/adapter-vercel Minor
@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

@svelte-docs-bot

Copy link
Copy Markdown

Comment thread packages/adapter-static/index.js Outdated
Comment thread packages/kit/src/core/config/options.js Outdated
Comment thread packages/kit/src/core/sync/write_client_manifest.js
Comment thread packages/kit/src/runtime/client/client.js Outdated
Comment thread packages/kit/src/runtime/client/client.js Outdated
Comment thread packages/kit/src/runtime/client/client.js Outdated
Comment thread packages/kit/src/runtime/client/parse.js
Comment thread packages/kit/src/runtime/server/page/server_routing.js Outdated
Comment thread packages/kit/src/runtime/server/page/server_routing.js
Comment thread packages/kit/src/runtime/server/page/server_routing.js Outdated
…es work at runtime: we don't need to dynamically import server routes to get certain info, which is especially important for when we generate edge functions for these, where the server will have much less work to do / files to deploy. Also fixes a bug where prerendered routes with ssr=false were not showing up in the server manifest, and will prevent potential bugs in adapters when they split routes

@Rich-Harris Rich-Harris 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.

alright I think this is ready to go! The edge function generated in adapter-vercel still has a lot of dead weight — it includes all the Svelte rendering code, even though there's nothing to render — but I think it would be reasonable to tackle that later rather than blocking on it

@kkarikos

Copy link
Copy Markdown
Contributor

I tried to test this out by having route resolution set to "server".

Our scripts are hosted on CDN in different subdomain than the application. I noticed that the call to __route.js is sent to the https://cdn.example.com/route/__route.js instead of https://example.com/route/__route.js. Not exactly sure what is causing this behavior but it seems like a bug.

@benmccann benmccann mentioned this pull request Oct 9, 2025
Rich-Harris pushed a commit that referenced this pull request Jul 18, 2026
closes #14896

The `router.resolution` docs say the server "has an opportunity to
intercept each navigation (for example through a middleware)". #14896
read that as "the `handle` hook runs", which it does not. Route
resolution requests are answered as soon as the route has been looked up
([respond.js#L323-L324](https://github.com/sveltejs/kit/blob/1406668dbc9c18928dabeff9d15ec756d6d455b3/packages/kit/src/runtime/server/respond.js#L323-L324)),
before `handle` is invoked and after `reroute` has run
([respond.js#L249](https://github.com/sveltejs/kit/blob/1406668dbc9c18928dabeff9d15ec756d6d455b3/packages/kit/src/runtime/server/respond.js#L249)).
Verified empirically in dev and in preview after a production build,
details in
#14896 (comment).
The wording dates to #13379, which introduced server-side route
resolution.

The new sentence is scoped to the route lookup rather than stating a
blanket "handle never runs", because there is one path where it does. A
pathname that fails to decode falls through to normal request handling
([respond.js#L272-L277](https://github.com/sveltejs/kit/blob/1406668dbc9c18928dabeff9d15ec756d6d455b3/packages/kit/src/runtime/server/respond.js#L272-L277)),
deliberate behavior from #15744 so that malformed URLs render the
default error page. In production `GET /%c0/__route.js` therefore
reaches `handle` with pathname `/%c0` before being rejected with a 400.
Dev masks this because the dev middleware rejects malformed URIs
earlier, which #15744 also notes.

Two changes to the option's JSDoc. "Middleware" now says what it refers
to, and a sentence states which hooks run for resolution requests.
`types/index.d.ts` regenerated.

Note on sequencing, #16396 also touches `public.d.ts` (different lines)
and regenerates `types/index.d.ts`; whichever merges second needs a
trivial rebase and regeneration.

---

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

---------

Co-authored-by: Nic Polumeyv <nicolas.polum@gmail.com>
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.

4 participants