rsnext/packages/next/server/static-paths-worker.ts
JJ Kasper b2d1d87e7f
Add initial changes for i18n support (#17370)
This adds the initial changes outlined in the [i18n routing RFC](https://github.com/vercel/next.js/discussions/17078). This currently treats the locale prefix on routes similar to how the basePath is treated in that the config doesn't require any changes to your pages directory and is automatically stripped/added based on the detected locale that should be used. 

Currently redirecting occurs on the `/` route if a locale is detected regardless of if an optional catch-all route would match the `/` route or not we may want to investigate whether we want to disable this redirection automatically if an `/index.js` file isn't present at root of the pages directory. 

TODO: 

- [x] ensure locale detection/populating works in serverless mode correctly
- [x] add tests for locale handling in different modes, fallback/getStaticProps/getServerSideProps

To be continued in fall-up PRs

- [ ] add tests for revalidate, auto-export, basePath + i18n
- [ ] add mapping of domains with locales
- [ ] investigate detecting locale against non-index routes and populating the locale in a cookie

x-ref: https://github.com/vercel/next.js/issues/17110
2020-10-07 21:11:01 +00:00

46 lines
1.2 KiB
TypeScript

import { buildStaticPaths } from '../build/utils'
import { loadComponents } from '../next-server/server/load-components'
import '../next-server/server/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,
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('./../next-server/lib/runtime-config').setConfig(config)
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,
locales,
defaultLocale
)
}