2020-02-24 22:36:59 +01:00
|
|
|
import { buildStaticPaths } from '../build/utils'
|
|
|
|
import { loadComponents } from '../next-server/server/load-components'
|
2020-04-30 16:35:48 +02:00
|
|
|
import fetch from 'next/dist/compiled/node-fetch'
|
|
|
|
|
|
|
|
// @ts-ignore fetch exists globally
|
|
|
|
if (!global.fetch) {
|
|
|
|
// Polyfill fetch() in the Node.js environment
|
|
|
|
// @ts-ignore fetch exists globally
|
|
|
|
global.fetch = fetch
|
|
|
|
}
|
2020-03-05 19:48:09 +01:00
|
|
|
|
2020-03-25 19:57:14 +01:00
|
|
|
let workerWasUsed = false
|
2020-02-24 22:36:59 +01:00
|
|
|
|
|
|
|
// we call getStaticPaths in a separate process to ensure
|
|
|
|
// side-effects aren't relied on in dev that will break
|
|
|
|
// during a production build
|
|
|
|
export async function loadStaticPaths(
|
|
|
|
distDir: string,
|
|
|
|
buildId: string,
|
|
|
|
pathname: string,
|
|
|
|
serverless: boolean
|
|
|
|
) {
|
2020-03-25 19:57:14 +01:00
|
|
|
// we only want to use each worker once to prevent any invalid
|
|
|
|
// caches
|
|
|
|
if (workerWasUsed) {
|
|
|
|
process.exit(1)
|
|
|
|
}
|
2020-02-24 22:36:59 +01:00
|
|
|
|
|
|
|
const components = await loadComponents(
|
|
|
|
distDir,
|
|
|
|
buildId,
|
|
|
|
pathname,
|
|
|
|
serverless
|
|
|
|
)
|
|
|
|
|
2020-02-27 18:57:39 +01:00
|
|
|
if (!components.getStaticPaths) {
|
2020-02-24 22:36:59 +01:00
|
|
|
// we shouldn't get to this point since the worker should
|
|
|
|
// only be called for SSG pages with getStaticPaths
|
|
|
|
throw new Error(
|
2020-02-27 18:57:39 +01:00
|
|
|
`Invariant: failed to load page with getStaticPaths for ${pathname}`
|
2020-02-24 22:36:59 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-03-25 19:57:14 +01:00
|
|
|
workerWasUsed = true
|
2020-02-27 18:57:39 +01:00
|
|
|
return buildStaticPaths(pathname, components.getStaticPaths)
|
2020-02-24 22:36:59 +01:00
|
|
|
}
|