未验证 提交 f5cb1a69 编写于 作者: J JJ Kasper 提交者: GitHub

Add error messages for dynamic SSG page without getStaticPaths (#10620)

* Add error messages for dynamic SSG page without getStaticPaths

* Update error check

* Update test file

* Update another test file

* Adjust
Co-authored-by: NJoe Haddad <timer150@gmail.com>
上级 95071730
......@@ -543,10 +543,19 @@ 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, 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}'.` +
`\nRead more: https://err.sh/next.js/invalid-getstaticpaths-value`
)
}
......
......@@ -317,6 +317,8 @@ export async function renderToHTML(
const hasPageGetInitialProps = !!(Component as any).getInitialProps
const pageIsDynamic = isDynamicRoute(pathname)
const isAutoExport =
!hasPageGetInitialProps &&
defaultAppGetInitialProps &&
......@@ -326,7 +328,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
......@@ -361,6 +363,13 @@ 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}'.` +
`\nRead more: https://err.sh/next.js/invalid-getstaticpaths-value`
)
}
if (dev) {
const { isValidElementType } = require('react-is')
if (!isValidElementType(Component)) {
......@@ -459,7 +468,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,
}
......
......@@ -6,4 +6,10 @@ export function unstable_getStaticProps({ params }) {
return { props: { params } }
}
export function unstable_getStaticPaths() {
return {
paths: [],
}
}
export default All
......@@ -12,4 +12,10 @@ export function unstable_getStaticProps({ params }) {
return { props: { params } }
}
export function unstable_getStaticPaths() {
return {
paths: [],
}
}
export default All
/* 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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册