From e10c522f121b2b980827bed5270a9a1a37cc5943 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Tue, 3 Mar 2020 12:00:54 -0600 Subject: [PATCH 1/4] Add identifier to NEXT_DATA for gs(s)p --- packages/next/next-server/lib/utils.ts | 2 ++ packages/next/next-server/server/render.tsx | 8 ++++++++ .../getserversideprops/test/index.test.js | 12 ++++++++++++ test/integration/prerender/test/index.test.js | 12 ++++++++++++ 4 files changed, 34 insertions(+) diff --git a/packages/next/next-server/lib/utils.ts b/packages/next/next-server/lib/utils.ts index 1114770b2aae1..8c1fa81750413 100644 --- a/packages/next/next-server/lib/utils.ts +++ b/packages/next/next-server/lib/utils.ts @@ -78,6 +78,8 @@ export type NEXT_DATA = { isFallback?: boolean dynamicIds?: string[] err?: Error & { statusCode?: number } + gsp?: boolean + gssp?: boolean } /** diff --git a/packages/next/next-server/server/render.tsx b/packages/next/next-server/server/render.tsx index e4b0d81dd3d48..51bb9c94ac833 100644 --- a/packages/next/next-server/server/render.tsx +++ b/packages/next/next-server/server/render.tsx @@ -181,6 +181,8 @@ function renderDocument( htmlProps, bodyTags, headTags, + gsp, + gssp, }: RenderOpts & { props: any docProps: DocumentInitialProps @@ -201,6 +203,8 @@ function renderDocument( bodyTags: any headTags: any isFallback?: boolean + gsp?: boolean + gssp?: boolean } ): string { return ( @@ -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, @@ -680,6 +686,8 @@ export async function renderToHTML( files, lowPriorityFiles, polyfillFiles, + gsp: !!getStaticProps ? true : undefined, + gssp: !!getServerSideProps ? true : undefined, }) if (inAmpMode && html) { diff --git a/test/integration/getserversideprops/test/index.test.js b/test/integration/getserversideprops/test/index.test.js index 98ce879a5e41a..2499fb159054d 100644 --- a/test/integration/getserversideprops/test/index.test.js +++ b/test/integration/getserversideprops/test/index.test.js @@ -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(JSON.parse($('#__NEXT_DATA__').text()).gssp).toBeFalsy() + }) + it('should supply query values SSR', async () => { const html = await renderViaHTTP(appPort, '/blog/post-1?hello=world') const $ = cheerio.load(html) diff --git a/test/integration/prerender/test/index.test.js b/test/integration/prerender/test/index.test.js index a71b87e0773a2..058ef951e92af 100644 --- a/test/integration/prerender/test/index.test.js +++ b/test/integration/prerender/test/index.test.js @@ -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(JSON.parse($('#__NEXT_DATA__').text()).gsp).toBeFalsy() + }) + 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) From 88f85ff40f990ab4ed939add158936b0464e725f Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Tue, 3 Mar 2020 12:13:05 -0600 Subject: [PATCH 2/4] Apply suggestions from code review Co-Authored-By: Joe Haddad --- test/integration/getserversideprops/test/index.test.js | 2 +- test/integration/prerender/test/index.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/getserversideprops/test/index.test.js b/test/integration/getserversideprops/test/index.test.js index 2499fb159054d..860f83874091e 100644 --- a/test/integration/getserversideprops/test/index.test.js +++ b/test/integration/getserversideprops/test/index.test.js @@ -200,7 +200,7 @@ const runTests = (dev = false) => { it('should not have gssp in __NEXT_DATA__ for non-GSSP page', async () => { const html = await renderViaHTTP(appPort, '/normal') const $ = cheerio.load(html) - expect(JSON.parse($('#__NEXT_DATA__').text()).gssp).toBeFalsy() + expect('gssp' in JSON.parse($('#__NEXT_DATA__').text())).toBe(false); }) it('should supply query values SSR', async () => { diff --git a/test/integration/prerender/test/index.test.js b/test/integration/prerender/test/index.test.js index 058ef951e92af..3a3bb916c379f 100644 --- a/test/integration/prerender/test/index.test.js +++ b/test/integration/prerender/test/index.test.js @@ -291,7 +291,7 @@ const runTests = (dev = false, looseMode = false) => { it('should not have gsp in __NEXT_DATA__ for non-GSP page', async () => { const html = await renderViaHTTP(appPort, '/normal') const $ = cheerio.load(html) - expect(JSON.parse($('#__NEXT_DATA__').text()).gsp).toBeFalsy() + 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 () => { From 34af5762cda074daa8aaa8692f9629f3211b9497 Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Tue, 3 Mar 2020 13:26:45 -0500 Subject: [PATCH 3/4] fix lint --- test/integration/getserversideprops/test/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/getserversideprops/test/index.test.js b/test/integration/getserversideprops/test/index.test.js index 860f83874091e..48df86696bb41 100644 --- a/test/integration/getserversideprops/test/index.test.js +++ b/test/integration/getserversideprops/test/index.test.js @@ -200,7 +200,7 @@ const runTests = (dev = false) => { 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); + expect('gssp' in JSON.parse($('#__NEXT_DATA__').text())).toBe(false) }) it('should supply query values SSR', async () => { From fdef821699c397ff77ab73d4eee2fa2e8af3fa9e Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Tue, 3 Mar 2020 12:27:27 -0600 Subject: [PATCH 4/4] apply lint fix --- test/integration/getserversideprops/test/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/getserversideprops/test/index.test.js b/test/integration/getserversideprops/test/index.test.js index 860f83874091e..48df86696bb41 100644 --- a/test/integration/getserversideprops/test/index.test.js +++ b/test/integration/getserversideprops/test/index.test.js @@ -200,7 +200,7 @@ const runTests = (dev = false) => { 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); + expect('gssp' in JSON.parse($('#__NEXT_DATA__').text())).toBe(false) }) it('should supply query values SSR', async () => {