Skip to content

fix: removes group segment from route id to allow correct resolution of urls #14064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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/ninety-bears-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: allow the usage of RouteId without the group segment with `resolve`
16 changes: 11 additions & 5 deletions packages/kit/src/core/sync/write_types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export function write_all_types(config, manifest_data) {
/** @type {Set<string>} */
const pathnames = new Set();

/** @type {string[]} */
const dynamic_routes = [];
/** @type {Set<string>} */
const dynamic_routes = new Set();

/** @type {string[]} */
const layouts = [];
Expand All @@ -79,10 +79,16 @@ export function write_all_types(config, manifest_data) {
const params = route.params.map((p) => `${p.name}${p.optional ? '?:' : ':'} string`);
const route_type = `${s(route.id)}: { ${params.join('; ')} }`;

dynamic_routes.push(route_type);
dynamic_routes.add(route_type);

const pathname = remove_group_segments(route.id);
pathnames.add(`\`${replace_required_params(replace_optional_params(pathname))}\` & {}`);

if (pathname !== route.id) {
const route_type = `${s(pathname)}: { ${params.join('; ')} }`;

dynamic_routes.add(route_type);
}
} else {
const pathname = remove_group_segments(route.id);
pathnames.add(s(pathname));
Expand Down Expand Up @@ -114,11 +120,11 @@ export function write_all_types(config, manifest_data) {
fs.writeFileSync(
`${types_dir}/index.d.ts`,
[
`type DynamicRoutes = {\n\t${dynamic_routes.join(';\n\t')}\n};`,
`type DynamicRoutes = {\n\t${Array.from(dynamic_routes).join(';\n\t')}\n};`,
`type Layouts = {\n\t${layouts.join(';\n\t')}\n};`,
// we enumerate these rather than doing `keyof Routes` so that the list is visible on hover
`export type RouteId = ${manifest_data.routes.map((r) => s(r.id)).join(' | ')};`,
'export type RouteParams<T extends RouteId> = T extends keyof DynamicRoutes ? DynamicRoutes[T] : Record<string, never>;',
'export type RouteParams<T extends RouteId | keyof DynamicRoutes> = T extends keyof DynamicRoutes ? DynamicRoutes[T] : Record<string, never>;',
'export type LayoutParams<T extends RouteId> = Layouts[T] | Record<string, never>;',
`export type Pathname = ${Array.from(pathnames).join(' | ')};`,
'export type ResolvedPathname = `${"" | `/${string}`}${Pathname}`;',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ pathname = '/(group)/path-a';

// read `pathname` otherwise it is treated as unused
pathname;

declare let subpath_with_group: RouteParams<'/path-b/[id]/path-c'>;

subpath_with_group.id; // okay
10 changes: 7 additions & 3 deletions packages/kit/src/runtime/app/paths/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-ignore
import { Asset, RouteId, RouteParams, Pathname, ResolvedPathname } from '$app/types';
import { Asset, DynamicRoutes, RouteId, RouteParams, Pathname, ResolvedPathname } from '$app/types';

/**
* A string that matches [`config.kit.paths.base`](https://svelte.dev/docs/kit/configuration#paths).
Expand All @@ -19,7 +19,9 @@ export let base: '' | `/${string}`;
*/
export let assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';

type ResolveArgs<T extends RouteId | Pathname> = T extends RouteId
type ResolveArgs<T extends RouteId | Pathname | keyof DynamicRoutes> = T extends
| RouteId
| keyof DynamicRoutes
? RouteParams<T> extends Record<string, never>
? [route: T]
: [route: T, params: RouteParams<T>]
Expand All @@ -44,7 +46,9 @@ type ResolveArgs<T extends RouteId | Pathname> = T extends RouteId
* ```
* @since 2.26
*/
export function resolve<T extends RouteId | Pathname>(...args: ResolveArgs<T>): ResolvedPathname;
export function resolve<T extends RouteId | Pathname | keyof DynamicRoutes>(
...args: ResolveArgs<T>
): ResolvedPathname;

/**
* 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
10 changes: 7 additions & 3 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2360,7 +2360,7 @@ declare module '$app/navigation' {
}

declare module '$app/paths' {
import type { Asset, RouteId, RouteParams, Pathname, ResolvedPathname } from '$app/types';
import type { Asset, DynamicRoutes, RouteId, RouteParams, Pathname, ResolvedPathname } from '$app/types';
/**
* A string that matches [`config.kit.paths.base`](https://svelte.dev/docs/kit/configuration#paths).
*
Expand All @@ -2379,7 +2379,9 @@ declare module '$app/paths' {
*/
export let assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';

type ResolveArgs<T extends RouteId | Pathname> = T extends RouteId
type ResolveArgs<T extends RouteId | Pathname | keyof DynamicRoutes> = T extends
| RouteId
| keyof DynamicRoutes
? RouteParams<T> extends Record<string, never>
? [route: T]
: [route: T, params: RouteParams<T>]
Expand All @@ -2404,7 +2406,9 @@ declare module '$app/paths' {
* ```
* @since 2.26
*/
export function resolve<T extends RouteId | Pathname>(...args: ResolveArgs<T>): ResolvedPathname;
export function resolve<T extends RouteId | Pathname | keyof DynamicRoutes>(
...args: ResolveArgs<T>
): ResolvedPathname;

/**
* 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
Loading