-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(router): hash history links with target="_blank" generate incorrect URLs
#5242
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
fix(router): hash history links with target="_blank" generate incorrect URLs
#5242
Conversation
WalkthroughInternal link normalization in React and Solid Link components now routes stripped-origin paths through Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor U as User
participant L as Link (React/Solid)
participant R as Router
participant H as History
U->>L: Render <Link href="...">
L->>R: Inspect router.origin and href
alt internal (href startsWith router.origin)
L->>H: createHref(strippedPath)
H-->>L: normalizedHref (e.g., "/#/about" for hash history)
else external
L-->>L: use original href
end
L-->>U: Render <a href="normalizedHref|original" ...>
note right of L: React Link also re-evaluates when router.history changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests
Tip 👮 Agentic pre-merge checks are now available in preview!Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.
Please see the documentation for more information. Example: reviews:
pre_merge_checks:
custom_checks:
- name: "Undocumented Breaking Changes"
mode: "warning"
instructions: |
Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).Please share your feedback with us on this Discord post. Comment |
|
View your CI Pipeline Execution ↗ for commit 2ef5384
☁️ Nx Cloud last updated this comment at |
0de856d to
7771c00
Compare
…history mode Signed-off-by: leesb971204 <leesb971204@gmail.com>
More templates
@tanstack/arktype-adapter
@tanstack/directive-functions-plugin
@tanstack/eslint-plugin-router
@tanstack/history
@tanstack/nitro-v2-vite-plugin
@tanstack/react-router
@tanstack/react-router-devtools
@tanstack/react-router-ssr-query
@tanstack/react-start
@tanstack/react-start-client
@tanstack/react-start-server
@tanstack/router-cli
@tanstack/router-core
@tanstack/router-devtools
@tanstack/router-devtools-core
@tanstack/router-generator
@tanstack/router-plugin
@tanstack/router-ssr-query-core
@tanstack/router-utils
@tanstack/router-vite-plugin
@tanstack/server-functions-plugin
@tanstack/solid-router
@tanstack/solid-router-devtools
@tanstack/solid-start
@tanstack/solid-start-client
@tanstack/solid-start-server
@tanstack/start-client-core
@tanstack/start-plugin-core
@tanstack/start-server-core
@tanstack/start-static-server-functions
@tanstack/start-storage-context
@tanstack/valibot-adapter
@tanstack/virtual-file-routes
@tanstack/zod-adapter
commit: |
| if (router.origin) { | ||
| if (href.startsWith(router.origin)) { | ||
| href = href.replace(router.origin, '') || '/' | ||
| href = router.history.createHref(href.replace(router.origin, '')) || '/' |
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.
Since routes with target="_blank" don’t go through the router.navigate logic, the href needs to include #.
But the hrefOption generation logic wasn't applying router.history.createHref() transformation, which is essential for hash history to format URLs correctly (adding the # prefix).
|
some tests would be nice |
Signed-off-by: leesb971204 <leesb971204@gmail.com>
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.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/react-router/tests/link.test.tsx(2 hunks)packages/solid-router/tests/link.test.tsx(2 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
Use TypeScript in strict mode with extensive type safety across the codebase
Files:
packages/react-router/tests/link.test.tsxpackages/solid-router/tests/link.test.tsx
packages/{react-router,solid-router}/**
📄 CodeRabbit inference engine (AGENTS.md)
Implement React and Solid bindings/components only in packages/react-router/ and packages/solid-router/
Files:
packages/react-router/tests/link.test.tsxpackages/solid-router/tests/link.test.tsx
🧠 Learnings (1)
📓 Common learnings
Learnt from: CR
PR: TanStack/router#0
File: AGENTS.md:0-0
Timestamp: 2025-09-23T17:36:12.598Z
Learning: Applies to packages/{react-router,solid-router}/** : Implement React and Solid bindings/components only in packages/react-router/ and packages/solid-router/
🧬 Code graph analysis (1)
packages/react-router/tests/link.test.tsx (1)
packages/react-router/src/router.ts (1)
createRouter(80-82)
⏰ 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 (1)
packages/solid-router/tests/link.test.tsx (1)
5681-5728: Nice regression coverage for hash-history links.The new test nails the original repro: confirming both standard and
target="_blank"links emit/#/…ensures the fix keeps working in hash mode. Thanks for guarding this edge case.
| const hashHistory = createHashHistory() | ||
|
|
||
| const rootRoute = createRootRoute() | ||
| const indexRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/', | ||
| component: () => { | ||
| return ( | ||
| <> | ||
| <h1>Index</h1> | ||
| <Link data-testid="posts-link" to="/posts"> | ||
| Posts (same tab) | ||
| </Link> | ||
| <Link data-testid="about-blank-link" to="/about" target="_blank"> | ||
| About (new tab) | ||
| </Link> | ||
| </> | ||
| ) | ||
| }, | ||
| }) | ||
|
|
||
| const postsRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/posts', | ||
| component: () => <h1>Posts</h1>, | ||
| }) | ||
|
|
||
| const aboutRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/about', | ||
| component: () => <h1>About</h1>, | ||
| }) | ||
|
|
||
| const router = createRouter({ | ||
| routeTree: rootRoute.addChildren([indexRoute, postsRoute, aboutRoute]), | ||
| history: hashHistory, | ||
| }) | ||
|
|
||
| render(<RouterProvider router={router} />) | ||
|
|
||
| const postsLink = await screen.findByTestId('posts-link') | ||
| expect(postsLink).toHaveAttribute('href', '/#/posts') | ||
| expect(postsLink).not.toHaveAttribute('target', '_blank') | ||
|
|
||
| const postsBlankLink = await screen.findByTestId('about-blank-link') | ||
| expect(postsBlankLink).toHaveAttribute('href', '/#/about') | ||
| expect(postsBlankLink).toHaveAttribute('target', '_blank') | ||
| }) |
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.
Destroy the hash history instance after the test.
createHashHistory() hooks a window hashchange listener. Unlike the other tests, this block never tears it down (the global afterEach only destroys the createBrowserHistory from beforeEach). Leaving the hash history alive can bleed listeners and state into subsequent tests. Please ensure we destroy it—either reassign history = hashHistory so the existing cleanup handles it, or explicitly call hashHistory.destroy() in a finally block here.
- const hashHistory = createHashHistory()
+ const hashHistory = createHashHistory()
+ try {
…
- expect(postsBlankLink).toHaveAttribute('target', '_blank')
+ expect(postsBlankLink).toHaveAttribute('target', '_blank')
+ } finally {
+ hashHistory.destroy()
+ }📝 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.
| const hashHistory = createHashHistory() | |
| const rootRoute = createRootRoute() | |
| const indexRoute = createRoute({ | |
| getParentRoute: () => rootRoute, | |
| path: '/', | |
| component: () => { | |
| return ( | |
| <> | |
| <h1>Index</h1> | |
| <Link data-testid="posts-link" to="/posts"> | |
| Posts (same tab) | |
| </Link> | |
| <Link data-testid="about-blank-link" to="/about" target="_blank"> | |
| About (new tab) | |
| </Link> | |
| </> | |
| ) | |
| }, | |
| }) | |
| const postsRoute = createRoute({ | |
| getParentRoute: () => rootRoute, | |
| path: '/posts', | |
| component: () => <h1>Posts</h1>, | |
| }) | |
| const aboutRoute = createRoute({ | |
| getParentRoute: () => rootRoute, | |
| path: '/about', | |
| component: () => <h1>About</h1>, | |
| }) | |
| const router = createRouter({ | |
| routeTree: rootRoute.addChildren([indexRoute, postsRoute, aboutRoute]), | |
| history: hashHistory, | |
| }) | |
| render(<RouterProvider router={router} />) | |
| const postsLink = await screen.findByTestId('posts-link') | |
| expect(postsLink).toHaveAttribute('href', '/#/posts') | |
| expect(postsLink).not.toHaveAttribute('target', '_blank') | |
| const postsBlankLink = await screen.findByTestId('about-blank-link') | |
| expect(postsBlankLink).toHaveAttribute('href', '/#/about') | |
| expect(postsBlankLink).toHaveAttribute('target', '_blank') | |
| }) | |
| const hashHistory = createHashHistory() | |
| try { | |
| const rootRoute = createRootRoute() | |
| const indexRoute = createRoute({ | |
| getParentRoute: () => rootRoute, | |
| path: '/', | |
| component: () => { | |
| return ( | |
| <> | |
| <h1>Index</h1> | |
| <Link data-testid="posts-link" to="/posts"> | |
| Posts (same tab) | |
| </Link> | |
| <Link data-testid="about-blank-link" to="/about" target="_blank"> | |
| About (new tab) | |
| </Link> | |
| </> | |
| ) | |
| }, | |
| }) | |
| const postsRoute = createRoute({ | |
| getParentRoute: () => rootRoute, | |
| path: '/posts', | |
| component: () => <h1>Posts</h1>, | |
| }) | |
| const aboutRoute = createRoute({ | |
| getParentRoute: () => rootRoute, | |
| path: '/about', | |
| component: () => <h1>About</h1>, | |
| }) | |
| const router = createRouter({ | |
| routeTree: rootRoute.addChildren([indexRoute, postsRoute, aboutRoute]), | |
| history: hashHistory, | |
| }) | |
| render(<RouterProvider router={router} />) | |
| const postsLink = await screen.findByTestId('posts-link') | |
| expect(postsLink).toHaveAttribute('href', '/#/posts') | |
| expect(postsLink).not.toHaveAttribute('target', '_blank') | |
| const postsBlankLink = await screen.findByTestId('about-blank-link') | |
| expect(postsBlankLink).toHaveAttribute('href', '/#/about') | |
| expect(postsBlankLink).toHaveAttribute('target', '_blank') | |
| } finally { | |
| hashHistory.destroy() | |
| } | |
| }) |
🤖 Prompt for AI Agents
packages/react-router/tests/link.test.tsx around lines 6342 to 6389: the test
creates a hash history with createHashHistory() but never destroys it, leaking a
window hashchange listener into later tests; fix by ensuring the instance is
cleaned up—either assign the created hashHistory to the test-level history
variable so the shared afterEach cleanup will destroy it, or wrap the test logic
in try/finally and call hashHistory.destroy() in the finally block to explicitly
remove the listener.
|
thanks a lot! |
fixes #5209
Summary by CodeRabbit
Bug Fixes
Tests
New Features