sveltejs/kit · error

argument passed to preloadCode must be a route ID (i.e. "/bl

Error message

argument passed to preloadCode must be a route ID (i.e. "/blog/[slug]" rather than "blog/[slug]")

What it means

preloadCode() expects a route ID in the framework's internal format, which always starts with a leading slash and includes bracketed parameters (e.g. "/blog/[slug]"). In DEV a missing leading slash throws immediately with guidance; a wrong-format ID would otherwise silently match no route.

Source

Thrown at packages/kit/src/runtime/client/client.js:2858

 * [`match`](https://svelte.dev/docs/kit/$app-paths#match) from `$app/paths`:
 *
 * ```js
 * import { match } from '$app/paths';
 * import { preloadCode } from '$app/navigation';
 *
 * const matched = await match('/blog/hello-world');
 * if (matched) await preloadCode(matched.id);
 * ```
 *
 * Unlike `preloadData`, this won't call `load` functions.
 * Returns a Promise that resolves when the modules have been imported.
 *
 * @param {import('$app/types').RouteId} id
 * @returns {Promise<void>}
 */
export async function preloadCode(id) {
	if (DEV && id[0] !== '/') {
		throw new Error(
			`argument passed to preloadCode must be a route ID (i.e. "/blog/[slug]" rather than "blog/[slug]")`
		);
	}

	const route = __SVELTEKIT_CLIENT_ROUTING__
		? routes.find((r) => r.id === id)
		: (route_id_cache.get(id) ?? (await load_route_by_id(id)));

	if (route === ENDPOINT_ONLY) {
		if (DEV) {
			console.warn(
				`'${id}' has no \`+page\`, so there is no code to preload. If you meant to warm up an ` +
					`endpoint, request it with \`fetch\` instead.`
			);
		}

		return;
	}

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Prefix the argument with '/' and use the parameterized form: '/blog/[slug]'
  2. Derive route IDs from your src/routes directory structure, not runtime URLs
  3. Handle runtime parameter values with preloadData instead of preloadCode

Example fix

// before
await preloadCode('blog/[slug]');
await preloadCode('/blog/hello');
// after
await preloadCode('/blog/[slug]');
Defensive patterns

Strategy: validation

Validate before calling

function assertRouteId(id) {
  if (typeof id !== 'string' || id[0] !== '/') throw new TypeError('route ID must start with /');
  return id;
}
await preloadCode(assertRouteId('/blog/[slug]'));

Type guard

function isRouteId(s) { return typeof s === 'string' && s.startsWith('/'); }

Prevention

When it happens

Trigger: preloadCode('blog/[slug]') — omitting the leading slash; passing a URL path with real values like '/blog/hello' instead of the parameterized route ID.

Common situations: Confusing route IDs with URLs; building the argument from user-facing paths; copying route segments without the leading '/'.

Related errors


AI-assisted analysis of sveltejs/kit@03f1687fe6 (2026-09-02). Data as JSON: /api/errors/76ec944c8ce7f897. Report an issue: GitHub.