2021-11-28 17:48:43 +01:00
|
|
|
import type { GetStaticPaths } from 'next/types'
|
|
|
|
import type { NextConfigComplete } from '../config-shared'
|
|
|
|
import type { UnwrapPromise } from '../../lib/coalesced-function'
|
|
|
|
|
|
|
|
import '../node-polyfill-fetch'
|
2021-06-30 13:44:40 +02:00
|
|
|
import { buildStaticPaths } from '../../build/utils'
|
|
|
|
import { loadComponents } from '../load-components'
|
2021-11-28 17:48:43 +01:00
|
|
|
import { setHttpAgentOptions } from '../config'
|
2020-03-05 19:48:09 +01:00
|
|
|
|
2020-08-04 18:47:37 +02:00
|
|
|
type RuntimeConfig = any
|
|
|
|
|
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,
|
|
|
|
pathname: string,
|
2020-08-04 18:47:37 +02:00
|
|
|
serverless: boolean,
|
2020-10-07 23:11:01 +02:00
|
|
|
config: RuntimeConfig,
|
2021-08-03 02:38:42 +02:00
|
|
|
httpAgentOptions: NextConfigComplete['httpAgentOptions'],
|
2020-10-07 23:11:01 +02:00
|
|
|
locales?: string[],
|
|
|
|
defaultLocale?: string
|
2021-11-28 17:48:43 +01:00
|
|
|
): Promise<
|
|
|
|
Omit<UnwrapPromise<ReturnType<GetStaticPaths>>, 'paths'> & {
|
|
|
|
paths: string[]
|
|
|
|
encodedPaths: string[]
|
|
|
|
}
|
|
|
|
> {
|
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
|
|
|
|
2020-08-04 18:47:37 +02:00
|
|
|
// update work memory runtime-config
|
2021-06-30 13:44:40 +02:00
|
|
|
require('../../shared/lib/runtime-config').setConfig(config)
|
2021-08-03 02:38:42 +02:00
|
|
|
setHttpAgentOptions(httpAgentOptions)
|
2020-08-04 18:47:37 +02:00
|
|
|
|
2022-09-03 02:13:47 +02:00
|
|
|
const components = await loadComponents(
|
|
|
|
distDir,
|
|
|
|
pathname,
|
|
|
|
serverless,
|
|
|
|
false,
|
|
|
|
false
|
|
|
|
)
|
2020-02-24 22:36:59 +01:00
|
|
|
|
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-10-07 23:11:01 +02:00
|
|
|
return buildStaticPaths(
|
|
|
|
pathname,
|
|
|
|
components.getStaticPaths,
|
2021-10-22 01:04:40 +02:00
|
|
|
config.configFileName,
|
2020-10-07 23:11:01 +02:00
|
|
|
locales,
|
|
|
|
defaultLocale
|
|
|
|
)
|
2020-02-24 22:36:59 +01:00
|
|
|
}
|