Skip to content

breaking: remove leading / from AssetPath and Path - #16430

Merged
Rich-Harris merged 18 commits into
version-3from
slashless-assets-and-paths
Jul 20, 2026
Merged

Rich-Harris merged 18 commits into
version-3from
slashless-assets-and-paths

Conversation

@Rich-Harris

@Rich-Harris Rich-Harris commented Jul 19, 2026 •

Copy link
Copy Markdown
Member

While working on #16372 I had a realisation that we shouldn't prefix Asset and Pathname strings with a /. It makes no sense — these are intended to be relative to the base path, and so having them be root-relative (/favicon.png instead of favicon.png) is wrong.

This change means Pathname is the wrong name, since it wrongly implies a root-relative url.pathname. As well as removing the leading slash this PR renames Pathname to Path, and Asset to AssetPath. (This allows future us to add a new Asset type containing path: AssetPath, and possibly other stuff like type and size.)

There are practical benefits to removing the leading slash:

  • In feat: add $app/manifest module #16372, we're adding an $app/manifest module with immutable and assets exports. The latter is an array of objects with a path property — rather than typing it as string, we can type it as AssetPath. But if it had a leading slash then it would be unsuitable for its main purpose, which is for use with cache.add(path) inside a service worker. We can either make it base-path-relative (as this PR proposes) or root-relative (i.e. /my-basepath/favicon.png) which is inherently less portable.
  • Similarly, we can type the path elements in the prerendered array as Path rather than string
  • We can more easily distinguish between resolve(path) and resolve(route_id), both type-wise and at runtime. At the moment they're sort of smushed together awkwardly

I also think that asset('favicon.png') is just nicer to look at than asset('/favicon.png') which is either weird or pointless depending on whether there's a configured base.

Anyway: this is all somewhat independent of the $app/manifest stuff so I figured it deserved its own PR.

Closes #16425, by tightening up the type of AssetPath (no string & {}).


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

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.

@pkg-svelte-dev

ghost commented Jul 19, 2026 •

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from 9956b0a:

pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/9956b0a3f9a410f71f28ed94e6dd9deff8a51cb4

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

@changeset-bot

ghost commented Jul 19, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9956b0a

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

ghost commented Jul 19, 2026

Copy link
Copy Markdown

Rich Harris and others added 3 commits July 19, 2026 17:51
…lash-prefixed pathnames because `resolve_route` is always called even for relative (non route-ID) inputs.

This commit fixes the issue reported at packages/kit/src/runtime/app/paths/server.js:24

## Bug

In `packages/kit/src/runtime/app/paths/server.js`, `resolve(id, params)` unconditionally calls `resolve_route(id, params ?? {})`, regardless of whether `id` starts with `/`.

`resolve_route` → `get_route_segments` does `route.slice(1)`:

```js
export function get_route_segments(route) {
	return route.slice(1).split('/').filter(affects_path);
}
```

That `slice(1)` assumes the input is a route ID with a leading slash. When a plain pathname without a leading `/` is passed, the first character is silently dropped:

*   `resolve_route('favicon.png', {})` → `/avicon.png`
*   `resolve_route('about/us', {})` → `/bout/us`
*   `resolve_route('/about', {})` → `/about` (correct)

Because this PR makes `AssetPath`/`Path` relative (no leading `/`), `resolve` now regularly receives non-slash inputs. This is especially impactful for `asset()`: in the default config `assets === base === ''`, so `asset('favicon.png')` calls `resolve('favicon.png')` and returns the corrupted `/avicon.png`.

The client implementation (`client.js`) is correct — it only calls `resolve_route` for `/`-prefixed route IDs and otherwise returns `base + pathname_prefix + '/' + args[0]`. The server version was missing the equivalent branch.

## Fix

Mirror the client's branching in the server `resolve`:

```js
let resolved;

if (id[0] === '/') {
	if (id.includes('[') && !params) {
		throw new Error(`Missing params for dynamic route ID  
