feat: server side route resolution - #13379
Conversation
…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 detectedLatest commit: 27a8243 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 |
bc7cdff to
b05f960
Compare
…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
… and works better for the root
…r things like is_foo = _is_foo(...)
Rich-Harris
left a comment
There was a problem hiding this comment.
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
|
I tried to test this out by having route resolution set to Our scripts are hosted on CDN in different subdomain than the application. I noticed that the call to |
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>
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:
<a href="/foo/bar">..</a>_app/routes[route].js, so in this case_app/routes/foo/bar.jsWhat this enables:
TODO:
router.resolution = 'client' | 'server') or sth like thatPlease don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits