rsnext/packages/next/server/dev/static-paths-worker.ts
Steven 6f3e9470a6
Ensure error message prints next.config.mjs (#30152)
This PR ensures we print the correct error message for either `next.config.js` or `next.config.mjs`, whichever was detected.
2021-10-21 23:04:40 +00:00

51 lines
1.4 KiB
TypeScript

import { buildStaticPaths } from '../../build/utils'
import { setHttpAgentOptions } from '../config'
import { NextConfigComplete } from '../config-shared'
import { loadComponents } from '../load-components'
import '../node-polyfill-fetch'
type RuntimeConfig = any
let workerWasUsed = false
// 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,
pathname: string,
serverless: boolean,
config: RuntimeConfig,
httpAgentOptions: NextConfigComplete['httpAgentOptions'],
locales?: string[],
defaultLocale?: string
) {
// we only want to use each worker once to prevent any invalid
// caches
if (workerWasUsed) {
process.exit(1)
}
// update work memory runtime-config
require('../../shared/lib/runtime-config').setConfig(config)
setHttpAgentOptions(httpAgentOptions)
const components = await loadComponents(distDir, pathname, serverless)
if (!components.getStaticPaths) {
// we shouldn't get to this point since the worker should
// only be called for SSG pages with getStaticPaths
throw new Error(
`Invariant: failed to load page with getStaticPaths for ${pathname}`
)
}
workerWasUsed = true
return buildStaticPaths(
pathname,
components.getStaticPaths,
config.configFileName,
locales,
defaultLocale
)
}