Commit 2d9f1991 authored by JJ Kasper's avatar JJ Kasper Committed by Joe Haddad

Add warning for getStaticParams without getStaticProps (#9226)

* Add warning for getStaticParams without getStaticProps

* Throw error instead of logging to make sure it's noticed

* Lower default concurrency for tests
parent 34e0ce6f
...@@ -21,6 +21,7 @@ export type LoadComponentsReturnType = { ...@@ -21,6 +21,7 @@ export type LoadComponentsReturnType = {
props: any props: any
revalidate: number | false revalidate: number | false
} }
unstable_getStaticParams?: () => void
buildManifest?: any buildManifest?: any
reactLoadableManifest?: any reactLoadableManifest?: any
Document?: any Document?: any
...@@ -40,6 +41,7 @@ export async function loadComponents( ...@@ -40,6 +41,7 @@ export async function loadComponents(
Component, Component,
pageConfig: Component.config || {}, pageConfig: Component.config || {},
unstable_getStaticProps: Component.unstable_getStaticProps, unstable_getStaticProps: Component.unstable_getStaticProps,
unstable_getStaticParams: Component.unstable_getStaticParams,
} }
} }
const documentPath = join( const documentPath = join(
...@@ -87,5 +89,6 @@ export async function loadComponents( ...@@ -87,5 +89,6 @@ export async function loadComponents(
reactLoadableManifest, reactLoadableManifest,
pageConfig: ComponentMod.config || {}, pageConfig: ComponentMod.config || {},
unstable_getStaticProps: ComponentMod.unstable_getStaticProps, unstable_getStaticProps: ComponentMod.unstable_getStaticProps,
unstable_getStaticParams: ComponentMod.unstable_getStaticParams,
} }
} }
...@@ -158,6 +158,7 @@ type RenderOpts = { ...@@ -158,6 +158,7 @@ type RenderOpts = {
props: any props: any
revalidate: number | false revalidate: number | false
} }
unstable_getStaticParams?: () => void
} }
function renderDocument( function renderDocument(
...@@ -268,6 +269,7 @@ export async function renderToHTML( ...@@ -268,6 +269,7 @@ export async function renderToHTML(
reactLoadableManifest, reactLoadableManifest,
ErrorDebug, ErrorDebug,
unstable_getStaticProps, unstable_getStaticProps,
unstable_getStaticParams,
} = renderOpts } = renderOpts
const isSpr = !!unstable_getStaticProps const isSpr = !!unstable_getStaticProps
...@@ -283,6 +285,12 @@ export async function renderToHTML( ...@@ -283,6 +285,12 @@ export async function renderToHTML(
throw new Error(SPR_GET_INITIAL_PROPS_CONFLICT + ` ${pathname}`) throw new Error(SPR_GET_INITIAL_PROPS_CONFLICT + ` ${pathname}`)
} }
if (!!unstable_getStaticParams && !isSpr) {
throw new Error(
`unstable_getStaticParams was added without a unstable_getStaticProps in ${pathname}. Without unstable_getStaticProps, unstable_getStaticParams does nothing`
)
}
if (dev) { if (dev) {
const { isValidElementType } = require('react-is') const { isValidElementType } = require('react-is')
if (!isValidElementType(Component)) { if (!isValidElementType(Component)) {
......
...@@ -8,7 +8,7 @@ const glob = promisify(_glob) ...@@ -8,7 +8,7 @@ const glob = promisify(_glob)
const exec = promisify(execOrig) const exec = promisify(execOrig)
const NUM_RETRIES = 2 const NUM_RETRIES = 2
const DEFAULT_CONCURRENCY = 3 const DEFAULT_CONCURRENCY = 2
;(async () => { ;(async () => {
let concurrencyIdx = process.argv.indexOf('-c') let concurrencyIdx = process.argv.indexOf('-c')
......
...@@ -237,6 +237,29 @@ const runTests = (dev = false) => { ...@@ -237,6 +237,29 @@ const runTests = (dev = false) => {
await fs.writeFile(indexPage, origContent) await fs.writeFile(indexPage, origContent)
} }
}) })
it('should show error when getStaticParams is used without getStaticProps', async () => {
const pagePath = join(appDir, 'pages/no-getStaticProps.js')
await fs.writeFile(
pagePath,
`
export async function unstable_getStaticParams() {
return []
}
export default () => 'hi'
`,
'utf8'
)
const html = await renderViaHTTP(appPort, '/no-getStaticProps')
await fs.remove(pagePath)
await waitFor(500)
expect(html).toMatch(
/unstable_getStaticParams was added without a unstable_getStaticProps in/
)
})
} else { } else {
it('should should use correct caching headers for a no-revalidate page', async () => { it('should should use correct caching headers for a no-revalidate page', async () => {
const initialRes = await fetchViaHTTP(appPort, '/something') const initialRes = await fetchViaHTTP(appPort, '/something')
...@@ -370,7 +393,11 @@ describe('SPR Prerender', () => { ...@@ -370,7 +393,11 @@ describe('SPR Prerender', () => {
describe('dev mode', () => { describe('dev mode', () => {
beforeAll(async () => { beforeAll(async () => {
appPort = await findPort() appPort = await findPort()
app = await launchApp(appDir, appPort) app = await launchApp(appDir, appPort, {
onStderr: msg => {
stderr += msg
}
})
buildId = 'development' buildId = 'development'
}) })
afterAll(() => killApp(app)) afterAll(() => killApp(app))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment