diff --git a/.changeset/curvy-bugs-build.md b/.changeset/curvy-bugs-build.md new file mode 100644 index 000000000000..4fb178ce47cc --- /dev/null +++ b/.changeset/curvy-bugs-build.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: add trailing slash pathname when generating typed routes diff --git a/packages/kit/src/core/sync/write_types/index.js b/packages/kit/src/core/sync/write_types/index.js index d51c60a94442..ba3f88d34056 100644 --- a/packages/kit/src/core/sync/write_types/index.js +++ b/packages/kit/src/core/sync/write_types/index.js @@ -82,10 +82,21 @@ export function write_all_types(config, manifest_data) { dynamic_routes.push(route_type); const pathname = remove_group_segments(route.id); - pathnames.add(`\`${replace_required_params(replace_optional_params(pathname))}\` & {}`); + const replaced_pathname = replace_required_params(replace_optional_params(pathname)); + pathnames.add(`\`${replaced_pathname}\` & {}`); + + if (pathname !== '/') { + // Support trailing slash + pathnames.add(`\`${replaced_pathname + '/'}\` & {}`); + } } else { const pathname = remove_group_segments(route.id); pathnames.add(s(pathname)); + + if (pathname !== '/') { + // Support trailing slash + pathnames.add(s(pathname + '/')); + } } /** @type {Map} */ diff --git a/packages/kit/src/core/sync/write_types/test/app-types/+page.ts b/packages/kit/src/core/sync/write_types/test/app-types/+page.ts index 09dea2ca9d96..7da8f0ee9b6a 100644 --- a/packages/kit/src/core/sync/write_types/test/app-types/+page.ts +++ b/packages/kit/src/core/sync/write_types/test/app-types/+page.ts @@ -26,9 +26,12 @@ declare let pathname: Pathname; pathname = '/nope'; pathname = '/foo'; pathname = '/foo/1/2'; +pathname = '/foo/'; +pathname = '/foo/1/2/'; // Test layout groups pathname = '/path-a'; +pathname = '/path-a/'; // @ts-expect-error layout group names are NOT part of the pathname type pathname = '/(group)/path-a';