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

Add identifier to NEXT_DATA for gs(s)p #10812

Merged
merged 6 commits into from Mar 3, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/next/next-server/lib/utils.ts
Expand Up @@ -78,6 +78,8 @@ export type NEXT_DATA = {
isFallback?: boolean
dynamicIds?: string[]
err?: Error & { statusCode?: number }
gsp?: boolean
gssp?: boolean
}

/**
Expand Down
8 changes: 8 additions & 0 deletions packages/next/next-server/server/render.tsx
Expand Up @@ -181,6 +181,8 @@ function renderDocument(
htmlProps,
bodyTags,
headTags,
gsp,
gssp,
}: RenderOpts & {
props: any
docProps: DocumentInitialProps
Expand All @@ -201,6 +203,8 @@ function renderDocument(
bodyTags: any
headTags: any
isFallback?: boolean
gsp?: boolean
gssp?: boolean
}
): string {
return (
Expand All @@ -221,6 +225,8 @@ function renderDocument(
dynamicIds:
dynamicImportsIds.length === 0 ? undefined : dynamicImportsIds,
err: err ? serializeError(dev, err) : undefined, // Error if one happened, otherwise don't sent in the resulting HTML
gsp, // whether the page is getStaticProps
gssp, // whether the page is getServerSideProps
},
dangerousAsPath,
canonicalBase,
Expand Down Expand Up @@ -680,6 +686,8 @@ export async function renderToHTML(
files,
lowPriorityFiles,
polyfillFiles,
gsp: !!getStaticProps ? true : undefined,
gssp: !!getServerSideProps ? true : undefined,
})

if (inAmpMode && html) {
Expand Down
12 changes: 12 additions & 0 deletions test/integration/getserversideprops/test/index.test.js
Expand Up @@ -191,6 +191,18 @@ const runTests = (dev = false) => {
expect(html).toMatch(/Post:.*?post-1/)
})

it('should have gssp in __NEXT_DATA__', async () => {
const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html)
expect(JSON.parse($('#__NEXT_DATA__').text()).gssp).toBe(true)
})

it('should not have gssp in __NEXT_DATA__ for non-GSSP page', async () => {
const html = await renderViaHTTP(appPort, '/normal')
const $ = cheerio.load(html)
expect('gssp' in JSON.parse($('#__NEXT_DATA__').text())).toBe(false)
})

it('should supply query values SSR', async () => {
const html = await renderViaHTTP(appPort, '/blog/post-1?hello=world')
const $ = cheerio.load(html)
Expand Down
12 changes: 12 additions & 0 deletions test/integration/prerender/test/index.test.js
Expand Up @@ -282,6 +282,18 @@ const runTests = (dev = false, looseMode = false) => {
expect(html).toMatch(/Post:.*?post-1/)
})

it('should have gsp in __NEXT_DATA__', async () => {
const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html)
expect(JSON.parse($('#__NEXT_DATA__').text()).gsp).toBe(true)
})

it('should not have gsp in __NEXT_DATA__ for non-GSP page', async () => {
const html = await renderViaHTTP(appPort, '/normal')
const $ = cheerio.load(html)
expect('gsp' in JSON.parse($('#__NEXT_DATA__').text())).toBe(false)
})

it('should not supply query values to params or useRouter non-dynamic page SSR', async () => {
const html = await renderViaHTTP(appPort, '/something?hello=world')
const $ = cheerio.load(html)
Expand Down