Skip to content

feat: allow resolve() to accept pathnames with query strings and hash fragments - #15458

Merged
teemingc merged 4 commits into
sveltejs:mainfrom
BridgerB:feat/resolve-accept-query-hash-types
Mar 3, 2026
Merged

teemingc merged 4 commits into
sveltejs:mainfrom
BridgerB:feat/resolve-accept-query-hash-types

Conversation

@BridgerB

Copy link
Copy Markdown
Contributor

Summary

Adds a ResolvablePath type that extends Pathname with optional ?${string} and #${string} suffixes, allowing resolve() to accept pathnames with query strings and hash fragments.

  • No runtime changes — resolve_route() already passes through ? and # correctly
  • Pathname type is unchanged (still used for page.url.pathname)
  • Existing resolve() behavior with route IDs and params is unaffected

Motivation

Currently there is no way to use goto() with query strings or hash fragments without either disabling the lint rule or bypassing resolve():

  1. resolve('/products?page=2') fails type-checking (Pathname doesn't include ?)
  2. goto(resolve('/products') + '?page=2') fails the svelte/no-navigation-without-resolve lint rule (argument isn't a resolve() call)

Per @teemingc's suggestion in #14750:

"It should be possible to add a union type with a template string such as /my-route#${string} and /my-route?${string}. This would continue to keep the function type-safe."

This is a minimal type-only fix that doesn't conflict with #14756 (which adds richer { hash, searchParams } options).

What this enables

// All of these now type-check AND satisfy the lint rule:
goto(resolve('/products?page=2'));
goto(resolve(`/search?${params}`));
goto(resolve('/docs/intro#getting-started'));

Test plan

  • pnpm check passes
  • pnpm lint passes
  • Existing tests pass (no runtime changes, only type widening)

Closes #14750

… fragments

- Add `ResolvablePath` type: `Pathname | ${Pathname}?${string} | ${Pathname}#${string}`
- Update `resolve()` and `resolveRoute()` signatures to accept `ResolvablePath`
- No runtime changes needed — `resolve_route()` already passes through `?` and `#`
- Enables `goto(resolve('/products?page=2'))` to satisfy both type checker and `svelte/no-navigation-without-resolve` lint rule

Closes sveltejs#14750
@changeset-bot

changeset-bot Bot commented Feb 28, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 977191d

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 Patch

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

@vercel vercel Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional Suggestion:

The resolve() function signature uses Pathname instead of ResolvablePath, preventing the use of query strings and hash fragments.

Fix on Vercel

- Update resolve() in types/index.d.ts (was still using Pathname)
- Update JSDoc template in client.js
Comment thread packages/kit/src/types/ambient.d.ts Resolved
Comment thread packages/kit/src/types/ambient.d.ts Resolved
Comment thread packages/kit/src/types/ambient.d.ts Resolved
Comment thread .changeset/resolve-accept-query-hash.md Resolved

@teemingc teemingc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for submitting this. I've added a few comments to help align things with our current conventions. Other than that, it looks good to me.

@teemingc teemingc linked an issue Mar 2, 2026 that may be closed by this pull request

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this, but it's missing one crucial bit. Because this only applies to pathnames, it means you can't do this:

resolve('/[slug]?foo=bar', { slug: 'banana' });

...because route parameter notation like this falls under the RouteId type. I think I'd prefer if the ?{string} and #{string} variants applied to the RouteId version as well, though... that might be challenging. want to give it a crack?

@teemingc

teemingc commented Mar 2, 2026 •

Copy link
Copy Markdown
Member

I love this, but it's missing one crucial bit. Because this only applies to pathnames, it means you can't do this:

resolve('/[slug]?foo=bar', { slug: 'banana' });

...because route parameter notation like this falls under the RouteId type. I think I'd prefer if the ?{string} and #{string} variants applied to the RouteId version as well, though... that might be challenging. want to give it a crack?

Would that also be something like:

export type RouteIdWithSearchOrHash = RouteId |
	`${RouteId}?${string}` |
	`${RouteId}#${string}`

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

Copy link
Copy Markdown
Contributor

Yeah I think so, but I'm not sure how it would affect our ability to extract the types of the route parameters for the second argument...

- Rename ResolvablePath to PathnameWithSearchOrHash
- Remove redundant ${Pathname}?${string}#${string} variant
- Add RouteIdWithSearchOrHash type so resolve('/[slug]?foo=bar', { slug: 'banana' }) works
- Update ResolveArgs to extract params from RouteId with search/hash suffix
- Shorten changeset text
@teemingc

teemingc commented Mar 3, 2026

Copy link
Copy Markdown
Member

Yeah I think so, but I'm not sure how it would affect our ability to extract the types of the route parameters for the second argument...

The contributed solution really is quite brilliant 😄 https://github.com/sveltejs/kit/pull/15458/changes#diff-ca363a5801bb1240cce13fcdf36af610cfa8d5aae2007e77244a0d91f9ce5b4dR8-R12 I've tested it along with params that have matchers and it works well!

@teemingc
teemingc merged commit 5c79eb4 into sveltejs:main Mar 3, 2026
23 of 24 checks passed
@github-actions github-actions Bot mentioned this pull request Mar 3, 2026
Copilot AI pushed a commit to Stadly/kit that referenced this pull request Mar 6, 2026
… fragments (sveltejs#15458)

## Summary

Adds a `ResolvablePath` type that extends `Pathname` with optional
`?${string}` and `#${string}` suffixes, allowing `resolve()` to accept
pathnames with query strings and hash fragments.

- No runtime changes — `resolve_route()` already passes through `?` and
`#` correctly
- `Pathname` type is unchanged (still used for `page.url.pathname`)
- Existing `resolve()` behavior with route IDs and params is unaffected

## Motivation

Currently there is no way to use `goto()` with query strings or hash
fragments without either disabling the lint rule or bypassing
`resolve()`:

1. `resolve('/products?page=2')` fails type-checking (`Pathname` doesn't
include `?`)
2. `goto(resolve('/products') + '?page=2')` fails the
`svelte/no-navigation-without-resolve` lint rule (argument isn't a
`resolve()` call)

Per @teemingc's suggestion in sveltejs#14750:
> "It should be possible to add a union type with a template string such
as `/my-route#${string}` and `/my-route?${string}`. This would continue
to keep the function type-safe."

This is a minimal type-only fix that doesn't conflict with sveltejs#14756 (which
adds richer `{ hash, searchParams }` options).

## What this enables

```js
// All of these now type-check AND satisfy the lint rule:
goto(resolve('/products?page=2'));
goto(resolve(`/search?${params}`));
goto(resolve('/docs/intro#getting-started'));
```

## Test plan

- [x] `pnpm check` passes
- [x] `pnpm lint` passes
- [x] Existing tests pass (no runtime changes, only type widening)

Closes sveltejs#14750

---------

Co-authored-by: Tee Ming <chewteeming01@gmail.com>
elliott-with-the-longest-name-on-github pushed a commit that referenced this pull request Mar 11, 2026
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @sveltejs/kit@2.54.0

### Minor Changes

- feat: allow error boundaries to catch errors on the server
([#15308](#15308))

### Patch Changes

- chore: upgrade `devalue`
([#15535](#15535))


- fix: don't wait for remote functions that are not awaited in the
template ([#15280](#15280))


- feat: allow `resolve()` to accept pathnames with a search string
and/or hash ([#15458](#15458))


- chore: remove deprecation warnings for `config.kit.files.*` options
when validating the Svelte config file
([#15482](#15482))


- fix: handles form target attribute in remote form redirects
([#15457](#15457))

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
dlrobson added a commit to dlrobson/dlrobson.github.io that referenced this pull request Mar 22, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [@sveltejs/kit](https://svelte.dev) ([source](https://github.com/sveltejs/kit/tree/HEAD/packages/kit)) | [`2.53.4` → `2.55.0`](https://renovatebot.com/diffs/npm/@sveltejs%2fkit/2.53.4/2.55.0) | ![age](https://developer.mend.io/api/mc/badges/age/npm/@sveltejs%2fkit/2.55.0?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@sveltejs%2fkit/2.53.4/2.55.0?slim=true) |

---

### Release Notes

<details>
<summary>sveltejs/kit (@&#8203;sveltejs/kit)</summary>

### [`v2.55.0`](https://github.com/sveltejs/kit/blob/HEAD/packages/kit/CHANGELOG.md#2550)

[Compare Source](https://github.com/sveltejs/kit/compare/@sveltejs/kit@2.54.0...@sveltejs/kit@2.55.0)

##### Minor Changes

- feat: page and layout params with matchers are now type narrowed in `$app/types`, leading to better type safety when working with params in `$app/types`, `$app/state`, and hooks. ([#&#8203;15502](sveltejs/kit#15502))

### [`v2.54.0`](https://github.com/sveltejs/kit/blob/HEAD/packages/kit/CHANGELOG.md#2540)

[Compare Source](https://github.com/sveltejs/kit/compare/@sveltejs/kit@2.53.4...@sveltejs/kit@2.54.0)

##### Minor Changes

- feat: allow error boundaries to catch errors on the server ([#&#8203;15308](sveltejs/kit#15308))

##### Patch Changes

- chore: upgrade `devalue` ([#&#8203;15535](sveltejs/kit#15535))

- fix: don't wait for remote functions that are not awaited in the template ([#&#8203;15280](sveltejs/kit#15280))

- feat: allow `resolve()` to accept pathnames with a search string and/or hash ([#&#8203;15458](sveltejs/kit#15458))

- chore: remove deprecation warnings for `config.kit.files.*` options when validating the Svelte config file ([#&#8203;15482](sveltejs/kit#15482))

- fix: handles form target attribute in remote form redirects ([#&#8203;15457](sveltejs/kit#15457))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about these updates again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My44NC4yIiwidXBkYXRlZEluVmVyIjoiNDMuODQuMiIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

Reviewed-on: https://forgejo.tail5a98.ts.net/gooilers123/monolith/pulls/45
teemingc added a commit that referenced this pull request Nov 18, 2026
…17808)

Fixes #17805

Since [#15458](#15458), `resolve()`
accepts route IDs followed by a query string or fragment. That type-only
change assumed the runtime already passed suffixes through correctly,
but `resolve_route` treated the entire input as route segments. A final
`(group)` therefore stopped matching the group predicate and remained in
the destination, producing a 404. Other suffix contents could also
undergo segment filtering, parameter replacement or escape expansion.

Separate the suffix at the first literal `?` or `#` in the shared
resolver, resolve only the route portion, and append the suffix
unchanged. Move the existing missing-params guard and parameter
defaulting there as well, so brackets inside query strings or fragments
do not trigger route-parameter diagnostics. Diagnostics still retain the
original input ID.

This preserves the group-free segment contract used by route parsing and
type generation from
[#14050](#14050), and the
single-pass parameter/escape expansion from
[#16570](#16570). Explicit
trailing-slash preservation from
[#14294](#14294) now inspects only
the route portion, with normalized roots retaining a single slash.
Widening the group regex instead would discard the suffix along with the
group and leave the other suffix transformations unresolved.

Both client and server resolution use this boundary without changing
base-path, request-relative or hash-router prefixing. Literal pathname
inputs still bypass route-ID processing; build-time callers continue
supplying their parameter objects.

- [Minimal SvelteLab
reproduction](https://www.sveltelab.dev/#code=N4IgzgTgxiBcoBMCWECmUAuB7CBPOoAhgA7EB0AFhgLYA2BIAZkrag1FgHYardhwgAPAEIEWTLmKoABFToA%2BADqdBc2tNqFOAcwC8ikLwNLOijKtSEEJs2cHVUGQtKgVCEMI-0gArhkYAtAAcBtIA9DYYdg5O0pyEDt4AbkioAO7EOFEgLlw83N5pSAgYFLoIqClQqAFFJRQANNJInEgYSIS0AWBQnai6AIyhEcq25jHO8YkGPAAeGN29rKEc3LwY3j19w5FmAKRgSai0PADWbZSWCHuj5mEUV5GCAEZYCLjSCIRO3UcnqOcFsQ0LQsFYAl8nN4KFgjhBjLc7MgktIwBhcKxvMgwMRNLhYLk1nxjAc-mcLq93ntBGFkU8wpTcCYaWoTCAAL7shogEjkBBkDD8eBMFhsYWrfKCgRhMLSADKqBkVAwxDAsBlh2OPDIFSStPEYDCgLC6KkYAAxLydQKwMoZdJGDhmpxHRBqN8kFxpIRXn5pKVUJ5nTwIIxCNVbZwKlBNGhpNpQc9OtJgLcpoHiOGZABBUgpxEYe0tENh6rSACiEAgTuA7ILRbWoaz0gAMuJOmAU3XTFFC7Li6gm2WAAqEbSoAAi32ctfr-cbpZko-HcqcPC7c%2BDg8X0mHmn8OGo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AABZQHyVPA%2BDGqdX5kAzlnIUBMNbVy1mhHihFvRfGwoOCS9FKH8MwdgrxEA8EENkq0Xi15%2BLoMBOJRgtFpB0T8f8ahbQwjXzCW4vixEKjME4KgAAwjbTOsT4l0XSZxc8l4%2BI5PwScT4qBckFKKdoX8yDpBcJ4Z7AkcdoltFaR4nBg4CSBJDJZDyedODsk8mraWQA)

<!--
svelte-triage-bot:feedback-baseline:d243d66890c122c246950c28f26c1dc952615c26
-->

---------

Co-authored-by: svelte-triage-bot <team@svelte.com>
Co-authored-by: Tee Ming <chewteeming01@gmail.com>
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.

Add optional hash and searchParams arguments to resolve() Typescript error when resolve has a query string parameter

3 participants