Unverified Commit 90a86e67 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure auto-export dynamic routes work with i18n next start (#18930)

This ensures dynamic routes are correctly generated for `next start` with i18n and adds tests to the i18n suite to ensure they are working correctly. 

Closes: https://github.com/vercel/next.js/issues/18397
parent 78204684
...@@ -100,6 +100,11 @@ type FindComponentsResult = { ...@@ -100,6 +100,11 @@ type FindComponentsResult = {
query: ParsedUrlQuery query: ParsedUrlQuery
} }
type DynamicRouteItem = {
page: string
match: ReturnType<typeof getRouteMatcher>
}
export type ServerConstructor = { export type ServerConstructor = {
/** /**
* Where the Next project is located - @default '.' * Where the Next project is located - @default '.'
...@@ -985,13 +990,21 @@ export default class Server { ...@@ -985,13 +990,21 @@ export default class Server {
] ]
} }
protected getDynamicRoutes() { protected getDynamicRoutes(): Array<DynamicRouteItem> {
const addedPages = new Set<string>()
return getSortedRoutes(Object.keys(this.pagesManifest!)) return getSortedRoutes(Object.keys(this.pagesManifest!))
.filter(isDynamicRoute) .filter(isDynamicRoute)
.map((page) => ({ .map((page) => {
page, page = normalizeLocalePath(page, this.nextConfig.i18n?.locales).pathname
match: getRouteMatcher(getRouteRegex(page)), if (addedPages.has(page)) return null
})) addedPages.add(page)
return {
page,
match: getRouteMatcher(getRouteRegex(page)),
}
})
.filter((item): item is DynamicRouteItem => Boolean(item))
} }
private handleCompression(req: IncomingMessage, res: ServerResponse): void { private handleCompression(req: IncomingMessage, res: ServerResponse): void {
......
import Link from 'next/link'
import { useRouter } from 'next/router'
export default function Page(props) {
const router = useRouter()
return (
<>
<p id="dynamic">dynamic page</p>
<p id="router-locale">{router.locale}</p>
<p id="router-default-locale">{router.defaultLocale}</p>
<p id="router-locales">{JSON.stringify(router.locales)}</p>
<p id="router-query">{JSON.stringify(router.query)}</p>
<p id="router-pathname">{router.pathname}</p>
<p id="router-as-path">{router.asPath}</p>
<Link href="/another">
<a id="to-another">to /another</a>
</Link>
<br />
<Link href="/gsp">
<a id="to-gsp">to /gsp</a>
</Link>
<br />
<Link href="/gsp/fallback/first">
<a id="to-fallback-first">to /gsp/fallback/first</a>
</Link>
<br />
<Link href="/gsp/fallback/hello">
<a id="to-fallback-hello">to /gsp/fallback/hello</a>
</Link>
<br />
<Link href="/gsp/no-fallback/first">
<a id="to-no-fallback-first">to /gsp/no-fallback/first</a>
</Link>
<br />
<Link href="/gssp">
<a id="to-gssp">to /gssp</a>
</Link>
<br />
<Link href="/gssp/first">
<a id="to-gssp-slug">to /gssp/first</a>
</Link>
<br />
</>
)
}
...@@ -18,6 +18,10 @@ export default function Page(props) { ...@@ -18,6 +18,10 @@ export default function Page(props) {
<a id="to-another">to /another</a> <a id="to-another">to /another</a>
</Link> </Link>
<br /> <br />
<Link href="/dynamic/first">
<a id="to-dynamic">to /dynamic/first</a>
</Link>
<br />
<Link href="/gsp"> <Link href="/gsp">
<a id="to-gsp">to /gsp</a> <a id="to-gsp">to /gsp</a>
</Link> </Link>
......
...@@ -245,6 +245,64 @@ function runTests(isDev) { ...@@ -245,6 +245,64 @@ function runTests(isDev) {
}) })
} }
it('should resolve auto-export dynamic route correctly', async () => {
for (const locale of locales) {
const res = await fetchViaHTTP(
appPort,
`/${locale}/dynamic/first`,
undefined,
{
redirect: 'manual',
}
)
expect(res.status).toBe(200)
expect(await res.text()).toContain('dynamic page')
}
})
it('should navigate to auto-export dynamic page', async () => {
for (const locale of locales) {
const browser = await webdriver(appPort, `/${locale}`)
await browser.eval('window.beforeNav = 1')
await browser
.elementByCss('#to-dynamic')
.click()
.waitForElementByCss('#dynamic')
expect(await browser.eval('window.beforeNav')).toBe(1)
expect(await browser.elementByCss('#router-locale').text()).toBe(locale)
expect(
JSON.parse(await browser.elementByCss('#router-locales').text())
).toEqual(locales)
expect(
JSON.parse(await browser.elementByCss('#router-query').text())
).toEqual({ slug: 'first' })
expect(await browser.elementByCss('#router-pathname').text()).toBe(
'/dynamic/[slug]'
)
expect(await browser.elementByCss('#router-as-path').text()).toBe(
'/dynamic/first'
)
expect(await browser.eval('window.location.pathname')).toBe(
`${locale === 'en-US' ? '' : `/${locale}`}/dynamic/first`
)
await browser.back().waitForElementByCss('#index')
expect(await browser.eval('window.beforeNav')).toBe(1)
expect(await browser.elementByCss('#router-locale').text()).toBe(locale)
expect(
JSON.parse(await browser.elementByCss('#router-locales').text())
).toEqual(locales)
expect(
JSON.parse(await browser.elementByCss('#router-query').text())
).toEqual({})
expect(await browser.elementByCss('#router-pathname').text()).toBe('/')
expect(await browser.elementByCss('#router-as-path').text()).toBe('/')
}
})
it('should apply redirects correctly', async () => { it('should apply redirects correctly', async () => {
for (const path of ['/redirect', '/en-US/redirect', '/nl/redirect']) { for (const path of ['/redirect', '/en-US/redirect', '/nl/redirect']) {
const res = await fetchViaHTTP(appPort, path, undefined, { const res = await fetchViaHTTP(appPort, path, undefined, {
......
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