```

The existing `relative`/`base` handling continues to operate on `resolved`, so no other behavior changes. Now `asset('favicon.png')` resolves to `/favicon.png` (or the appropriate relative/base-prefixed form) instead of `/avicon.png`.


Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Co-authored-by: Rich-Harris <[email protected]>
… the resolved URL for static route IDs with no params, diverging from the client.

This commit fixes the issue reported at packages/kit/src/runtime/app/paths/server.js:34

## Bug

In `packages/kit/src/runtime/app/paths/server.js`, the `/`-prefixed branch was changed to:

```js
resolved = params ? resolve_route(id, params) : id;
```

When `params` is falsy (i.e. a static route ID passed with no params), this skips `resolve_route` entirely and uses the raw route `id`. But `resolve_route` → `get_route_segments` → `affects_path` also strips layout **group** segments matching `/^([^)]+)$/` (e.g. `(app)`, `(marketing)`), not just dynamic params.

### Concrete triggers
- `resolve('/(app)/dashboard')` returns `/(app)/dashboard` (literal parens in the URL) instead of `/dashboard`.
- `resolve('/(marketing)')` returns `/(marketing)` instead of `/`.

`resolve_route('/(app)/dashboard', {})` correctly returns `/dashboard`, confirming the divergence.

### Client/server parity
The client implementation (`client.js`) always calls `resolve_route(args[0], args[1] ?? {})` for `/`-prefixed inputs, so it correctly strips groups. The server change caused a mismatch: any static route inside a layout group would render broken links on the server.

## Fix

Restore unconditional group-stripping by always calling `resolve_route`, matching the client:

```js
resolved = resolve_route(id, params ?? {});
```

This handles both static routes with group segments and dynamic routes, and restores client/server parity.

Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Co-authored-by: Rich-Harris <[email protected]>
@Rich-Harris Rich-Harris mentioned this pull request Jul 20, 2026
6 tasks done
@teemingc

ghost commented Jul 20, 2026 •

Copy link
Copy Markdown
Member

Closes #16430, by tightening up the type of AssetPath (no string & {}).

This PR is 16430. Did you mean another issue?

Also, the test type check is erroring because we can no longer do the asset('') hack to get the asset base path

@Rich-Harris

ghost commented Jul 20, 2026

Copy link
Copy Markdown
Member Author

Yep, I meant #16425. The check failure is annoying as I'd got rid of the red squigglies locally with /** @type {any} */ '', but apparently we need to be more invasive

Comment thread packages/kit/src/types/ambient.d.ts Resolved
Comment thread packages/kit/types/index.d.ts Resolved
@nickolay

ghost commented Aug 20, 2026 •

Copy link
Copy Markdown

This change means Pathname is the wrong name, since it wrongly implies a root-relative url.pathname

Yet, resolve() still takes a PathnameWithSearchOrHash, makes multiple references to the pathname in its documentation, and conflicts with the standard definition of resolving relative references on the web.

It is also very weird that if you resolve a Path (without params, e.g. resolve('blog/hello-world');) you omit the leading slash, but if you resolve a RouteId (resolve('/blog/[slug]', {..}) you suddenly need it. Why doesn't the argument for "having them be root-relative (/favicon.png instead of favicon.png) [being] wrong" apply to the second form? (I know the technical answer now, but consider the user's perspective!)

Even worse, for simple routes you can use both, yet the migrator removes the leading / leading to unnecessary churn. For some reason it keeps resolve("/") alone though (update: sveltejs/cli#1268), which fails the type check when you have the root page in routes/(area)/+page.svelte. Trying to read the docs to figure what the heck is going on does not help at all: "Resolve a pathname by prefixing it with the base path, if any, or resolve a route ID by populating dynamic segments with parameters. During server rendering, the base path is relative and depends on the page currently being rendered" (I've highlighted the terms that make zero sense to someone who haven't already read the code.)

I also think that asset('favicon.png') is just nicer to look at than asset('/favicon.png')

Maybe, but what about <a href={resolve('')}> ? What about resolve('posts')?

The SK2 version might have been confusing to those who used the advanced functionality. The SK3 version, while technically superior, is confusing to those who don't know about base URL.

In case you're wondering why am I using resolve() at all — it's because eslint default set includes no-navigation-without-resolve. (That page needs an update explaining how SK2 and 3 are different in what they require the user to do...)


Anyway, what's the rush with requiring everyone to remove SK2-style pathnames from their resolve() calls? This PR is 1 month old, there have been no deprecations in SK2, and there's no way to prepare SK2 code so that the 2->3 migration doesn't have to change it all at once, which makes it harder to deal with branches — all for unclear gain.

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

ghost commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

@nickolay if you want to discuss this please open an issue! Merged PRs are not a good place to talk 😄

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.

improve the typing of static assets import

4 participants