Unverified Commit f02363e0 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure no-op rewrite matches dynamic route with i18n correctly (#23860)

This ensures we match dynamic routes correctly when applying `check: true` behavior with i18n so that no-op rewrites match dynamic routes correctly. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added

Fixes: https://github.com/vercel/next.js/issues/23854
parent 4f7f5f30
...@@ -153,13 +153,17 @@ export default class Router { ...@@ -153,13 +153,17 @@ export default class Router {
checkParsedUrl.pathname = originalFsPathname checkParsedUrl.pathname = originalFsPathname
} }
} }
let matchedPage = await memoizedPageChecker(fsPathname) let matchedPage = await memoizedPageChecker(fsPathname)
// If we didn't match a page check dynamic routes // If we didn't match a page check dynamic routes
if (!matchedPage) { if (!matchedPage) {
const normalizedFsPathname = normalizeLocalePath(
fsPathname,
this.locales
).pathname
for (const dynamicRoute of this.dynamicRoutes) { for (const dynamicRoute of this.dynamicRoutes) {
if (dynamicRoute.match(fsPathname)) { if (dynamicRoute.match(normalizedFsPathname)) {
matchedPage = true matchedPage = true
} }
} }
......
module.exports = {
i18n: {
locales: ['en', 'fr'],
defaultLocale: 'en',
},
rewrites() {
return [
{
source: '/:path*',
destination: '/:path*',
},
{
source: '/:path*',
destination: '/another',
},
]
},
}
import { useRouter } from 'next/router'
export default function Another(props) {
const router = useRouter()
return (
<p id="router">
{JSON.stringify({
pathname: router.pathname,
asPath: router.asPath,
query: router.query,
another: true,
})}
</p>
)
}
import { useRouter } from 'next/router'
export default function Dynamic(props) {
const router = useRouter()
return (
<p id="router">
{JSON.stringify({
pathname: router.pathname,
asPath: router.asPath,
query: router.query,
dynamic: true,
})}
</p>
)
}
import { useRouter } from 'next/router'
export default function Index(props) {
const router = useRouter()
return (
<p id="router">
{JSON.stringify({
pathname: router.pathname,
asPath: router.asPath,
query: router.query,
index: true,
})}
</p>
)
}
import url from 'url'
import fs from 'fs-extra'
import { join } from 'path'
import webdriver from 'next-webdriver'
import {
nextBuild,
nextStart,
findPort,
killApp,
File,
launchApp,
waitFor,
} from 'next-test-utils'
jest.setTimeout(1000 * 60 * 1)
const appDir = join(__dirname, '../')
const nextConfig = new File(join(appDir, 'next.config.js'))
let appPort
let app
function runTests() {
it('should not rewrite for index page', async () => {
for (const [pathname, query] of [
['/', {}],
['/en', {}],
['/fr', {}],
['/', { hello: 'world' }],
['/en', { hello: 'world' }],
['/fr', { hello: 'world' }],
]) {
const asPath = url.format({ pathname, query })
const browser = await webdriver(appPort, asPath)
expect(JSON.parse(await browser.elementByCss('#router').text())).toEqual({
index: true,
pathname: '/',
asPath: url.format({ pathname: '/', query }),
query,
})
await waitFor(1000)
expect(JSON.parse(await browser.elementByCss('#router').text())).toEqual({
index: true,
pathname: '/',
asPath: url.format({ pathname: '/', query }),
query,
})
}
})
it('should not rewrite for dynamic page', async () => {
for (const [pathname, query] of [
['/dynamic/first', {}],
['/en/dynamic/first', {}],
['/fr/dynamic/first', {}],
['/dynamic/first', { hello: 'world' }],
['/en/dynamic/first', { hello: 'world' }],
['/fr/dynamic/first', { hello: 'world' }],
]) {
const asPath = url.format({ pathname, query })
const browser = await webdriver(appPort, asPath)
expect(JSON.parse(await browser.elementByCss('#router').text())).toEqual({
dynamic: true,
pathname: '/dynamic/[slug]',
asPath: url.format({ pathname: '/dynamic/first', query }),
query: {
...query,
slug: 'first',
},
})
await waitFor(1000)
expect(JSON.parse(await browser.elementByCss('#router').text())).toEqual({
dynamic: true,
pathname: '/dynamic/[slug]',
asPath: url.format({ pathname: '/dynamic/first', query }),
query: {
...query,
slug: 'first',
},
})
}
})
}
describe('i18n Support', () => {
describe('dev mode', () => {
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
nextConfig.restore()
})
runTests()
})
describe('production mode', () => {
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
nextConfig.restore()
})
runTests()
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment