Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warning for getStaticParams without getStaticProps #9226

Merged
merged 3 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
timneutkens marked this conversation as resolved.
Show resolved Hide resolved
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