Skip to content

Commit

Permalink
Fix API page check during SSG page collecting (vercel#17092)
Browse files Browse the repository at this point in the history
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: vercel#17091
  • Loading branch information
ijjk authored and Piotr Bosak committed Sep 26, 2020
1 parent f8eaeb1 commit bcfe224
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,9 @@ export default async function build(
let isHybridAmp = false
let ssgPageRoutes: string[] | null = null

const nonReservedPage = !page.match(/^\/(_app|_error|_document|api)/)
const nonReservedPage = !page.match(
/^\/(_app|_error|_document|api(\/|$))/
)

if (nonReservedPage) {
const serverBundle = getPagePath(page, distDir, isLikeServerless)
Expand Down
27 changes: 27 additions & 0 deletions test/integration/prerender/pages/api-docs/[...slug].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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>
</>
)
}
60 changes: 60 additions & 0 deletions test/integration/prerender/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ const expectedManifestRoutes = () => ({
initialRevalidateSeconds: 1,
srcRoute: null,
},
'/api-docs/first': {
dataRoute: `/_next/data/${buildId}/api-docs/first.json`,
initialRevalidateSeconds: false,
srcRoute: '/api-docs/[...slug]',
},
'/blocking-fallback-some/a': {
dataRoute: `/_next/data/${buildId}/blocking-fallback-some/a.json`,
initialRevalidateSeconds: 1,
Expand Down Expand Up @@ -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 () => {
const html = await renderViaHTTP(
appPort,
Expand Down Expand Up @@ -1136,6 +1173,20 @@ const runTests = (dev = false, isEmulatedServerless = false) => {
),
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(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/bad-gssp.json$`
Expand Down Expand Up @@ -1388,6 +1439,14 @@ const runTests = (dev = false, isEmulatedServerless = false) => {
expect(manifest.version).toBe(2)
expect(manifest.routes).toEqual(expectedManifestRoutes())
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]': {
dataRoute: `/_next/data/${buildId}/blocking-fallback-once/[slug].json`,
dataRouteRegex: normalizeRegEx(
Expand Down Expand Up @@ -1995,6 +2054,7 @@ describe('SSG Prerender', () => {
'/non-json/[p].js',
'/blog/[post]/index.js',
'/fallback-only/[slug].js',
'/api-docs/[...slug].js',
]
const fallbackBlockingPages = [
'/blocking-fallback/[slug].js',
Expand Down

0 comments on commit bcfe224

Please sign in to comment.