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

// store initial require modules so we don't clear them below
const initialCache = new Set(Object.keys(require.cache))
6 7 8 9 10 11 12 13 14 15 16 17 18

// 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
) {
  // we need to clear any modules manually here since the
  // require-cache-hot-loader doesn't affect require cache here
  // since we're in a separate process
19 20 21 22 23
  Object.keys(require.cache).forEach(mod => {
    if (!initialCache.has(mod)) {
      delete require.cache[mod]
    }
  })
24 25 26 27 28 29 30 31

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

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

40
  return buildStaticPaths(pathname, components.getStaticPaths)
41
}