Skip to content

refactor(router-core): throw redirect w/ a pre-build location doesn't need to re-parse from string - #6512

Merged
Sheraff merged 1 commit into
mainfrom
refactor-router-core-redirect-pre-built-location
Jan 25, 2026
Merged

refactor(router-core): throw redirect w/ a pre-build location doesn't need to re-parse from string#6512
Sheraff merged 1 commit into
mainfrom
refactor-router-core-redirect-pre-built-location

Conversation

@Sheraff

@SheraffSheraff commented Jan 25, 2026

Copy link
Copy Markdown
Collaborator

During SSR, if the href of an incoming request ends up matching and being re-written to another href (for example: adding default search params), we throw a redirect({ href }) with that new href to force a Response(, { headers: { Location: href } })

When handling a thrown redirect, we treat href as any untrusted string. This is sub-optimal when it actually comes from us just parsing a correct internal location.

This PR proposes an internal _builtLocation argument that bypasses such checks and re-parsing.

before
Screenshot 2026-01-25 at 18 59 31

after
Screenshot 2026-01-25 at 18 59 41

@coderabbitai

coderabbitaiBot commented Jan 25, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The PR introduces an internal _builtLocation field to redirect handling, allowing the router to carry and reuse pre-built location objects through the redirect flow. This optimization bypasses redundant location building and protocol/URL parsing checks when a computed location is already available.

Changes

Cohort / File(s)Summary
Internal location caching for redirects
packages/router-core/src/redirect.ts, packages/router-core/src/router.ts
Added optional _builtLocation?: ParsedLocation field to Redirect.options and RedirectOptions types; updated resolveRedirect to attach pre-built locations for non-external redirects; modified location building to reuse _builtLocation when present, skipping protocol checks and URL parsing; extended BuildNextOptions signature to support the new field

Sequence Diagram

sequenceDiagram
participant Router
participant RedirectHandler as Redirect Handler
participant LocationBuilder as Location Builder
Router->>RedirectHandler: resolveRedirect(redirect, location)
alt _builtLocation provided
RedirectHandler->>LocationBuilder: Use _builtLocation directly
LocationBuilder->>LocationBuilder: Skip protocol & URL parsing
else No _builtLocation
RedirectHandler->>LocationBuilder: Build new location from options
LocationBuilder->>LocationBuilder: Execute full parsing logic
end
LocationBuilder->>Router: Return location for redirect
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • #6447 — Modifies router-core's location/buildLocation/redirect flow including ParsedLocation shape handling
  • #4986 — Updates redirect.ts URL-parsing logic to conditionally skip URL object construction
  • #6506 — Refactors buildLocation and location-building logic in router.ts during redirect/build flows

Suggested reviewers

  • schiller-manuel
  • chorobin

Poem

🐰 With whiskers twitching and paws held high,
We cache our paths before they fly—
No parsing twice, just trust and reuse,
The _builtLocation we can't refuse! 🏃‍♂️✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check nameStatusExplanation
Description Check✅ PassedCheck skipped - CodeRabbit’s high-level summary is enabled.
Title check✅ PassedThe title clearly describes the main change: adding a pre-built location to redirect handling to avoid re-parsing from strings.
Docstring Coverage✅ PassedDocstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Comment @coderabbitai help to get the list of available commands and usage tips.

@nx-cloud

nx-cloudBot commented Jan 25, 2026

Copy link
Copy Markdown
Contributor

View your CI Pipeline Execution ↗ for commit deb48d8

CommandStatusDurationResult
nx affected --targets=test:eslint,test:unit,tes...✅ Succeeded10m 30sView ↗
nx run-many --target=build --exclude=examples/*...✅ Succeeded1m 43sView ↗

☁️ Nx Cloud last updated this comment at 2026-01-25 18:10:00 UTC

@pkg-pr-new

Copy link
Copy Markdown
More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@6512

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@6512

@tanstack/history

npm i https://pkg.pr.new/TanStack/router/@tanstack/history@6512

@tanstack/nitro-v2-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/nitro-v2-vite-plugin@6512

@tanstack/react-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@6512

@tanstack/react-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@6512

@tanstack/react-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-ssr-query@6512

@tanstack/react-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@6512

@tanstack/react-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@6512

@tanstack/react-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@6512

@tanstack/router-cli

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@6512

@tanstack/router-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@6512

@tanstack/router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@6512

@tanstack/router-devtools-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@6512

@tanstack/router-generator

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@6512

@tanstack/router-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@6512

@tanstack/router-ssr-query-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-ssr-query-core@6512

@tanstack/router-utils

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@6512

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@6512

@tanstack/solid-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@6512

@tanstack/solid-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@6512

@tanstack/solid-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-ssr-query@6512

@tanstack/solid-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@6512

@tanstack/solid-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@6512

@tanstack/solid-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@6512

@tanstack/start-client-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@6512

@tanstack/start-fn-stubs

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-fn-stubs@6512

@tanstack/start-plugin-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@6512

@tanstack/start-server-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@6512

@tanstack/start-static-server-functions

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-static-server-functions@6512

@tanstack/start-storage-context

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@6512

@tanstack/valibot-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@6512

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@6512

@tanstack/vue-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router@6512

@tanstack/vue-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router-devtools@6512

@tanstack/vue-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router-ssr-query@6512

@tanstack/vue-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start@6512

@tanstack/vue-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-client@6512

@tanstack/vue-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-server@6512

@tanstack/zod-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@6512

commit: deb48d8

@Sheraff
Sheraff merged commit 54649f9 into mainJan 25, 2026
6 checks passed
@Sheraff
Sheraff deleted the refactor-router-core-redirect-pre-built-location branch January 25, 2026 18:53
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@Sheraff