Repository navigation
feat: allow resolve() to accept pathnames with query strings and hash fragments - #15458
Conversation
… 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 detectedLatest commit: 977191d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
- Update resolve() in types/index.d.ts (was still using Pathname) - Update JSDoc template in client.js
teemingc
left a comment
There was a problem hiding this comment.
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.
elliott-with-the-longest-name-on-github
left a comment
There was a problem hiding this comment.
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}` |
|
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
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! |
… 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 <[email protected]>
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/[email protected] ### 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>
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) |  |  | --- ### Release Notes <details> <summary>sveltejs/kit (@​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/[email protected]...@sveltejs/[email protected]) ##### 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. ([#​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/[email protected]...@sveltejs/[email protected]) ##### Minor Changes - feat: allow error boundaries to catch errors on the server ([#​15308](sveltejs/kit#15308)) ##### Patch Changes - chore: upgrade `devalue` ([#​15535](sveltejs/kit#15535)) - fix: don't wait for remote functions that are not awaited in the template ([#​15280](sveltejs/kit#15280)) - feat: allow `resolve()` to accept pathnames with a search string and/or hash ([#​15458](sveltejs/kit#15458)) - chore: remove deprecation warnings for `config.kit.files.*` options when validating the Svelte config file ([#​15482](sveltejs/kit#15482)) - fix: handles form target attribute in remote form redirects ([#​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
…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=N4IgzgTgxiBcoBMCWECmUA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g64MYbVkXNFULTV9WNdtLG4dIF1XSAN3rAEbgTc9a3zd1AFLVgxADTSxC7WEB3do%2BnLchBeIJbBZJiqAiH40DKF8AIgg%2Bdhf2sERLgUCwCBoFx1EaQxwJw2Almcd5UCYZTcnAAAAozFQQLT9OM5ZaOctLT6CqszDaGQABWZGcAhorIUSUqwCAqY9gYF75AgtpwNIBhMaQJo9FwCsGA0iLEwRooQAA8thnqcCbBJ62MBhophmAGASzmubcXIOy0MY%2BBUQfSAA2n70B22bIApNq8tIIrgoGAAusoUtPpmUCnGOqDK6r6tIeKJPa7ryh4QY6axwYgIBAMADsQQAAwAKx2-XKdwmAHvNyAXdkOPQwgPb8kGMCSBJDpsch6gM8NyApqoKPykID4yzTwPfs875Xv5vJ6%2B6qPacyJfB-nynzw%2BPTV9tDIj-P3feHr8ClSpGkL-rh-ikdIBgB7h1niAXUE5UBSCjLwKAqRT56y-inAW0lUAqzCFYEgIYAiED8FgUeQQJ4T37vfAwaC8aYMBKPAAzGQAYpDP5fwoegzB18Ai4h8NoFovwtRb1NgYduZB6FTzXiwa8AeNR49zID3YRAAmMhKCZiSEDMfbCo8ABsJDaFKIkdfIhIiyAABZQHyVPA%2BDGqdX5kAzlnIUBMNbVy1mhHihFvRfGwoOCS9FKH8MwdgrxEA8EENkq0Xi15%2BLoMBOJRgtFpB0T8f8ahbQwjXzCW4vixEKjME4KgAAwjbTOsT4l0XSZxc8l4%2BI5PwScT4qBckFKKdoX8yDpBcJ4Z7AkcdoltFaR4nBg4CSBJDJZDyedODsk8mraWQA) <!-- svelte-triage-bot:feedback-baseline:d243d66890c122c246950c28f26c1dc952615c26 --> --------- Co-authored-by: svelte-triage-bot <[email protected]> Co-authored-by: Tee Ming <[email protected]>
Summary
Adds a
ResolvablePathtype that extendsPathnamewith optional?${string}and#${string}suffixes, allowingresolve()to accept pathnames with query strings and hash fragments.resolve_route()already passes through?and#correctlyPathnametype is unchanged (still used forpage.url.pathname)resolve()behavior with route IDs and params is unaffectedMotivation
Currently there is no way to use
goto()with query strings or hash fragments without either disabling the lint rule or bypassingresolve():resolve('/products?page=2')fails type-checking (Pathnamedoesn't include?)goto(resolve('/products') + '?page=2')fails thesvelte/no-navigation-without-resolvelint rule (argument isn't aresolve()call)Per @teemingc's suggestion in #14750:
This is a minimal type-only fix that doesn't conflict with #14756 (which adds richer
{ hash, searchParams }options).What this enables
Test plan
pnpm checkpassespnpm lintpassesCloses #14750