-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: correctly handle client-side vs server-side redirects with rewrites #5330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { Link, createFileRoute } from '@tanstack/react-router' | ||
|
|
||
| export const Route = createFileRoute('/redirect/')({ | ||
| component: RouteComponent, | ||
| }) | ||
|
|
||
| function RouteComponent() { | ||
| return ( | ||
| <div className="p-2 flex gap-2"> | ||
| <Link to="/redirect/throw-it"> | ||
| <div data-testid="link-to-throw-it">Throw It</div> | ||
| </Link> | ||
| </div> | ||
| ) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { createFileRoute, redirect } from '@tanstack/react-router' | ||
|
|
||
| export const Route = createFileRoute('/redirect/throw-it')({ | ||
| beforeLoad: () => { | ||
| throw redirect({ | ||
| to: '/posts/$postId', | ||
| params: { postId: '1' }, | ||
| }) | ||
| }, | ||
| }) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2230,18 +2230,17 @@ export class RouterCore< | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resolveRedirect = (redirect: AnyRedirect): AnyRedirect => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!redirect.options.href) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let href = this.buildLocation(redirect.options).url | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const location = this.buildLocation(redirect.options) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let href = location.url | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (this.origin && href.startsWith(this.origin)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| href = href.replace(this.origin, '') || '/' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+2234
to
2237
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Remove unused origin-stripping code. The local This dead code should be removed for clarity, or if the origin stripping is needed, the logic should be updated to use the processed Apply this diff to remove the unused code: resolveRedirect = (redirect: AnyRedirect): AnyRedirect => {
if (!redirect.options.href) {
const location = this.buildLocation(redirect.options)
- let href = location.url
- if (this.origin && href.startsWith(this.origin)) {
- href = href.replace(this.origin, '') || '/'
- }
redirect.options.href = location.href
redirect.headers.set('Location', redirect.options.href)
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| redirect.options.href = href | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| redirect.headers.set('Location', redirect.options.href) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| redirect.options.href = location.href | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| redirect.headers.set('Location', href) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!redirect.headers.get('Location')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| redirect.headers.set('Location', redirect.options.href) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return redirect | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
2231
to
2245
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use The past review comment correctly identified dead code at lines 2234-2237. The Additionally, the manual origin-stripping is fragile—if Since Apply this diff: resolveRedirect = (redirect: AnyRedirect): AnyRedirect => {
if (!redirect.options.href) {
const location = this.buildLocation(redirect.options)
- let href = location.url
- if (this.origin && href.startsWith(this.origin)) {
- href = href.replace(this.origin, '') || '/'
- }
redirect.options.href = location.href
- redirect.headers.set('Location', href)
+ redirect.headers.set('Location', location.publicHref)
}
if (!redirect.headers.get('Location')) {
redirect.headers.set('Location', redirect.options.href)
}
return redirect
}This ensures:
📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, this is exactly what is wanted. in case the origin matches the router's origin (e.g. only if the redirect points to a different origin, THEN we want a full url
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @schiller-manuel, thank you for the clarification! I understand now—the origin-stripping logic is intentional to handle same-origin vs. cross-origin redirects correctly:
This ensures the Location header contains the appropriate format based on whether the redirect target matches the router's origin. I appreciate you explaining the reasoning behind this implementation! ✏️ Learnings added
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct the RedirectIndexRoute path
Route.updatecurrently setspath: '/redirect/', but the generated type metadata just below (seeFileRoutesByPath['/redirect/']) expects/redirectwithout the trailing slash. That mismatch means the runtime route emits/redirect/URLs while the type-safe APIs point to/redirect, which undermines the new redirect tests and can reintroduce the basepath duplication we’re fixing. Please align the runtime path with the metadata by dropping the trailing slash.const RedirectIndexRoute = RedirectIndexRouteImport.update({ id: '/redirect/', - path: '/redirect/', + path: '/redirect', getParentRoute: () => rootRouteImport, } as any)📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dont ever again bother about the autogenerated file routeTree.gen.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@schiller-manuel Understood! I'll skip reviewing
routeTree.gen.tsfiles in future reviews since they're autogenerated.✏️ Learnings added
🧠 Learnings used