Skip to content

breaking: change preloadCode(pathname) to preloadCode(routeId), make match pre-resolve loaders - #16576

Open
elliott-with-the-longest-name-on-github wants to merge 5 commits into
version-3from
elliott/preload-code-route-id
Open

breaking: change preloadCode(pathname) to preloadCode(routeId), make match pre-resolve loaders#16576
elliott-with-the-longest-name-on-github wants to merge 5 commits into
version-3from
elliott/preload-code-route-id

Conversation

@elliott-with-the-longest-name-on-github

Copy link
Copy Markdown
Contributor

closes #16511

The top-of-the-line goal of this PR is to change preloadCode(pathname) to preloadCode(routeId). This just makes more sense -- the code corresponds to the route you wrote, not the pathname you might be requesting. If you need to resolve a pathname to a route ID, you can always do this:

const route = await match('/foo');
await preloadCode(route.id);

In order to enable this without creating unnecessary waterfalls (where match has to go to the server, and then preloadCode has to go to the server to resolve the route id to its loaders), match caches its resolutions' loaders when it executes.

Basically what this means is that:

  • If serverside route resolution is not enabled
    • match resolution is trivial, because the full routing manifest is available clientside already
  • Else, when routes do resolve on the server, match consults the server for the route definition, then immutably caches the route and its loaders for future reference by preloadCode

There's one additional challenge: When preloadCode is called with a route ID that wasn't resolved using match, it has no cache entry to pull the loaders from. It can't use match internally to resolve the route, because match operates on pathnames and has no ability to resolve a route ID. To enable this, this PR adds an _app/routes/[...id]/__route.js endpoint that operates very similarly to the existing serverside route resolution and allows resolving route IDs to their route info. This enables preloadCode to fall back to actually looking up the information if it hasn't been pre-resolved and cached.

@pkg-svelte-dev

pkg-svelte-dev Bot commented Jul 29, 2026

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from 49c99c0:

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

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

@changeset-bot

changeset-bot Bot commented Jul 29, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 49c99c0

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

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Major

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/kit/src/runtime/server/page/render.js
Comment thread packages/kit/src/runtime/server/page/server_routing.js Outdated
Comment thread packages/kit/src/runtime/server/page/render.js Outdated
Comment thread packages/kit/src/runtime/server/page/server_routing.js
Nic-Polumeyv and others added 4 commits July 30, 2026 10:03
…ing (#16578)

Stacked on #16576, per
#16576 (comment).

`dependencies` is per-`visit()`, so the route-ID module was generated
once per prerendered page. On the `prerendering/basics` fixture with
`resolution: 'server'`, 31 generations produced 28 files; with this, 28
produce 28, and the output is byte-identical apart from the build
version stamp.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update preloadCode API

3 participants