Skip to content

fix: add support for trailing slashes in typed routes - #14065

Merged
Rich-Harris merged 3 commits into
sveltejs:mainfrom
yuki0418:fix/trailing-slashes-typed-routes
Aug 20, 2025
Merged

Rich-Harris merged 3 commits into
sveltejs:mainfrom
yuki0418:fix/trailing-slashes-typed-routes

Conversation

@yuki0418

Copy link
Copy Markdown
Contributor

Fixes #14047

Add pathnames with a trailing slash when generating typed routes.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

Copilot AI review requested due to automatic review settings July 27, 2025 13:48
@changeset-bot

changeset-bot Bot commented Jul 27, 2025 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: dc85fde

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for trailing slashes in typed routes to fix issue #14047. The change ensures that both versions of pathnames (with and without trailing slashes) are included in the generated type definitions.

Key changes:

  • Modified pathname generation logic to include trailing slash variants for all non-root routes
  • Added test cases to verify trailing slash support in typed routes

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
packages/kit/src/core/sync/write_types/index.js Core logic update to generate both trailing and non-trailing slash pathname types
packages/kit/src/core/sync/write_types/test/app-types/+page.ts Test cases added to verify trailing slash pathname support
.changeset/curvy-bugs-build.md Changeset documentation for the patch fix

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

if (pathname !== '/') {

@yuki0418 yuki0418 Jul 27, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I want to add trailing slash only when export const trailingSlash = 'always'; is in a file.
But I couldn't find a way to check it.


if (pathname !== '/') {
// Support trailing slash
pathnames.add(`\`${replaced_pathname + '/'}\` & {}`);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Also I just add a trailing slash to type Pathname because user would not enter Router Id in href.
Sorry if I'm wrong.

@Rich-Harris

Copy link
Copy Markdown
Member

I wonder if we should try and statically analyse the trailingSlash option where possible (@eltigerchino laid the groundwork in #13684) so that we don't add a pathname with a trailing slash if we know that it doesn't need one (and vice versa one day, though it would be a breaking change to do that now)

@teemingc

teemingc commented Aug 8, 2025 •

Copy link
Copy Markdown
Member

I wonder if we should try and statically analyse the trailingSlash option where possible (@eltigerchino laid the groundwork in #13684) so that we don't add a pathname with a trailing slash if we know that it doesn't need one (and vice versa one day, though it would be a breaking change to do that now)

It might make the type generation process longer if we needed to analyse every node to get those page options for all pages. Right now it's not noticeable because we do it lazily in development and only for one route when the user accesses it. It's probably not the same for type generation

@svelte-docs-bot

Copy link
Copy Markdown

@Rich-Harris

Copy link
Copy Markdown
Member

It might make the type generation process longer

Fair enough, I'll file this under nice-to-have rather than letting it continue to block this PR

@Rich-Harris Rich-Harris 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!

@Rich-Harris
Rich-Harris merged commit d8f9198 into sveltejs:main Aug 20, 2025
@github-actions github-actions Bot mentioned this pull request Aug 20, 2025
@yuki0418

Copy link
Copy Markdown
Contributor Author

Sorry I couldn't find a better solution, I will continue to find it (I study about a compiler nowadays).
Anyway thank you for merging.

@charbelnicolas

charbelnicolas commented Aug 22, 2025 •

Copy link
Copy Markdown

Will this work for plain javascript? I'm still having this issue (#14047). I even had to update the derived to check if the pathname has an ending slash for the base href.

import { page } from '$app/stores';

let { href, icon, size, label } = $props();

let hrefWithSlash = $derived(

	href.endsWith('/') ? href : `${href}/`

);

$inspect($page.url.pathname, hrefWithSlash);

Console

init /store/dashboard/orders/ /store/
link.svelte:11 init /store/dashboard/orders/ /store/dashboard/
link.svelte:11 init /store/dashboard/orders/ /store/dashboard/orders/
link.svelte:11 init /store/dashboard/orders/ /store/dashboard/tasks/
link.svelte:11 init /store/dashboard/orders/ /store/dashboard/products/
link.svelte:11 init /store/dashboard/orders/ /store/dashboard/users/
link.svelte:11 init /store/dashboard/orders/ /store/dashboard/settings/
import { page } from '$app/stores';

let { href, icon, size, label } = $props();

let hrefWithSlash = $derived(

	`${href}/`

);

$inspect($page.url.pathname, hrefWithSlash);

Console

update /store/ /store//
utils.js:236 update /store/ /store/dashboard/
utils.js:236 update /store/ /store/dashboard/orders/
utils.js:236 update /store/ /store/dashboard/tasks/
utils.js:236 update /store/ /store/dashboard/products/
utils.js:236 update /store/ /store/dashboard/users/
utils.js:236 update /store/ /store/dashboard/settings/

Now I get /store//

This is my svelte.config.js

import adapter from '@sveltejs/adapter-node';

/** @type {import('@sveltejs/kit').Config} */
const config = {
	kit: {
		paths: {
			base: '/store',
			relative: false
		},
		adapter: adapter({
			precompress: true
		})
	}
};

export default config;

Is there antoher way I should be doing this?

@teemingc

Copy link
Copy Markdown
Member

Will this work for plain javascript? I'm still having this issue (#14047). I even had to update the derived to check if the pathname has an ending slash for the base href.

import { page } from '$app/stores';

let { href, icon, size, label } = $props();

let hrefWithSlash = $derived(

	href.endsWith('/') ? href : `${href}/`

);

$inspect($page.url.pathname, hrefWithSlash);

Console

init /store/dashboard/orders/ /store/
link.svelte:11 init /store/dashboard/orders/ /store/dashboard/
link.svelte:11 init /store/dashboard/orders/ /store/dashboard/orders/
link.svelte:11 init /store/dashboard/orders/ /store/dashboard/tasks/
link.svelte:11 init /store/dashboard/orders/ /store/dashboard/products/
link.svelte:11 init /store/dashboard/orders/ /store/dashboard/users/
link.svelte:11 init /store/dashboard/orders/ /store/dashboard/settings/
import { page } from '$app/stores';

let { href, icon, size, label } = $props();

let hrefWithSlash = $derived(

	`${href}/`

);

$inspect($page.url.pathname, hrefWithSlash);

Console

update /store/ /store//
utils.js:236 update /store/ /store/dashboard/
utils.js:236 update /store/ /store/dashboard/orders/
utils.js:236 update /store/ /store/dashboard/tasks/
utils.js:236 update /store/ /store/dashboard/products/
utils.js:236 update /store/ /store/dashboard/users/
utils.js:236 update /store/ /store/dashboard/settings/

Now I get /store//

This is my svelte.config.js

import adapter from '@sveltejs/adapter-node';

/** @type {import('@sveltejs/kit').Config} */
const config = {
	kit: {
		paths: {
			base: '/store',
			relative: false
		},
		adapter: adapter({
			precompress: true
		})
	}
};

export default config;

Is there antoher way I should be doing this?

You should try to create a new discussion in GitHub or visit the Svelte Discord to ask for help.

Copilot AI pushed a commit to Stadly/kit that referenced this pull request Mar 6, 2026
* fix: add support for trailing slashes in typed routes

* fix merge

---------

Co-authored-by: Rich Harris <[email protected]>
Co-authored-by: Rich Harris <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

resolve() lacks a trailing slash when using trailingSlash = always

5 participants