Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix API page check during SSG page collecting #17092

Merged
merged 6 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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