Unverified Commit 9afcdfcb authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure i18n index prefetch is correct with trailingSlash (#22746)

This fixes index data route loading for i18n with `trailingSlash: true` enabled and also fixes prerendering `asPath` values not containing a trailingSlash when enabled. 


Fixes: https://github.com/vercel/next.js/issues/17813
Fixes: https://github.com/vercel/next.js/issues/22747
parent c1b2b3f9
...@@ -1022,7 +1022,6 @@ export default async function build( ...@@ -1022,7 +1022,6 @@ export default async function build(
} }
return defaultMap return defaultMap
}, },
trailingSlash: false,
} }
await exportApp(dir, exportOptions, exportConfig) await exportApp(dir, exportOptions, exportConfig)
......
...@@ -8,6 +8,7 @@ import { ...@@ -8,6 +8,7 @@ import {
import getAssetPathFromRoute from '../next-server/lib/router/utils/get-asset-path-from-route' import getAssetPathFromRoute from '../next-server/lib/router/utils/get-asset-path-from-route'
import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic' import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic'
import { parseRelativeUrl } from '../next-server/lib/router/utils/parse-relative-url' import { parseRelativeUrl } from '../next-server/lib/router/utils/parse-relative-url'
import { removePathTrailingSlash } from './normalize-trailing-slash'
import createRouteLoader, { import createRouteLoader, {
getClientBuildManifest, getClientBuildManifest,
RouteLoader, RouteLoader,
...@@ -96,7 +97,10 @@ export default class PageLoader { ...@@ -96,7 +97,10 @@ export default class PageLoader {
const route = normalizeRoute(hrefPathname) const route = normalizeRoute(hrefPathname)
const getHrefForSlug = (path: string) => { const getHrefForSlug = (path: string) => {
const dataRoute = getAssetPathFromRoute(addLocale(path, locale), '.json') const dataRoute = getAssetPathFromRoute(
removePathTrailingSlash(addLocale(path, locale)),
'.json'
)
return addBasePath( return addBasePath(
`/_next/data/${this.buildId}${dataRoute}${ssg ? '' : search}` `/_next/data/${this.buildId}${dataRoute}${ssg ? '' : search}`
) )
......
...@@ -170,7 +170,7 @@ export default async function exportApp( ...@@ -170,7 +170,7 @@ export default async function exportApp(
) )
} }
const subFolders = nextConfig.trailingSlash const subFolders = nextConfig.trailingSlash && !options.buildExport
const isLikeServerless = nextConfig.target !== 'server' const isLikeServerless = nextConfig.target !== 'server'
if (!options.silent && !options.buildExport) { if (!options.silent && !options.buildExport) {
...@@ -367,6 +367,7 @@ export default async function exportApp( ...@@ -367,6 +367,7 @@ export default async function exportApp(
locale: i18n?.defaultLocale, locale: i18n?.defaultLocale,
defaultLocale: i18n?.defaultLocale, defaultLocale: i18n?.defaultLocale,
domainLocales: i18n?.domains, domainLocales: i18n?.domains,
trailingSlash: nextConfig.trailingSlash,
} }
const { serverRuntimeConfig, publicRuntimeConfig } = nextConfig const { serverRuntimeConfig, publicRuntimeConfig } = nextConfig
......
...@@ -76,6 +76,7 @@ interface RenderOpts { ...@@ -76,6 +76,7 @@ interface RenderOpts {
locales?: string[] locales?: string[]
locale?: string locale?: string
defaultLocale?: string defaultLocale?: string
trailingSlash?: boolean
} }
type ComponentModule = ComponentType<{}> & { type ComponentModule = ComponentType<{}> & {
...@@ -184,6 +185,10 @@ export default async function exportPage({ ...@@ -184,6 +185,10 @@ export default async function exportPage({
res.statusCode = 500 res.statusCode = 500
} }
if (renderOpts.trailingSlash && !req.url?.endsWith('/')) {
req.url += '/'
}
envConfig.setConfig({ envConfig.setConfig({
serverRuntimeConfig, serverRuntimeConfig,
publicRuntimeConfig: renderOpts.runtimeConfig, publicRuntimeConfig: renderOpts.runtimeConfig,
......
...@@ -28,6 +28,11 @@ export default function Page(props) { ...@@ -28,6 +28,11 @@ export default function Page(props) {
<Link href="/gsp" locale="fr"> <Link href="/gsp" locale="fr">
<a id="to-gsp-fr">to /gsp</a> <a id="to-gsp-fr">to /gsp</a>
</Link> </Link>
<br />
<Link href="/" locale="fr">
<a id="to-index-fr">to /</a>
</Link>
</> </>
) )
} }
...@@ -28,6 +28,11 @@ export default function Page(props) { ...@@ -28,6 +28,11 @@ export default function Page(props) {
<Link href="/gsp" locale="fr"> <Link href="/gsp" locale="fr">
<a id="to-gsp-fr">to /gsp</a> <a id="to-gsp-fr">to /gsp</a>
</Link> </Link>
<br />
<Link href="/" locale="fr">
<a id="to-index-fr">to /</a>
</Link>
</> </>
) )
} }
...@@ -14,7 +14,9 @@ import { ...@@ -14,7 +14,9 @@ import {
fetchViaHTTP, fetchViaHTTP,
File, File,
launchApp, launchApp,
check,
} from 'next-test-utils' } from 'next-test-utils'
import assert from 'assert'
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
const nextConfig = new File(join(appDir, 'next.config.js')) const nextConfig = new File(join(appDir, 'next.config.js'))
...@@ -291,21 +293,38 @@ describe('i18n Support', () => { ...@@ -291,21 +293,38 @@ describe('i18n Support', () => {
}) })
describe('with trailingSlash: true', () => { describe('with trailingSlash: true', () => {
const curCtx = { const runSlashTests = (curCtx) => {
...ctx, if (!curCtx.isDev) {
isDev: true, it('should preload all locales data correctly', async () => {
} const browser = await webdriver(
beforeAll(async () => { curCtx.appPort,
await fs.remove(join(appDir, '.next')) `${curCtx.basePath}/mixed`
nextConfig.replace('// trailingSlash', 'trailingSlash') )
curCtx.appPort = await findPort() await browser.eval(`(function() {
curCtx.app = await launchApp(appDir, curCtx.appPort) document.querySelector('#to-gsp-en-us').scrollIntoView()
}) document.querySelector('#to-gsp-nl-nl').scrollIntoView()
afterAll(async () => { document.querySelector('#to-gsp-fr').scrollIntoView()
nextConfig.restore() })()`)
await killApp(curCtx.app)
await check(async () => {
const hrefs = await browser.eval(
`Object.keys(window.next.router.sdc)`
)
hrefs.sort()
assert.deepEqual(
hrefs.map((href) =>
new URL(href).pathname
.replace(ctx.basePath, '')
.replace(/^\/_next\/data\/[^/]+/, '')
),
['/en-US/gsp.json', '/fr.json', '/fr/gsp.json', '/nl-NL/gsp.json']
)
return 'yes'
}, 'yes')
}) })
}
it('should redirect correctly', async () => { it('should redirect correctly', async () => {
for (const locale of nonDomainLocales) { for (const locale of nonDomainLocales) {
...@@ -375,7 +394,9 @@ describe('i18n Support', () => { ...@@ -375,7 +394,9 @@ describe('i18n Support', () => {
expect(await browser.elementByCss('#router-as-path').text()).toBe( expect(await browser.elementByCss('#router-as-path').text()).toBe(
'/gsp/' '/gsp/'
) )
expect(await browser.elementByCss('#router-locale').text()).toBe(locale) expect(await browser.elementByCss('#router-locale').text()).toBe(
locale
)
expect(await browser.eval('window.beforeNav')).toBe(1) expect(await browser.eval('window.beforeNav')).toBe(1)
expect(await browser.eval('window.location.pathname')).toBe( expect(await browser.eval('window.location.pathname')).toBe(
`${localePath}gsp/` `${localePath}gsp/`
...@@ -383,9 +404,13 @@ describe('i18n Support', () => { ...@@ -383,9 +404,13 @@ describe('i18n Support', () => {
await browser.back().waitForElementByCss('#index') await browser.back().waitForElementByCss('#index')
expect(await browser.elementByCss('#router-pathname').text()).toBe('/') expect(await browser.elementByCss('#router-pathname').text()).toBe(
'/'
)
expect(await browser.elementByCss('#router-as-path').text()).toBe('/') expect(await browser.elementByCss('#router-as-path').text()).toBe('/')
expect(await browser.elementByCss('#router-locale').text()).toBe(locale) expect(await browser.elementByCss('#router-locale').text()).toBe(
locale
)
expect(await browser.eval('window.beforeNav')).toBe(1) expect(await browser.eval('window.beforeNav')).toBe(1)
expect(await browser.eval('window.location.pathname')).toBe( expect(await browser.eval('window.location.pathname')).toBe(
`${localePath}` `${localePath}`
...@@ -400,12 +425,55 @@ describe('i18n Support', () => { ...@@ -400,12 +425,55 @@ describe('i18n Support', () => {
expect(await browser.elementByCss('#router-as-path').text()).toBe( expect(await browser.elementByCss('#router-as-path').text()).toBe(
'/gssp/first/' '/gssp/first/'
) )
expect(await browser.elementByCss('#router-locale').text()).toBe(locale) expect(await browser.elementByCss('#router-locale').text()).toBe(
locale
)
expect(await browser.eval('window.beforeNav')).toBe(1) expect(await browser.eval('window.beforeNav')).toBe(1)
expect(await browser.eval('window.location.pathname')).toBe( expect(await browser.eval('window.location.pathname')).toBe(
`${localePath}gssp/first/` `${localePath}gssp/first/`
) )
} }
}) })
}
describe('dev mode', () => {
const curCtx = {
...ctx,
isDev: true,
}
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
nextConfig.replace('// trailingSlash', 'trailingSlash')
curCtx.appPort = await findPort()
curCtx.app = await launchApp(appDir, curCtx.appPort)
})
afterAll(async () => {
nextConfig.restore()
await killApp(curCtx.app)
})
runSlashTests(curCtx)
})
describe('production mode', () => {
const curCtx = {
...ctx,
}
beforeAll(async () => {
await fs.remove(join(appDir, '.next'))
nextConfig.replace('// trailingSlash', 'trailingSlash')
await nextBuild(appDir)
curCtx.appPort = await findPort()
curCtx.app = await nextStart(appDir, curCtx.appPort)
})
afterAll(async () => {
nextConfig.restore()
await killApp(curCtx.app)
})
runSlashTests(curCtx)
})
}) })
}) })
...@@ -339,7 +339,7 @@ export function runTests(ctx) { ...@@ -339,7 +339,7 @@ export function runTests(ctx) {
.replace(ctx.basePath, '') .replace(ctx.basePath, '')
.replace(/^\/_next\/data\/[^/]+/, '') .replace(/^\/_next\/data\/[^/]+/, '')
), ),
['/en-US/gsp.json', '/fr/gsp.json', '/nl-NL/gsp.json'] ['/en-US/gsp.json', '/fr.json', '/fr/gsp.json', '/nl-NL/gsp.json']
) )
return 'yes' return 'yes'
}, 'yes') }, 'yes')
......
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