Skip to content

Commit

Permalink
Add identifier to NEXT_DATA for gs(s)p (#10812)
Browse files Browse the repository at this point in the history
* Add identifier to NEXT_DATA for gs(s)p

* Apply suggestions from code review

Co-Authored-By: Joe Haddad <joe.haddad@zeit.co>

* fix lint

* apply lint fix

Co-authored-by: Joe Haddad <timer150@gmail.com>
  • Loading branch information
ijjk and Timer committed Mar 3, 2020
1 parent 3ca022a commit 4ef1cd4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
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

0 comments on commit 4ef1cd4

Please sign in to comment.