fix: redirect respects rewrites #5315
main fromrefs/pull/5315/head·merged by schiller-manuel(GitHub profile, opens in a new tab) fixes #5300
Summary by CodeRabbit#
- Bug Fixes
- Corrected localhost examples to use http, avoiding TLS/mixed-content issues.
- Improved SSR origin detection for more reliable URL construction.
- Redirects now normalize to relative paths, preventing incorrect absolute-origin links.
- Refactor
- Centralized origin resolution and exported a shared origin utility for consistent server and router behavior.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: router-core
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: start-server-core
View your CI Pipeline Execution ↗ for commit 86a5961071a516695c9a955044586ba517a8388c
Command Status Duration Result nx affected --targets=test:eslint,test:unit,tes...✅ Succeeded 46s View ↗ nx run-many --target=build --exclude=examples/*...✅ Succeeded 1s View ↗
☁️ Nx Cloud last updated this comment at
2025-09-30 18:43:26UTCMore templates
- tanstack-router-solid-example-basic
- tanstack-router-solid-example-basic-devtools-panel
- tanstack-router-solid-example-basic-file-based
- tanstack-router-solid-example-basic-non-nested-devtools
- tanstack-router-solid-example-basic-solid-query
- tanstack-router-solid-example-basic-solid-query-file-based
- tanstack-router-solid-example-basic-ssr-streaming-file-based
- tanstack-router-solid-example-kitchen-sink-file-based
- tanstack-router-solid-example-quickstart-file-based
- tanstack-solid-start-example-bare
- tanstack-solid-start-example-basic
- tanstack-solid-start-example-basic-static
- tanstack-router-react-example-authenticated-routes
- tanstack-router-react-example-authenticated-routes-firebase
- tanstack-router-react-example-basic
- tanstack-router-react-example-basic-default-search-params
- tanstack-router-react-example-basic-devtools-panel
- tanstack-router-react-example-basic-file-based
- tanstack-router-react-example-basic-non-nested-devtools
- tanstack-router-react-example-react-query
- tanstack-router-react-example-basic-react-query-file-based
- tanstack-router-react-example-basic-ssr-file-based
- tanstack-router-react-example-basic-ssr-streaming-file-based
- tanstack-router-react-example-basic-virtual-file-based
- tanstack-router-react-example-basic-virtual-inside-file-based
- tanstack-router-react-example-deferred-data
- tanstack-router-i18n-paraglide
- tanstack-router-react-example-kitchen-sink
- tanstack-router-react-example-kitchen-sink-file-based
- tanstack-router-react-example-kitchen-sink-react-query
- tanstack-router-react-example-kitchen-sink-react-query-file-based
- tanstack-router-react-example-large-file-based
- tanstack-router-react-example-location-masking
- tanstack-router-react-example-navigation-blocking
- tanstack-router-react-example-quickstart
- tanstack-router-react-example-quickstart-esbuild-file-based
- tanstack-router-react-example-quickstart-file-based
- tanstack-router-react-example-quickstart-rspack-file-based
- tanstack-router-react-example-quickstart-webpack-file-based
- router-monorepo-react-query
- router-mono-simple
- router-mono-simple-lazy
- tanstack-router-react-example-scroll-restoration
- tanstack-search-validator-adapters
- tanstack-start-example-bare
- tanstack-start-example-basic
- tanstack-start-example-basic-auth
- tanstack-start-example-basic-cloudflare
- tanstack-start-example-basic-react-query
- tanstack-start-example-basic-static
- tanstack-start-bun-hosting
- tanstack-start-example-convex-trellaux
- tanstack-start-example-counter
- tanstack-start-i18n-paraglide
- tanstack-start-example-large
- tanstack-start-example-material-ui
- tanstack-start-example-supabase-basic
- tanstack-start-tailwind-v4
- tanstack-start-example-trellaux
- tanstack-start-example-workos
- tanstack-router-react-example-view-transitions
- tanstack-router-react-example-with-framer-motion
- tanstack-router-react-example-with-trpc
- tanstack-router-react-example-with-trpc-react-query
@tanstack/arktype-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@5315@tanstack/directive-functions-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/directive-functions-plugin@5315@tanstack/eslint-plugin-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@5315@tanstack/history
npm i https://pkg.pr.new/TanStack/router/@tanstack/history@5315@tanstack/nitro-v2-vite-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/nitro-v2-vite-plugin@5315@tanstack/react-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@5315@tanstack/react-router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@5315@tanstack/react-router-ssr-query
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-ssr-query@5315@tanstack/react-start
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@5315@tanstack/react-start-client
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@5315@tanstack/react-start-server
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@5315@tanstack/router-cli
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@5315@tanstack/router-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@5315@tanstack/router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@5315@tanstack/router-devtools-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@5315@tanstack/router-generator
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@5315@tanstack/router-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@5315@tanstack/router-ssr-query-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-ssr-query-core@5315@tanstack/router-utils
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@5315@tanstack/router-vite-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@5315@tanstack/server-functions-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/server-functions-plugin@5315@tanstack/solid-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@5315@tanstack/solid-router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@5315@tanstack/solid-start
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@5315@tanstack/solid-start-client
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@5315@tanstack/solid-start-server
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@5315@tanstack/start-client-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@5315@tanstack/start-plugin-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@5315@tanstack/start-server-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@5315@tanstack/start-static-server-functions
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-static-server-functions@5315@tanstack/start-storage-context
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@5315@tanstack/valibot-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@5315@tanstack/virtual-file-routes
npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@5315@tanstack/zod-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@5315commit:
c6e1e48Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between 581941a86a9e8b0858b6c69c313e7d7a0708075e and c6e1e48e7ff4c08dd21bc80897d8d490607452ff.
📒 Files selected for processing (7)
examples/react/basic-ssr-file-based/src/entry-server.tsx(1 hunks)examples/react/basic-ssr-streaming-file-based/src/entry-server.tsx(2 hunks)packages/router-core/src/router.ts(1 hunks)packages/router-core/src/ssr/createRequestHandler.ts(3 hunks)packages/router-core/src/ssr/server.ts(1 hunks)packages/router-core/src/ssr/ssr-server.ts(1 hunks)packages/start-server-core/src/createStartHandler.ts(5 hunks)
🧰 Additional context used
📓 Path-based instructions (4)
**/*.{ts,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
Use TypeScript in strict mode with extensive type safety across the codebase
Files:
examples/react/basic-ssr-file-based/src/entry-server.tsxpackages/router-core/src/ssr/ssr-server.tspackages/router-core/src/ssr/server.tspackages/router-core/src/router.tspackages/start-server-core/src/createStartHandler.tspackages/router-core/src/ssr/createRequestHandler.tsexamples/react/basic-ssr-streaming-file-based/src/entry-server.tsx
examples/{react,solid}/**
📄 CodeRabbit inference engine (AGENTS.md)
Keep example applications under examples/react/ and examples/solid/
Files:
examples/react/basic-ssr-file-based/src/entry-server.tsxexamples/react/basic-ssr-streaming-file-based/src/entry-server.tsx
packages/router-core/**
📄 CodeRabbit inference engine (AGENTS.md)
Keep framework-agnostic core router logic in packages/router-core/
Files:
packages/router-core/src/ssr/ssr-server.tspackages/router-core/src/ssr/server.tspackages/router-core/src/router.tspackages/router-core/src/ssr/createRequestHandler.ts
packages/{*-start,start-*}/**
📄 CodeRabbit inference engine (AGENTS.md)
Name and place Start framework packages under packages/-start/ or packages/start-/
Files:
packages/start-server-core/src/createStartHandler.ts
🧬 Code graph analysis (4)
packages/router-core/src/ssr/ssr-server.ts (1)
packages/router-core/src/ssr/server.ts (1)
getOrigin(10-10)
packages/router-core/src/router.ts (1)
packages/router-core/src/redirect.ts (1)
redirect(57-93)
packages/start-server-core/src/createStartHandler.ts (1)
packages/router-core/src/ssr/ssr-server.ts (1)
getOrigin(145-157)
packages/router-core/src/ssr/createRequestHandler.ts (1)
packages/router-core/src/ssr/ssr-server.ts (1)
getOrigin(145-157)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Preview
- GitHub Check: Test
🔇 Additional comments (10)
examples/react/basic-ssr-streaming-file-based/src/entry-server.tsx (1)
21-21: Protocol change looks intentional but verify consistency.The base URL protocol changed from
httpstohttp. This aligns with the newgetOriginutility's fallback default of'http://localhost'(as seen in the related changes). Ensure this matches your local development environment configuration.examples/react/basic-ssr-file-based/src/entry-server.tsx (1)
21-21: LGTM! Consistent protocol change.The base URL protocol change from
httpstohttpis consistent with the streaming variant and aligns with the centralized origin resolution strategy introduced in this PR.packages/router-core/src/ssr/server.ts (1)
10-10: LGTM! Exports new origin resolution utility.The addition of
getOriginto the public exports enables consumers to use the centralized origin resolution logic. This is part of the broader refactoring to standardize origin handling across SSR flows.packages/router-core/src/ssr/createRequestHandler.ts (1)
3-3: LGTM! Centralized origin resolution for SSR.The changes properly:
- Import the new
getOriginutility - Compute the origin from the request using
getOrigin(request) - Respect explicit
router.options.originconfiguration while falling back to the computed origin
This ensures consistent origin handling across SSR flows and integrates cleanly with the redirect resolution fix.
Also applies to: 30-42
packages/router-core/src/router.ts (1)
2231-2246: Ensure path-only hrefs are supported byrouter.navigatein all contexts. Test the redirect flow in both SSR (start-server, router-ssr-query) and CSR (solid-start, react-start, load-matches) to confirmrouter.navigate(resolveRedirect(err).options)correctly handles relativeoptions.href. Validate reproduction of issue #5300: navigating from/my-app/posts/2should redirect to/my-app/errorrather than/error.packages/router-core/src/ssr/ssr-server.ts (1)
145-157: LGTM! Clean origin resolution with sensible fallbacks.The implementation correctly prioritizes the Origin header, falls back to parsing request.url, and provides a safe default. The try/catch blocks appropriately handle invalid URLs without exposing error details.
packages/start-server-core/src/createStartHandler.ts (4)
14-17: Good refactor: centralizing origin resolution.Moving to a shared
getOriginutility from@tanstack/router-core/ssr/serverimproves consistency across SSR handlers.
100-100: LGTM! Consistent origin computation.Computing the origin once at the start of the request handler ensures consistent origin resolution across fetch patching and router initialization.
115-115: LGTM! Fetch patching uses consistent origin.Both URL construction paths (string input and Request object) correctly use the shared
originvalue, ensuring relative URLs resolve consistently.Also applies to: 124-124
161-161: LGTM! Correct origin precedence for redirect resolution.The fallback logic
router.options.origin ?? origincorrectly prioritizes explicit configuration while using the computed origin from the request when not specified. This should ensure redirects respect the rewrite base path as intended in the PR objectives.@@ -57,3 +57,4 @@5757 children: <RouterServer router={router} />,5858 }),5959 )60+ console.log(response)⚠️ Potential issue | 🟡 Minor
Remove debug logging before merging.
The
console.log(response)statement appears to be temporary debugging code and should be removed before this PR is merged to production.Apply this diff:
- console.log(response) - // Convert the fetch response back to an express response📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
// Convert the fetch response back to an express response🤖 Prompt for AI Agents
In examples/react/basic-ssr-streaming-file-based/src/entry-server.tsx around line 60, there's a temporary debug console.log(response) that should be removed; delete that console.log statement so no debug output is emitted in production and ensure no other stray debug logs remain before merging.✅ Addressed in commit ed72004
Sign in to join this conversation.
Caution
Review failed#
The pull request is closed.
Walkthrough#
Centralizes server-side origin resolution with a new getOrigin(request) utility, uses it across SSR/start handlers, normalizes redirect hrefs by stripping matching origins to paths, and updates example apps to use http://localhost:3000 (protocol change only).
Changes#
examples/react/basic-ssr-file-based/src/entry-server.tsx,examples/react/basic-ssr-streaming-file-based/src/entry-server.tsxhttps://localhost:3000tohttp://localhost:3000when constructing fetch-equivalent request URLs. No other logic changes.packages/router-core/src/ssr/ssr-server.ts,packages/router-core/src/ssr/server.tsgetOrigin(request)which derives origin from theOriginheader orrequest.url, with fallback tohttp://localhost; re-exportgetOriginfrom the public SSR server module.packages/router-core/src/ssr/createRequestHandler.ts,packages/start-server-core/src/createStartHandler.tsgetOrigin(request)to compute origin for fetch patching and router update; setrouterorigin torouter.options.origin ?? origin; remove duplicated local origin logic.packages/router-core/src/router.tsresolveRedirect, whenhrefis absent, build the URL via the location builder and, if it starts with the router origin, strip that origin to produce a path (defaulting to/); setredirect.options.hrefand theLocationheader accordingly.Sequence Diagram(s)#
Estimated code review effort#
🎯 3 (Moderate) | ⏱️ ~25 minutes
Possibly related PRs#
Suggested reviewers#
Poem#
Pre-merge checks and finishing touches#
❌ Failed checks (2 warnings)
@coderabbitai generate docstringsto improve docstring coverage.✅ Passed checks (3 passed)
📜 Recent review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between c6e1e48e7ff4c08dd21bc80897d8d490607452ff and 86a5961071a516695c9a955044586ba517a8388c.
📒 Files selected for processing (3)
examples/react/basic-ssr-streaming-file-based/src/entry-server.tsx(1 hunks)packages/router-core/src/router.ts(1 hunks)packages/start-server-core/src/createStartHandler.ts(5 hunks)Comment `@coderabbitai help` to get the list of available commands and usage tips.