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
This commit is contained in:
parent
34e0ce6f05
commit
2d9f199105
4 changed files with 40 additions and 2 deletions
|
@ -21,6 +21,7 @@ export type LoadComponentsReturnType = {
|
|||
props: any
|
||||
revalidate: number | false
|
||||
}
|
||||
unstable_getStaticParams?: () => void
|
||||
buildManifest?: any
|
||||
reactLoadableManifest?: any
|
||||
Document?: any
|
||||
|
@ -40,6 +41,7 @@ export async function loadComponents(
|
|||
Component,
|
||||
pageConfig: Component.config || {},
|
||||
unstable_getStaticProps: Component.unstable_getStaticProps,
|
||||
unstable_getStaticParams: Component.unstable_getStaticParams,
|
||||
}
|
||||
}
|
||||
const documentPath = join(
|
||||
|
@ -87,5 +89,6 @@ export async function loadComponents(
|
|||
reactLoadableManifest,
|
||||
pageConfig: ComponentMod.config || {},
|
||||
unstable_getStaticProps: ComponentMod.unstable_getStaticProps,
|
||||
unstable_getStaticParams: ComponentMod.unstable_getStaticParams,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -158,6 +158,7 @@ type RenderOpts = {
|
|||
props: any
|
||||
revalidate: number | false
|
||||
}
|
||||
unstable_getStaticParams?: () => void
|
||||
}
|
||||
|
||||
function renderDocument(
|
||||
|
@ -268,6 +269,7 @@ export async function renderToHTML(
|
|||
reactLoadableManifest,
|
||||
ErrorDebug,
|
||||
unstable_getStaticProps,
|
||||
unstable_getStaticParams,
|
||||
} = renderOpts
|
||||
|
||||
const isSpr = !!unstable_getStaticProps
|
||||
|
@ -283,6 +285,12 @@ export async function renderToHTML(
|
|||
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) {
|
||||
const { isValidElementType } = require('react-is')
|
||||
if (!isValidElementType(Component)) {
|
||||
|
|
|
@ -8,7 +8,7 @@ const glob = promisify(_glob)
|
|||
const exec = promisify(execOrig)
|
||||
|
||||
const NUM_RETRIES = 2
|
||||
const DEFAULT_CONCURRENCY = 3
|
||||
const DEFAULT_CONCURRENCY = 2
|
||||
|
||||
;(async () => {
|
||||
let concurrencyIdx = process.argv.indexOf('-c')
|
||||
|
|
|
@ -237,6 +237,29 @@ const runTests = (dev = false) => {
|
|||
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 {
|
||||
it('should should use correct caching headers for a no-revalidate page', async () => {
|
||||
const initialRes = await fetchViaHTTP(appPort, '/something')
|
||||
|
@ -370,7 +393,11 @@ describe('SPR Prerender', () => {
|
|||
describe('dev mode', () => {
|
||||
beforeAll(async () => {
|
||||
appPort = await findPort()
|
||||
app = await launchApp(appDir, appPort)
|
||||
app = await launchApp(appDir, appPort, {
|
||||
onStderr: msg => {
|
||||
stderr += msg
|
||||
}
|
||||
})
|
||||
buildId = 'development'
|
||||
})
|
||||
afterAll(() => killApp(app))
|
||||
|
|
Loading…
Reference in a new issue