static-paths-worker.ts 1.0 KB
Newer Older
1 2
import { buildStaticPaths } from '../build/utils'
import { loadComponents } from '../next-server/server/load-components'
3
import '../next-server/server/node-polyfill-fetch'
4

5
let workerWasUsed = false
6 7 8 9 10 11 12 13 14 15

// 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
) {
16 17 18 19 20
  // we only want to use each worker once to prevent any invalid
  // caches
  if (workerWasUsed) {
    process.exit(1)
  }
21 22 23 24 25 26 27 28

  const components = await loadComponents(
    distDir,
    buildId,
    pathname,
    serverless
  )

29
  if (!components.getStaticPaths) {
30 31 32
    // we shouldn't get to this point since the worker should
    // only be called for SSG pages with getStaticPaths
    throw new Error(
33
      `Invariant: failed to load page with getStaticPaths for ${pathname}`
34 35 36
    )
  }

37
  workerWasUsed = true
38
  return buildStaticPaths(pathname, components.getStaticPaths)
39
}