Unverified Commit 62cbbf52 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix API page check during SSG page collecting (#17092)

Fixes SSG pages that start with `/api` not being detected as SSG pages. This also adds tests to ensure this is working correctly in the `prerender` suite.

x-ref: https://github.com/vercel/next.js/issues/17091 
parent d3f4a4cb
...@@ -523,7 +523,9 @@ export default async function build( ...@@ -523,7 +523,9 @@ export default async function build(
let isHybridAmp = false let isHybridAmp = false
let ssgPageRoutes: string[] | null = null let ssgPageRoutes: string[] | null = null
const nonReservedPage = !page.match(/^\/(_app|_error|_document|api)/) const nonReservedPage = !page.match(
/^\/(_app|_error|_document|api(\/|$))/
)
if (nonReservedPage) { if (nonReservedPage) {
const serverBundle = getPagePath(page, distDir, isLikeServerless) const serverBundle = getPagePath(page, distDir, isLikeServerless)
......
import { useRouter } from 'next/router'
export const getStaticProps = () => {
return {
props: {
hello: 'world',
},
}
}
export const getStaticPaths = () => {
return {
paths: ['/api-docs/first'],
fallback: true,
}
}
export default function Slug(props) {
if (useRouter().isFallback) return 'Loading...'
return (
<>
<p id="api-docs">API Docs</p>
<p id="props">{JSON.stringify(props)}</p>
</>
)
}
...@@ -142,6 +142,11 @@ const expectedManifestRoutes = () => ({ ...@@ -142,6 +142,11 @@ const expectedManifestRoutes = () => ({
initialRevalidateSeconds: 1, initialRevalidateSeconds: 1,
srcRoute: null, srcRoute: null,
}, },
'/api-docs/first': {
dataRoute: `/_next/data/${buildId}/api-docs/first.json`,
initialRevalidateSeconds: false,
srcRoute: '/api-docs/[...slug]',
},
'/blocking-fallback-some/a': { '/blocking-fallback-some/a': {
dataRoute: `/_next/data/${buildId}/blocking-fallback-some/a.json`, dataRoute: `/_next/data/${buildId}/blocking-fallback-some/a.json`,
initialRevalidateSeconds: 1, initialRevalidateSeconds: 1,
...@@ -602,6 +607,38 @@ const runTests = (dev = false, isEmulatedServerless = false) => { ...@@ -602,6 +607,38 @@ const runTests = (dev = false, isEmulatedServerless = false) => {
}) })
} }
it('should server prerendered path correctly for SSG pages that starts with api-docs', async () => {
const html = await renderViaHTTP(appPort, '/api-docs/first')
const $ = cheerio.load(html)
expect($('#api-docs').text()).toBe('API Docs')
expect(JSON.parse($('#props').text())).toEqual({
hello: 'world',
})
})
it('should render correctly for SSG pages that starts with api-docs', async () => {
const browser = await webdriver(appPort, '/api-docs/second')
await browser.waitForElementByCss('#api-docs')
expect(await browser.elementByCss('#api-docs').text()).toBe('API Docs')
expect(JSON.parse(await browser.elementByCss('#props').text())).toEqual({
hello: 'world',
})
})
it('should return data correctly for SSG pages that starts with api-docs', async () => {
const data = await renderViaHTTP(
appPort,
`/_next/data/${buildId}/api-docs/first.json`
)
const { pageProps } = JSON.parse(data)
expect(pageProps).toEqual({
hello: 'world',
})
})
it('should SSR catch-all page with brackets in param as string', async () => { it('should SSR catch-all page with brackets in param as string', async () => {
const html = await renderViaHTTP( const html = await renderViaHTTP(
appPort, appPort,
...@@ -1136,6 +1173,20 @@ const runTests = (dev = false, isEmulatedServerless = false) => { ...@@ -1136,6 +1173,20 @@ const runTests = (dev = false, isEmulatedServerless = false) => {
), ),
page: '/another', page: '/another',
}, },
{
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/api\\-docs\\/(.+?)\\.json$`
),
namedDataRouteRegex: `^/_next/data/${escapeRegex(
buildId
)}/api\\-docs/(?<slug>.+?)\\.json$`,
page: '/api-docs/[...slug]',
routeKeys: {
slug: 'slug',
},
},
{ {
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/bad-gssp.json$` `^\\/_next\\/data\\/${escapeRegex(buildId)}\\/bad-gssp.json$`
...@@ -1388,6 +1439,14 @@ const runTests = (dev = false, isEmulatedServerless = false) => { ...@@ -1388,6 +1439,14 @@ const runTests = (dev = false, isEmulatedServerless = false) => {
expect(manifest.version).toBe(2) expect(manifest.version).toBe(2)
expect(manifest.routes).toEqual(expectedManifestRoutes()) expect(manifest.routes).toEqual(expectedManifestRoutes())
expect(manifest.dynamicRoutes).toEqual({ expect(manifest.dynamicRoutes).toEqual({
'/api-docs/[...slug]': {
dataRoute: `/_next/data/${buildId}/api-docs/[...slug].json`,
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapedBuildId}\\/api\\-docs\\/(.+?)\\.json$`
),
fallback: '/api-docs/[...slug].html',
routeRegex: normalizeRegEx(`^\\/api\\-docs\\/(.+?)(?:\\/)?$`),
},
'/blocking-fallback-once/[slug]': { '/blocking-fallback-once/[slug]': {
dataRoute: `/_next/data/${buildId}/blocking-fallback-once/[slug].json`, dataRoute: `/_next/data/${buildId}/blocking-fallback-once/[slug].json`,
dataRouteRegex: normalizeRegEx( dataRouteRegex: normalizeRegEx(
...@@ -1995,6 +2054,7 @@ describe('SSG Prerender', () => { ...@@ -1995,6 +2054,7 @@ describe('SSG Prerender', () => {
'/non-json/[p].js', '/non-json/[p].js',
'/blog/[post]/index.js', '/blog/[post]/index.js',
'/fallback-only/[slug].js', '/fallback-only/[slug].js',
'/api-docs/[...slug].js',
] ]
const fallbackBlockingPages = [ const fallbackBlockingPages = [
'/blocking-fallback/[slug].js', '/blocking-fallback/[slug].js',
......
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