Skip to content

Commit

Permalink
Add warning for getStaticParams without getStaticProps (#9226)
Browse files Browse the repository at this point in the history
* Add warning for getStaticParams without getStaticProps

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

* Lower default concurrency for tests
  • Loading branch information
ijjk authored and Timer committed Oct 28, 2019
1 parent 34e0ce6 commit 2d9f199
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/next/next-server/server/load-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type LoadComponentsReturnType = {
props: any
revalidate: number | false
}
unstable_getStaticParams?: () => void
buildManifest?: any
reactLoadableManifest?: any
Document?: any
Expand All @@ -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(
Expand Down Expand Up @@ -87,5 +89,6 @@ export async function loadComponents(
reactLoadableManifest,
pageConfig: ComponentMod.config || {},
unstable_getStaticProps: ComponentMod.unstable_getStaticProps,
unstable_getStaticParams: ComponentMod.unstable_getStaticParams,
}
}
8 changes: 8 additions & 0 deletions packages/next/next-server/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ type RenderOpts = {
props: any
revalidate: number | false
}
unstable_getStaticParams?: () => void
}

function renderDocument(
Expand Down Expand Up @@ -268,6 +269,7 @@ export async function renderToHTML(
reactLoadableManifest,
ErrorDebug,
unstable_getStaticProps,
unstable_getStaticParams,
} = renderOpts

const isSpr = !!unstable_getStaticProps
Expand All @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion run-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
29 changes: 28 additions & 1 deletion test/integration/prerender/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 2d9f199

Please sign in to comment.