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

Fix middleware dynamic route param on query hydration #41436

Merged
merged 1 commit into from Oct 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/next/shared/lib/router/router.ts
Expand Up @@ -1602,7 +1602,9 @@ export default class Router implements BaseRouter {
rewriteAs = localeResult.pathname
}
const routeRegex = getRouteRegex(pathname)
const curRouteMatch = getRouteMatcher(routeRegex)(rewriteAs)
const curRouteMatch = getRouteMatcher(routeRegex)(
new URL(rewriteAs, location.href).pathname
)

if (curRouteMatch) {
Object.assign(query, curRouteMatch)
Expand Down
24 changes: 24 additions & 0 deletions test/e2e/middleware-rewrites/test/index.test.ts
Expand Up @@ -381,6 +381,30 @@ describe('Middleware Rewrite', () => {
)
})

it('should have correct query info for dynamic route after query hydration', async () => {
const browser = await webdriver(
next.url,
'/fallback-true-blog/first?hello=world'
)

await check(
() =>
browser.eval(
'next.router.query.hello === "world" ? "success" : JSON.stringify(next.router.query)'
),
'success'
)

expect(await browser.eval('next.router.query')).toEqual({
slug: 'first',
hello: 'world',
})
expect(await browser.eval('location.pathname')).toBe(
'/fallback-true-blog/first'
)
expect(await browser.eval('location.search')).toBe('?hello=world')
})

it('should handle shallow navigation correctly (non-dynamic page)', async () => {
const browser = await webdriver(next.url, '/about')
const requests = []
Expand Down