From 869a5060f85bd0caaaa6f209ec460fa806ee8456 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 20 Feb 2020 16:51:03 -0600 Subject: [PATCH 1/5] Add error messages for dynamic SSG page without getStaticPaths --- packages/next/build/utils.ts | 12 ++++++-- packages/next/next-server/server/render.tsx | 13 +++++++-- test/integration/prerender/test/index.test.js | 29 ++++++++++++++++++- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index 88ab4890ae34e..d6622e71da283 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -543,10 +543,18 @@ export async function isPageStatic( throw new Error(SERVER_PROPS_SSG_CONFLICT) } + const pageIsDynamic = isDynamicRoute(page) // A page cannot have static parameters if it is not a dynamic page. - if (hasStaticProps && hasStaticPaths && !isDynamicRoute(page)) { + if (hasStaticProps && hasStaticPaths && !pageIsDynamic) { throw new Error( - `unstable_getStaticPaths can only be used with dynamic pages. https://nextjs.org/docs#dynamic-routing` + `unstable_getStaticPaths can only be used with dynamic pages ${page}. https://nextjs.org/docs#dynamic-routing` + ) + } + + if (pageIsDynamic && !hasStaticPaths) { + throw new Error( + `unstable_getStaticPaths is required for dynamic SSG pages and is missing for ${page}.\n` + + `See here for more info: https://err.sh/zeit/next.js/invalid-getstaticpaths-value` ) } diff --git a/packages/next/next-server/server/render.tsx b/packages/next/next-server/server/render.tsx index a20d35bf27e98..e7002b461d1bf 100644 --- a/packages/next/next-server/server/render.tsx +++ b/packages/next/next-server/server/render.tsx @@ -316,6 +316,8 @@ export async function renderToHTML( const hasPageGetInitialProps = !!(Component as any).getInitialProps + const pageIsDynamic = isDynamicRoute(pathname) + const isAutoExport = !hasPageGetInitialProps && defaultAppGetInitialProps && @@ -325,7 +327,7 @@ export async function renderToHTML( if ( process.env.NODE_ENV !== 'production' && (isAutoExport || isFallback) && - isDynamicRoute(pathname) && + pageIsDynamic && didRewrite ) { // TODO: add err.sh when rewrites go stable @@ -360,6 +362,13 @@ export async function renderToHTML( ) } + if (pageIsDynamic && !unstable_getStaticPaths) { + throw new Error( + `unstable_getStaticPaths is required for dynamic SSG pages and is missing for ${pathname}.\n` + + `See here for more info: https://err.sh/zeit/next.js/invalid-getstaticpaths-value` + ) + } + if (dev) { const { isValidElementType } = require('react-is') if (!isValidElementType(Component)) { @@ -458,7 +467,7 @@ export async function renderToHTML( // invoke, where we'd have to consider server & serverless. const previewData = tryGetPreviewData(req, res, previewProps) const data = await unstable_getStaticProps!({ - ...(isDynamicRoute(pathname) + ...(pageIsDynamic ? { params: query as ParsedUrlQuery, } diff --git a/test/integration/prerender/test/index.test.js b/test/integration/prerender/test/index.test.js index 15cc1629d39d2..1de888d0808e2 100644 --- a/test/integration/prerender/test/index.test.js +++ b/test/integration/prerender/test/index.test.js @@ -1,7 +1,7 @@ /* eslint-env jest */ /* global jasmine */ import fs from 'fs-extra' -import { join } from 'path' +import { join, dirname } from 'path' import cheerio from 'cheerio' import webdriver from 'next-webdriver' import escapeRegex from 'escape-string-regexp' @@ -448,6 +448,33 @@ const runTests = (dev = false) => { } }) + it('should error on dynamic page without getStaticPaths', async () => { + const curPage = join(__dirname, '../pages/temp/[slug].js') + await fs.mkdirp(dirname(curPage)) + await fs.writeFile( + curPage, + ` + export async function unstable_getStaticProps() { + return { + props: { + hello: 'world' + } + } + } + export default () => 'oops' + ` + ) + await waitFor(1000) + try { + const html = await renderViaHTTP(appPort, '/temp/hello') + expect(html).toMatch( + /unstable_getStaticPaths is required for dynamic SSG pages and is missing for/ + ) + } finally { + await fs.remove(curPage) + } + }) + it('should not re-call getStaticProps when updating query', async () => { const browser = await webdriver(appPort, '/something?hello=world') await waitFor(2000) From 70ca110da8bdbcf14f41ef265aff23181b45dea5 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 20 Feb 2020 16:59:07 -0600 Subject: [PATCH 2/5] Update error check --- packages/next/build/utils.ts | 2 +- packages/next/next-server/server/render.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index d6622e71da283..adf28c726b44a 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -551,7 +551,7 @@ export async function isPageStatic( ) } - if (pageIsDynamic && !hasStaticPaths) { + if (hasStaticProps && pageIsDynamic && !hasStaticPaths) { throw new Error( `unstable_getStaticPaths is required for dynamic SSG pages and is missing for ${page}.\n` + `See here for more info: https://err.sh/zeit/next.js/invalid-getstaticpaths-value` diff --git a/packages/next/next-server/server/render.tsx b/packages/next/next-server/server/render.tsx index e7002b461d1bf..bd42c4702864a 100644 --- a/packages/next/next-server/server/render.tsx +++ b/packages/next/next-server/server/render.tsx @@ -362,7 +362,7 @@ export async function renderToHTML( ) } - if (pageIsDynamic && !unstable_getStaticPaths) { + if (isSpr && pageIsDynamic && !unstable_getStaticPaths) { throw new Error( `unstable_getStaticPaths is required for dynamic SSG pages and is missing for ${pathname}.\n` + `See here for more info: https://err.sh/zeit/next.js/invalid-getstaticpaths-value` From cebb6ba03d9ffcf47c41087ff31f0aafccacfad4 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 20 Feb 2020 17:11:12 -0600 Subject: [PATCH 3/5] Update test file --- .../dynamic-routing/pages/p1/p2/all-ssg/[...rest].js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/integration/dynamic-routing/pages/p1/p2/all-ssg/[...rest].js b/test/integration/dynamic-routing/pages/p1/p2/all-ssg/[...rest].js index ec86e4056f5a7..0d054a972ace8 100644 --- a/test/integration/dynamic-routing/pages/p1/p2/all-ssg/[...rest].js +++ b/test/integration/dynamic-routing/pages/p1/p2/all-ssg/[...rest].js @@ -6,4 +6,10 @@ export function unstable_getStaticProps({ params }) { return { props: { params } } } +export function unstable_getStaticPaths() { + return { + paths: [], + } +} + export default All From 55132b0bfec9724835a802427491bce70e19ee18 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 20 Feb 2020 19:45:45 -0600 Subject: [PATCH 4/5] Update another test file --- .../pages/p1/p2/nested-all-ssg/[...rest]/index.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/integration/dynamic-routing/pages/p1/p2/nested-all-ssg/[...rest]/index.js b/test/integration/dynamic-routing/pages/p1/p2/nested-all-ssg/[...rest]/index.js index 0a44171410f32..cb57d820b359b 100644 --- a/test/integration/dynamic-routing/pages/p1/p2/nested-all-ssg/[...rest]/index.js +++ b/test/integration/dynamic-routing/pages/p1/p2/nested-all-ssg/[...rest]/index.js @@ -12,4 +12,10 @@ export function unstable_getStaticProps({ params }) { return { props: { params } } } +export function unstable_getStaticPaths() { + return { + paths: [], + } +} + export default All From 70d2b8c500b09a521fad947d5a4a6958b318083e Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Fri, 21 Feb 2020 00:38:19 -0500 Subject: [PATCH 5/5] Adjust --- packages/next/build/utils.ts | 7 ++++--- packages/next/next-server/server/render.tsx | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index adf28c726b44a..06330a8aa9829 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -547,14 +547,15 @@ export async function isPageStatic( // A page cannot have static parameters if it is not a dynamic page. if (hasStaticProps && hasStaticPaths && !pageIsDynamic) { throw new Error( - `unstable_getStaticPaths can only be used with dynamic pages ${page}. https://nextjs.org/docs#dynamic-routing` + `unstable_getStaticPaths can only be used with dynamic pages, not '${page}'.` + + `\nLearn more: https://nextjs.org/docs#dynamic-routing` ) } if (hasStaticProps && pageIsDynamic && !hasStaticPaths) { throw new Error( - `unstable_getStaticPaths is required for dynamic SSG pages and is missing for ${page}.\n` + - `See here for more info: https://err.sh/zeit/next.js/invalid-getstaticpaths-value` + `unstable_getStaticPaths is required for dynamic SSG pages and is missing for '${page}'.` + + `\nRead more: https://err.sh/next.js/invalid-getstaticpaths-value` ) } diff --git a/packages/next/next-server/server/render.tsx b/packages/next/next-server/server/render.tsx index bd42c4702864a..76cbe03516c6e 100644 --- a/packages/next/next-server/server/render.tsx +++ b/packages/next/next-server/server/render.tsx @@ -364,8 +364,8 @@ export async function renderToHTML( if (isSpr && pageIsDynamic && !unstable_getStaticPaths) { throw new Error( - `unstable_getStaticPaths is required for dynamic SSG pages and is missing for ${pathname}.\n` + - `See here for more info: https://err.sh/zeit/next.js/invalid-getstaticpaths-value` + `unstable_getStaticPaths is required for dynamic SSG pages and is missing for '${pathname}'.` + + `\nRead more: https://err.sh/next.js/invalid-getstaticpaths-value` ) }