Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: server side route resolution #13379

Merged
merged 75 commits into from
Feb 3, 2025
Merged

feat: server side route resolution #13379

merged 75 commits into from
Feb 3, 2025

Conversation

dummdidumm
Copy link
Member

@dummdidumm dummdidumm commented Jan 24, 2025

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
Copy link

changeset-bot bot commented Jan 24, 2025

🦋 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

packages/adapter-static/index.js Outdated Show resolved Hide resolved
packages/kit/src/core/config/options.js Outdated Show resolved Hide resolved
packages/kit/src/runtime/client/client.js Outdated Show resolved Hide resolved
packages/kit/src/runtime/client/client.js Outdated Show resolved Hide resolved
packages/kit/src/runtime/client/client.js Outdated Show resolved Hide resolved
packages/kit/src/runtime/client/parse.js Outdated Show resolved Hide resolved
packages/kit/src/runtime/server/page/server_routing.js Outdated Show resolved Hide resolved
packages/kit/src/runtime/server/page/server_routing.js Outdated Show resolved Hide resolved
…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
Copy link
Member

@Rich-Harris Rich-Harris left a comment

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

@dummdidumm dummdidumm merged commit 5906e97 into main Feb 3, 2025
15 checks passed
@dummdidumm dummdidumm deleted the server-side-routing branch February 3, 2025 09:33
@github-actions github-actions bot mentioned this pull request Jan 31, 2025
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.

3 participants