未验证 提交 33ebda1b 编写于 作者: J Jan Potoms 提交者: GitHub

Fix root route optional catch-all prerendering (#14986)

Fixes https://github.com/vercel/next.js/issues/14964
上级 62902545
......@@ -791,7 +791,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
const isSsgFallback = ssgFallbackPages.has(page)
const isDynamic = isDynamicRoute(page)
const hasAmp = hybridAmpPages.has(page)
let file = normalizePagePath(page)
const file = normalizePagePath(page)
// The dynamic version of SSG pages are only prerendered if the fallback
// is enabled. Below, we handle the specific prerenders of these.
......@@ -826,11 +826,12 @@ export default async function build(dir: string, conf = null): Promise<void> {
// `getStaticPaths` (additionalSsgPaths).
const extraRoutes = additionalSsgPaths.get(page) || []
for (const route of extraRoutes) {
await moveExportedPage(page, route, route, true, 'html')
await moveExportedPage(page, route, route, true, 'json')
const pageFile = normalizePagePath(route)
await moveExportedPage(page, route, pageFile, true, 'html')
await moveExportedPage(page, route, pageFile, true, 'json')
if (hasAmp) {
const ampPage = `${normalizePagePath(route)}.amp`
const ampPage = `${pageFile}.amp`
await moveExportedPage(page, ampPage, ampPage, true, 'html')
await moveExportedPage(page, ampPage, ampPage, true, 'json')
}
......
export const getStaticPaths = () => ({
paths: [
{ params: { optionalName: [] } },
{ params: { optionalName: ['one'] } },
{ params: { optionalName: ['one', 'two'] } },
],
fallback: false,
})
export const getStaticProps = ({ params }) => ({ props: { params } })
export default function Page({ params }) {
return (
<div id="success">
{params.optionalName ? params.optionalName.join(',') : 'yay'}
</div>
)
}
/* eslint-env jest */
import cheerio from 'cheerio'
import fs from 'fs-extra'
import {
findPort,
killApp,
launchApp,
nextBuild,
nextStart,
renderViaHTTP,
} from 'next-test-utils'
import { join } from 'path'
jest.setTimeout(1000 * 60 * 2)
let app
let appPort
const appDir = join(__dirname, '../')
function runTests() {
it('should render optional catch-all top-level route with no segments', async () => {
const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html)
expect($('#success').text()).toBe('yay')
})
it('should render optional catch-all top-level route with one segment', async () => {
const html = await renderViaHTTP(appPort, '/one')
const $ = cheerio.load(html)
expect($('#success').text()).toBe('one')
})
it('should render optional catch-all top-level route with two segments', async () => {
const html = await renderViaHTTP(appPort, '/one/two')
const $ = cheerio.load(html)
expect($('#success').text()).toBe('one,two')
})
}
const nextConfig = join(appDir, 'next.config.js')
describe('Dynamic Optional Routing', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
describe('production mode', () => {
beforeAll(async () => {
const curConfig = await fs.readFile(nextConfig, 'utf8')
if (curConfig.includes('target')) {
await fs.writeFile(nextConfig, `module.exports = {}`)
}
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTests()
})
describe('serverless mode', () => {
let origNextConfig
beforeAll(async () => {
origNextConfig = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await fs.writeFile(nextConfig, origNextConfig)
await killApp(app)
})
runTests()
})
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册