Skip to content
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

✨replace react-router wildcard routes with their actual path name #3023

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('createRouter', () => {
it('creates a new view with the fallback route', async () => {
startViewSpy.calls.reset()
await router.navigate('/non-existent')
expect(startViewSpy).toHaveBeenCalledWith('/*')
expect(startViewSpy).toHaveBeenCalledWith('/non-existent')
})

it('does not create a new view when the router navigates to the same URL', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,18 @@ describe('computeViewName', () => {
] as RouteMatch[])
).toBe('/foo/bar/:id/')
})

it('replaces match-all routes with the actual path', () => {
expect(
computeViewName([
{ route: { path: '/foo' } },
{
params: { '*': 'bar' },
pathname: '/bar',
pathnameBase: '/',
route: { path: '*' },
},
] as RouteMatch[])
).toBe('/foo/bar')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ export function computeViewName(routeMatches: RouteMatch[]) {
}

if (path.startsWith('/')) {
// Absolute path, replace the current view name
viewName = path
} else {
// Relative path, append to the current view name
if (!viewName.endsWith('/')) {
viewName += '/'
}
viewName += path
if (path === '*') {
viewName += routeMatch.params['*']!
} else {
viewName += path
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions sandbox/react-app/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const router = createBrowserRouter(
path: 'test-error-boundary',
Component: TestErrorBoundaryPage,
},
{
path: '*',
Component: WildCardPage,
},
],
},
],
Expand Down Expand Up @@ -65,6 +69,11 @@ function UserPage() {
return <h1>User {id}</h1>
}

function WildCardPage() {
const path = useParams()['*']
return <h1>Wildcard: {path}</h1>
}

export function TestErrorBoundaryPage() {
const [shouldThrow, setShouldThrow] = useState(false)

Expand Down
4 changes: 3 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

"paths": {
"@datadog/browser-core": ["./packages/core/src"],
"@datadog/browser-rum-core": ["./packages/rum-core/src"]
"@datadog/browser-rum-core": ["./packages/rum-core/src"],
"@datadog/browser-rum-react": ["./packages/rum-react/src/entries/main"],
"@datadog/browser-rum-react/react-router-v6": ["./packages/rum-react/src/entries/reactRouterV6"]
}
}
}