2021-11-28 17:48:43 +01:00
|
|
|
import type { NextConfigComplete } from '../config-shared'
|
|
|
|
|
|
|
|
import '../node-polyfill-fetch'
|
2022-09-19 20:05:28 +02:00
|
|
|
import {
|
|
|
|
buildAppStaticPaths,
|
|
|
|
buildStaticPaths,
|
|
|
|
collectGenerateParams,
|
|
|
|
} from '../../build/utils'
|
2021-06-30 13:44:40 +02:00
|
|
|
import { loadComponents } from '../load-components'
|
2022-09-27 22:37:28 +02:00
|
|
|
import { setHttpClientAndAgentOptions } 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
|
2022-09-19 20:05:28 +02:00
|
|
|
export async function loadStaticPaths({
|
|
|
|
distDir,
|
|
|
|
pathname,
|
|
|
|
config,
|
|
|
|
httpAgentOptions,
|
2022-09-27 22:37:28 +02:00
|
|
|
enableUndici,
|
2022-09-19 20:05:28 +02:00
|
|
|
locales,
|
|
|
|
defaultLocale,
|
|
|
|
isAppPath,
|
|
|
|
originalAppPath,
|
|
|
|
}: {
|
|
|
|
distDir: string
|
|
|
|
pathname: string
|
|
|
|
config: RuntimeConfig
|
|
|
|
httpAgentOptions: NextConfigComplete['httpAgentOptions']
|
2022-09-27 22:37:28 +02:00
|
|
|
enableUndici: NextConfigComplete['enableUndici']
|
2022-09-19 20:05:28 +02:00
|
|
|
locales?: string[]
|
2020-10-07 23:11:01 +02:00
|
|
|
defaultLocale?: string
|
2022-09-19 20:05:28 +02:00
|
|
|
isAppPath?: boolean
|
|
|
|
originalAppPath?: string
|
|
|
|
}): Promise<{
|
|
|
|
paths?: string[]
|
|
|
|
encodedPaths?: string[]
|
|
|
|
fallback?: boolean | 'blocking'
|
|
|
|
}> {
|
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)
|
2022-09-27 22:37:28 +02:00
|
|
|
setHttpClientAndAgentOptions({
|
|
|
|
httpAgentOptions,
|
|
|
|
experimental: { enableUndici },
|
|
|
|
})
|
2020-08-04 18:47:37 +02:00
|
|
|
|
2022-09-09 00:17:15 +02:00
|
|
|
const components = await loadComponents({
|
2022-09-03 02:13:47 +02:00
|
|
|
distDir,
|
2022-09-19 20:05:28 +02:00
|
|
|
pathname: originalAppPath || pathname,
|
2022-09-09 00:17:15 +02:00
|
|
|
hasServerComponents: false,
|
2022-09-19 20:05:28 +02:00
|
|
|
isAppPath: !!isAppPath,
|
2022-09-09 00:17:15 +02:00
|
|
|
})
|
2020-02-24 22:36:59 +01:00
|
|
|
|
2022-09-19 20:05:28 +02:00
|
|
|
if (!components.getStaticPaths && !isAppPath) {
|
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
|
2022-09-19 20:05:28 +02:00
|
|
|
|
|
|
|
if (isAppPath) {
|
|
|
|
const generateParams = collectGenerateParams(components.ComponentMod.tree)
|
|
|
|
return buildAppStaticPaths({
|
|
|
|
page: pathname,
|
|
|
|
generateParams,
|
|
|
|
configFileName: config.configFileName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildStaticPaths({
|
|
|
|
page: pathname,
|
|
|
|
getStaticPaths: components.getStaticPaths,
|
|
|
|
configFileName: config.configFileName,
|
2020-10-07 23:11:01 +02:00
|
|
|
locales,
|
2022-09-19 20:05:28 +02:00
|
|
|
defaultLocale,
|
|
|
|
})
|
2020-02-24 22:36:59 +01:00
|
|
|
}
|