Skip to content
Source
Sign inSign up
TanStack/router

Start: prerender forces trailing slashes, causing unnecessary redirects with trailingSlash: "never" #8850

Send to my agent
OpenR4ULtv(GitHub profile, opens in a new tab) opened this issue 1 comment

Which project does this relate to?

Start

Describe the bug

When the router uses trailingSlash: 'never', prerendering still appends a trailing slash to each requested page URL. The router then returns a 307 redirect to the slashless URL, and prerender follows it. This adds an unnecessary request for every non-root page.

This happens with an explicit list of unique, slashless paths, with both autoStaticPathsDiscovery and crawlLinks disabled. It reproduces with the default Node preview server, without Cloudflare or any other adapter.

Complete minimal reproducer

https://github.com/R4ULtv/tanstack-start-prerender-trailing-slash

Steps to reproduce

  1. Clone the reproduction repository.
  2. Run npm ci.
  3. Run npm run build.
  4. Inspect the [preview-request] lines in the output.

The reproduction has one dynamic route (/page/$id) and three explicit paths: /page/1, /page/2, and /page/3. All input paths are slashless.

For each path the output shows:

[prerender] Crawling: /page/1
[preview-request] GET /page/1/ -> 307 Location: /page/1
[preview-request] GET /page/1 -> 200

The build succeeds, but each page requires two requests. The reproduction's Vite plugin only logs the preview requests and response status/Location header; it does not modify them. Setting PAGE_COUNT to 1 is sufficient to reproduce the issue.

Expected behavior

With trailingSlash: 'never', prerender should request slashless URLs directly, avoiding the redirect for each page. URL normalization should account for the configured trailing-slash policy and base path.

Platform

  • @tanstack/react-start: 1.169.4
  • @tanstack/react-router: 1.170.46
  • @tanstack/start-plugin-core (transitive): 1.171.55
  • React / React DOM: 19.3.1
  • Vite: 8.3.5
  • Node.js: 24.22.2
  • OS: macOS (Apple Silicon)
  • Adapter: default Node preview server

Additional context

In @tanstack/start-plugin-core/src/prerender.ts, the URL passed to fetch is constructed with:

withTrailingSlash(withBase(page.path, routerBasePath))

This forces the slash even though the provided page path is already canonical.

The reproduction also contains an optional npm run benchmark that measures increasing page counts, with four alternating-order runs per variant. It compares current behavior with a preview-only rewrite that avoids the redirect, checks request counts and generated output, and produces a self-contained HTML report with the raw measurements. These are synthetic local Node results at concurrency 1; the percentage improvement is workload-specific and excludes deployment.

Recorded benchmark artifacts:

I mentioned this separately in #8483, which primarily concerns CPU parallelism. This report isolates the redirect behavior independently of concurrency.

Related: #6978 / #7070 address duplicate discovered paths. This report concerns additional HTTP requests for explicit, unique paths with discovery/crawling disabled, so normalizing discovered paths alone would not resolve it.

Prepared with AI assistance. The reproduction and benchmark builds were executed and verified locally.

  1. I have a fix ready, and I'd like a maintainer to confirm the approach before I open a PR.

    Cause: prerender.ts requests every page at withTrailingSlash(withBase(page.path, basepath)). With trailingSlash: 'never', every page redirects. The slash also lands after the query string: /posts?page=2 is requested as /posts?page=2/.

    Fix: request each page at its listed path, so the router's trailingSlash setting decides the canonical URL. Only the basepath root keeps the slash (/ → <basepath>/), because that is the path the preview server serves; that root case is what #6304 added the slash for.

    • Branch: https://github.com/R4ULtv/router/compare/main...fix/prerender-trailing-slash (one commit, with a changeset)
    • New regression tests in prerender-ssrf.test.ts fail on main (4 cases, including the query string) and pass with the fix. test:unit, test:eslint and test:types pass for @tanstack/start-plugin-core.
    • start-basic-static (basepath /test/, crawlLinks) produces the same 37 pages on main and on the branch.
    • I've used the same change as a pnpm patch on a 20k-page Cloudflare app (preview deployment). It removed every prerender redirect.

    Should I open the PR?

    Prepared with AI assistance (Claude Code).

Sign in to join this conversation.