sveltejs / sveltejs/kit

Prerender with `reroute` and `trailingSlash="always"`

Open
#11,648 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the bug

When configuring a sveltekit project with prerender=true and trailingSlash="always" in a +layout.server.js with the following hooks file :

/** @type {Record<string, string>} */
const translated = {
	'/reroute': '/'
};

/** @type {import('@sveltejs/kit').Reroute} */
export function reroute({ url }) {
        const redirect = translated[url.pathname]
	if (redirect) {
		return redirect;
	}
}

the build fail with the following error :

SvelteKitError: Not found: /reroute/
    at new SvelteKitError (/home/httpswwwsveltelabdev-dcnb/.svelte-kit/output/server/index.js:111:5)
    at resolve2 (/home/httpswwwsveltelabdev-dcnb/.svelte-kit/output/server/index.js:2886:18)
    at resolve (/home/httpswwwsveltelabdev-dcnb/.svelte-kit/output/server/index.js:2718:34)
    at #options.hooks.handle (/home/httpswwwsveltelabdev-dcnb/.svelte-kit/output/server/index.js:2960:71)
    at respond (/home/httpswwwsveltelabdev-dcnb/.svelte-kit/output/server/index.js:2716:43) {
  status: 404,
  text: 'Not Found'
}
Error: 404 /reroute/ (linked from /reroute)
To suppress or handle this error, implement `handleHttpError` in https://kit.svelte.dev/docs/configuration#prerender
    at eval (/home/httpswwwsveltelabdev-dcnb/node_modules/@sveltejs/kit/src/core/config/options.js:217:13)
    at eval (/home/httpswwwsveltelabdev-dcnb/node_modules/@sveltejs/kit/src/core/postbuild/prerender.js:81:25)
    at save (/home/httpswwwsveltelabdev-dcnb/node_modules/@sveltejs/kit/src/core/postbuild/prerender.js:416:4)
    at visit (/home/httpswwwsveltelabdev-dcnb/node_modules/@sveltejs/kit/src/core/postbuild/prerender.js:249:3)

When removing the trainlingSlash line in the +layout.server.js, the build pass.

Please note that this does not happen when running in dev mode.

Reproduction

https://www.sveltelab.dev/zq5pv5rncgy0obm

npm install
npm run build

The build should fail.

When removing the line trailingSlash in +layout.server.ts, the build pass.

Logs

No response

System Info
System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.14.0 - /usr/local/bin/pnpm
  npmPackages:
    @sveltejs/adapter-static: ^3.0.1 => 3.0.1 
    @sveltejs/kit: ^2.0.0 => 2.3.3 
    @sveltejs/vite-plugin-svelte: ^3.0.0 => 3.0.1 
    svelte: ^4.0.5 => 4.2.8 
    vite: ^5.0.0 => 5.0.11
Severity

blocking all usage of SvelteKit

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked reproduction and run npm install followed by npm run build. Compare the behavior with prerender=true and trailingSlash="always" in +layout.server.js and the reroute hook shown in the issue. Done means the prerender build completes while /reroute is mapped to / as intended.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
build-system, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.