Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/resolve-accept-query-hash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

feat: allow `resolve()` to accept pathnames with a search string and/or hash
4 changes: 2 additions & 2 deletions packages/kit/src/runtime/app/paths/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @import { Asset, RouteId, Pathname, ResolvedPathname } from '$app/types' */
/** @import { Asset, RouteId, RouteIdWithSearchOrHash, Pathname, PathnameWithSearchOrHash, ResolvedPathname } from '$app/types' */
/** @import { ResolveArgs } from './types.js' */
import { base, assets, hash_routing } from './internal/client.js';
import { resolve_route } from '../../../utils/routing.js';
Expand Down Expand Up @@ -47,7 +47,7 @@ const pathname_prefix = hash_routing ? '#' : '';
* ```
* @since 2.26
*
* @template {RouteId | Pathname} T
* @template {RouteIdWithSearchOrHash | PathnameWithSearchOrHash} T
* @param {ResolveArgs<T>} args
* @returns {ResolvedPathname}
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/runtime/app/paths/public.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RouteId, Pathname, ResolvedPathname } from '$app/types';
import { RouteIdWithSearchOrHash, PathnameWithSearchOrHash, ResolvedPathname } from '$app/types';
import { ResolveArgs } from './types.js';

export { resolve, asset, match } from './client.js';
Expand All @@ -24,6 +24,6 @@ export let assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit
/**
* @deprecated Use [`resolve(...)`](https://svelte.dev/docs/kit/$app-paths#resolve) instead
*/
export function resolveRoute<T extends RouteId | Pathname>(
export function resolveRoute<T extends RouteIdWithSearchOrHash | PathnameWithSearchOrHash>(
...args: ResolveArgs<T>
): ResolvedPathname;
28 changes: 22 additions & 6 deletions packages/kit/src/runtime/app/paths/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
import { Pathname, RouteId, RouteParams } from '$app/types';
import {
PathnameWithSearchOrHash,
RouteId,
RouteIdWithSearchOrHash,
RouteParams
} from '$app/types';

export type ResolveArgs<T extends RouteId | Pathname> = T extends RouteId
? RouteParams<T> extends Record<string, never>
? [route: T]
: [route: T, params: RouteParams<T>]
: [route: T];
type StripSearchOrHash<T extends string> = T extends `${infer Pathname}?${string}`
? Pathname
: T extends `${infer Pathname}#${string}`
? Pathname
: T;

export type ResolveArgs<T extends RouteIdWithSearchOrHash | PathnameWithSearchOrHash> =
T extends RouteId
? RouteParams<T> extends Record<string, never>
? [route: T]
: [route: T, params: RouteParams<T>]
: StripSearchOrHash<T> extends infer U extends RouteId
? RouteParams<U> extends Record<string, never>
? [route: T]
: [route: T, params: RouteParams<U>]
: [route: T];
13 changes: 13 additions & 0 deletions packages/kit/src/types/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ declare module '$app/types' {
*/
export type RouteId = ReturnType<AppTypes['RouteId']>;

/**
* `RouteId`, but possibly suffixed with a search string and/or hash.
*/
export type RouteIdWithSearchOrHash = RouteId | `${RouteId}?${string}` | `${RouteId}#${string}`;

/**
* A utility for getting the parameters associated with a given route.
*/
Expand All @@ -123,6 +128,14 @@ declare module '$app/types' {
*/
export type Pathname = ReturnType<AppTypes['Pathname']>;

/**
* `Pathname`, but possibly suffixed with a search string and/or hash.
*/
export type PathnameWithSearchOrHash =
| Pathname
| `${Pathname}?${string}`
| `${Pathname}#${string}`;

/**
* `Pathname`, but possibly prefixed with a base path. Used for `page.url.pathname`.
*/
Expand Down
40 changes: 32 additions & 8 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3124,7 +3124,7 @@ declare module '$app/navigation' {
}

declare module '$app/paths' {
import type { RouteId, Pathname, ResolvedPathname, RouteParams, Asset } from '$app/types';
import type { RouteIdWithSearchOrHash, PathnameWithSearchOrHash, ResolvedPathname, RouteId, RouteParams, Asset, Pathname } from '$app/types';
/**
* A string that matches [`config.kit.paths.base`](https://svelte.dev/docs/kit/configuration#paths).
*
Expand All @@ -3146,14 +3146,25 @@ declare module '$app/paths' {
/**
* @deprecated Use [`resolve(...)`](https://svelte.dev/docs/kit/$app-paths#resolve) instead
*/
export function resolveRoute<T extends RouteId | Pathname>(
export function resolveRoute<T extends RouteIdWithSearchOrHash | PathnameWithSearchOrHash>(
...args: ResolveArgs<T>
): ResolvedPathname;
type ResolveArgs<T extends RouteId | Pathname> = T extends RouteId
? RouteParams<T> extends Record<string, never>
? [route: T]
: [route: T, params: RouteParams<T>]
: [route: T];
type StripSearchOrHash<T extends string> = T extends `${infer Base}?${string}`
? Base
: T extends `${infer Base}#${string}`
? Base
: T;

type ResolveArgs<T extends RouteIdWithSearchOrHash | PathnameWithSearchOrHash> =
T extends RouteId
? RouteParams<T> extends Record<string, never>
? [route: T]
: [route: T, params: RouteParams<T>]
: StripSearchOrHash<T> extends infer U extends RouteId
? RouteParams<U> extends Record<string, never>
? [route: T]
: [route: T, params: RouteParams<U>]
: [route: T];
/**
* Resolve the URL of an asset in your `static` directory, by prefixing it with [`config.kit.paths.assets`](https://svelte.dev/docs/kit/configuration#paths) if configured, or otherwise by prefixing it with the base path.
*
Expand Down Expand Up @@ -3191,7 +3202,7 @@ declare module '$app/paths' {
* @since 2.26
*
* */
export function resolve<T extends RouteId | Pathname>(...args: ResolveArgs<T>): ResolvedPathname;
export function resolve<T extends RouteIdWithSearchOrHash | PathnameWithSearchOrHash>(...args: ResolveArgs<T>): ResolvedPathname;
/**
* Match a path or URL to a route ID and extracts any parameters.
*
Expand Down Expand Up @@ -3577,6 +3588,11 @@ declare module '$app/types' {
*/
export type RouteId = ReturnType<AppTypes['RouteId']>;

/**
* `RouteId`, but possibly suffixed with a search string and/or hash.
*/
export type RouteIdWithSearchOrHash = RouteId | `${RouteId}?${string}` | `${RouteId}#${string}`;

/**
* A utility for getting the parameters associated with a given route.
*/
Expand All @@ -3596,6 +3612,14 @@ declare module '$app/types' {
*/
export type Pathname = ReturnType<AppTypes['Pathname']>;

/**
* `Pathname`, but possibly suffixed with a search string and/or hash.
*/
export type PathnameWithSearchOrHash =
| Pathname
| `${Pathname}?${string}`
| `${Pathname}#${string}`;

/**
* `Pathname`, but possibly prefixed with a base path. Used for `page.url.pathname`.
*/
Expand Down
Loading