Unverified Commit b2d1d87e authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

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
parent 65881081
...@@ -97,6 +97,9 @@ export function createEntrypoints( ...@@ -97,6 +97,9 @@ export function createEntrypoints(
loadedEnvFiles: Buffer.from(JSON.stringify(loadedEnvFiles)).toString( loadedEnvFiles: Buffer.from(JSON.stringify(loadedEnvFiles)).toString(
'base64' 'base64'
), ),
i18n: config.experimental.i18n
? JSON.stringify(config.experimental.i18n)
: '',
} }
Object.keys(pages).forEach((page) => { Object.keys(pages).forEach((page) => {
......
...@@ -563,7 +563,9 @@ export default async function build( ...@@ -563,7 +563,9 @@ export default async function build(
let workerResult = await staticCheckWorkers.isPageStatic( let workerResult = await staticCheckWorkers.isPageStatic(
page, page,
serverBundle, serverBundle,
runtimeEnvConfig runtimeEnvConfig,
config.experimental.i18n?.locales,
config.experimental.i18n?.defaultLocale
) )
if (workerResult.isHybridAmp) { if (workerResult.isHybridAmp) {
......
...@@ -27,6 +27,7 @@ import { denormalizePagePath } from '../next-server/server/normalize-page-path' ...@@ -27,6 +27,7 @@ import { denormalizePagePath } from '../next-server/server/normalize-page-path'
import { BuildManifest } from '../next-server/server/get-page-files' import { BuildManifest } from '../next-server/server/get-page-files'
import { removePathTrailingSlash } from '../client/normalize-trailing-slash' import { removePathTrailingSlash } from '../client/normalize-trailing-slash'
import type { UnwrapPromise } from '../lib/coalesced-function' import type { UnwrapPromise } from '../lib/coalesced-function'
import { normalizeLocalePath } from '../next-server/lib/i18n/normalize-locale-path'
const fileGzipStats: { [k: string]: Promise<number> } = {} const fileGzipStats: { [k: string]: Promise<number> } = {}
const fsStatGzip = (file: string) => { const fsStatGzip = (file: string) => {
...@@ -530,7 +531,9 @@ export async function getJsPageSizeInKb( ...@@ -530,7 +531,9 @@ export async function getJsPageSizeInKb(
export async function buildStaticPaths( export async function buildStaticPaths(
page: string, page: string,
getStaticPaths: GetStaticPaths getStaticPaths: GetStaticPaths,
locales?: string[],
defaultLocale?: string
): Promise< ): Promise<
Omit<UnwrapPromise<ReturnType<GetStaticPaths>>, 'paths'> & { paths: string[] } Omit<UnwrapPromise<ReturnType<GetStaticPaths>>, 'paths'> & { paths: string[] }
> { > {
...@@ -595,7 +598,17 @@ export async function buildStaticPaths( ...@@ -595,7 +598,17 @@ export async function buildStaticPaths(
// route. // route.
if (typeof entry === 'string') { if (typeof entry === 'string') {
entry = removePathTrailingSlash(entry) entry = removePathTrailingSlash(entry)
const result = _routeMatcher(entry)
const localePathResult = normalizeLocalePath(entry, locales)
let cleanedEntry = entry
if (localePathResult.detectedLocale) {
cleanedEntry = entry.substr(localePathResult.detectedLocale.length + 1)
} else if (defaultLocale) {
entry = `/${defaultLocale}${entry}`
}
const result = _routeMatcher(cleanedEntry)
if (!result) { if (!result) {
throw new Error( throw new Error(
`The provided path \`${entry}\` does not match the page: \`${page}\`.` `The provided path \`${entry}\` does not match the page: \`${page}\`.`
...@@ -607,7 +620,10 @@ export async function buildStaticPaths( ...@@ -607,7 +620,10 @@ export async function buildStaticPaths(
// For the object-provided path, we must make sure it specifies all // For the object-provided path, we must make sure it specifies all
// required keys. // required keys.
else { else {
const invalidKeys = Object.keys(entry).filter((key) => key !== 'params') const invalidKeys = Object.keys(entry).filter(
(key) => key !== 'params' && key !== 'locale'
)
if (invalidKeys.length) { if (invalidKeys.length) {
throw new Error( throw new Error(
`Additional keys were returned from \`getStaticPaths\` in page "${page}". ` + `Additional keys were returned from \`getStaticPaths\` in page "${page}". ` +
...@@ -657,7 +673,14 @@ export async function buildStaticPaths( ...@@ -657,7 +673,14 @@ export async function buildStaticPaths(
.replace(/(?!^)\/$/, '') .replace(/(?!^)\/$/, '')
}) })
prerenderPaths?.add(builtPage) if (entry.locale && !locales?.includes(entry.locale)) {
throw new Error(
`Invalid locale returned from getStaticPaths for ${page}, the locale ${entry.locale} is not specified in next.config.js`
)
}
const curLocale = entry.locale || defaultLocale || ''
prerenderPaths?.add(`${curLocale ? `/${curLocale}` : ''}${builtPage}`)
} }
}) })
...@@ -667,7 +690,9 @@ export async function buildStaticPaths( ...@@ -667,7 +690,9 @@ export async function buildStaticPaths(
export async function isPageStatic( export async function isPageStatic(
page: string, page: string,
serverBundle: string, serverBundle: string,
runtimeEnvConfig: any runtimeEnvConfig: any,
locales?: string[],
defaultLocale?: string
): Promise<{ ): Promise<{
isStatic?: boolean isStatic?: boolean
isAmpOnly?: boolean isAmpOnly?: boolean
...@@ -755,7 +780,12 @@ export async function isPageStatic( ...@@ -755,7 +780,12 @@ export async function isPageStatic(
;({ ;({
paths: prerenderRoutes, paths: prerenderRoutes,
fallback: prerenderFallback, fallback: prerenderFallback,
} = await buildStaticPaths(page, mod.getStaticPaths)) } = await buildStaticPaths(
page,
mod.getStaticPaths,
locales,
defaultLocale
))
} }
const config = mod.config || {} const config = mod.config || {}
......
...@@ -986,6 +986,9 @@ export default async function getBaseWebpackConfig( ...@@ -986,6 +986,9 @@ export default async function getBaseWebpackConfig(
), ),
'process.env.__NEXT_ROUTER_BASEPATH': JSON.stringify(config.basePath), 'process.env.__NEXT_ROUTER_BASEPATH': JSON.stringify(config.basePath),
'process.env.__NEXT_HAS_REWRITES': JSON.stringify(hasRewrites), 'process.env.__NEXT_HAS_REWRITES': JSON.stringify(hasRewrites),
'process.env.__NEXT_i18n_SUPPORT': JSON.stringify(
!!config.experimental.i18n
),
...(isServer ...(isServer
? { ? {
// Fix bad-actors in the npm ecosystem (e.g. `node-formidable`) // Fix bad-actors in the npm ecosystem (e.g. `node-formidable`)
......
...@@ -28,6 +28,7 @@ export type ServerlessLoaderQuery = { ...@@ -28,6 +28,7 @@ export type ServerlessLoaderQuery = {
runtimeConfig: string runtimeConfig: string
previewProps: string previewProps: string
loadedEnvFiles: string loadedEnvFiles: string
i18n: string
} }
const vercelHeader = 'x-vercel-id' const vercelHeader = 'x-vercel-id'
...@@ -49,6 +50,7 @@ const nextServerlessLoader: loader.Loader = function () { ...@@ -49,6 +50,7 @@ const nextServerlessLoader: loader.Loader = function () {
runtimeConfig, runtimeConfig,
previewProps, previewProps,
loadedEnvFiles, loadedEnvFiles,
i18n,
}: ServerlessLoaderQuery = }: ServerlessLoaderQuery =
typeof this.query === 'string' ? parse(this.query.substr(1)) : this.query typeof this.query === 'string' ? parse(this.query.substr(1)) : this.query
...@@ -66,6 +68,8 @@ const nextServerlessLoader: loader.Loader = function () { ...@@ -66,6 +68,8 @@ const nextServerlessLoader: loader.Loader = function () {
JSON.parse(previewProps) as __ApiPreviewProps JSON.parse(previewProps) as __ApiPreviewProps
) )
const i18nEnabled = !!i18n
const defaultRouteRegex = pageIsDynamicRoute const defaultRouteRegex = pageIsDynamicRoute
? ` ? `
const defaultRouteRegex = getRouteRegex("${page}") const defaultRouteRegex = getRouteRegex("${page}")
...@@ -212,6 +216,58 @@ const nextServerlessLoader: loader.Loader = function () { ...@@ -212,6 +216,58 @@ const nextServerlessLoader: loader.Loader = function () {
` `
: '' : ''
const handleLocale = i18nEnabled
? `
// get pathname from URL with basePath stripped for locale detection
const i18n = ${i18n}
const accept = require('@hapi/accept')
const { detectLocaleCookie } = require('next/dist/next-server/lib/i18n/detect-locale-cookie')
const { normalizeLocalePath } = require('next/dist/next-server/lib/i18n/normalize-locale-path')
let detectedLocale = detectLocaleCookie(req, i18n.locales)
if (!detectedLocale) {
detectedLocale = accept.language(
req.headers['accept-language'],
i18n.locales
) || i18n.defaultLocale
}
if (
!nextStartMode &&
i18n.localeDetection !== false &&
denormalizePagePath(parsedUrl.pathname || '/') === '/'
) {
res.setHeader(
'Location',
formatUrl({
// make sure to include any query values when redirecting
...parsedUrl,
pathname: \`/\${detectedLocale}\`,
})
)
res.statusCode = 307
res.end()
}
// TODO: domain based locales (domain to locale mapping needs to be provided in next.config.js)
const localePathResult = normalizeLocalePath(parsedUrl.pathname, i18n.locales)
if (localePathResult.detectedLocale) {
detectedLocale = localePathResult.detectedLocale
req.url = formatUrl({
...parsedUrl,
pathname: localePathResult.pathname,
})
parsedUrl.pathname = localePathResult.pathname
}
detectedLocale = detectedLocale || i18n.defaultLocale
`
: `
const i18n = {}
const detectedLocale = undefined
`
if (page.match(API_ROUTE)) { if (page.match(API_ROUTE)) {
return ` return `
import initServer from 'next-plugin-loader?middleware=on-init-server!' import initServer from 'next-plugin-loader?middleware=on-init-server!'
...@@ -305,6 +361,7 @@ const nextServerlessLoader: loader.Loader = function () { ...@@ -305,6 +361,7 @@ const nextServerlessLoader: loader.Loader = function () {
const { renderToHTML } = require('next/dist/next-server/server/render'); const { renderToHTML } = require('next/dist/next-server/server/render');
const { tryGetPreviewData } = require('next/dist/next-server/server/api-utils'); const { tryGetPreviewData } = require('next/dist/next-server/server/api-utils');
const { denormalizePagePath } = require('next/dist/next-server/server/denormalize-page-path') const { denormalizePagePath } = require('next/dist/next-server/server/denormalize-page-path')
const { setLazyProp, getCookieParser } = require('next/dist/next-server/server/api-utils')
const {sendPayload} = require('next/dist/next-server/server/send-payload'); const {sendPayload} = require('next/dist/next-server/server/send-payload');
const buildManifest = require('${buildManifest}'); const buildManifest = require('${buildManifest}');
const reactLoadableManifest = require('${reactLoadableManifest}'); const reactLoadableManifest = require('${reactLoadableManifest}');
...@@ -338,6 +395,9 @@ const nextServerlessLoader: loader.Loader = function () { ...@@ -338,6 +395,9 @@ const nextServerlessLoader: loader.Loader = function () {
export const _app = App export const _app = App
export async function renderReqToHTML(req, res, renderMode, _renderOpts, _params) { export async function renderReqToHTML(req, res, renderMode, _renderOpts, _params) {
const fromExport = renderMode === 'export' || renderMode === true; const fromExport = renderMode === 'export' || renderMode === true;
const nextStartMode = renderMode === 'passthrough'
setLazyProp({ req }, 'cookies', getCookieParser(req))
const options = { const options = {
App, App,
...@@ -388,12 +448,16 @@ const nextServerlessLoader: loader.Loader = function () { ...@@ -388,12 +448,16 @@ const nextServerlessLoader: loader.Loader = function () {
routeNoAssetPath = parsedUrl.pathname routeNoAssetPath = parsedUrl.pathname
} }
${handleLocale}
const renderOpts = Object.assign( const renderOpts = Object.assign(
{ {
Component, Component,
pageConfig: config, pageConfig: config,
nextExport: fromExport, nextExport: fromExport,
isDataReq: _nextData, isDataReq: _nextData,
locale: detectedLocale,
locales: i18n.locales,
}, },
options, options,
) )
......
...@@ -11,7 +11,11 @@ import type { ...@@ -11,7 +11,11 @@ import type {
AppProps, AppProps,
PrivateRouteInfo, PrivateRouteInfo,
} from '../next-server/lib/router/router' } from '../next-server/lib/router/router'
import { delBasePath, hasBasePath } from '../next-server/lib/router/router' import {
delBasePath,
hasBasePath,
delLocale,
} from '../next-server/lib/router/router'
import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic' import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic'
import * as querystring from '../next-server/lib/router/utils/querystring' import * as querystring from '../next-server/lib/router/utils/querystring'
import * as envConfig from '../next-server/lib/runtime-config' import * as envConfig from '../next-server/lib/runtime-config'
...@@ -60,8 +64,11 @@ const { ...@@ -60,8 +64,11 @@ const {
dynamicIds, dynamicIds,
isFallback, isFallback,
head: initialHeadData, head: initialHeadData,
locales,
} = data } = data
let { locale } = data
const prefix = assetPrefix || '' const prefix = assetPrefix || ''
// With dynamic assetPrefix it's no longer possible to set assetPrefix at the build time // With dynamic assetPrefix it's no longer possible to set assetPrefix at the build time
...@@ -80,6 +87,23 @@ if (hasBasePath(asPath)) { ...@@ -80,6 +87,23 @@ if (hasBasePath(asPath)) {
asPath = delBasePath(asPath) asPath = delBasePath(asPath)
} }
asPath = delLocale(asPath, locale)
if (process.env.__NEXT_i18n_SUPPORT) {
const {
normalizeLocalePath,
} = require('../next-server/lib/i18n/normalize-locale-path')
if (isFallback && locales) {
const localePathResult = normalizeLocalePath(asPath, locales)
if (localePathResult.detectedLocale) {
asPath = asPath.substr(localePathResult.detectedLocale.length + 1)
locale = localePathResult.detectedLocale
}
}
}
type RegisterFn = (input: [string, () => void]) => void type RegisterFn = (input: [string, () => void]) => void
const pageLoader = new PageLoader(buildId, prefix, page) const pageLoader = new PageLoader(buildId, prefix, page)
...@@ -291,6 +315,8 @@ export default async (opts: { webpackHMR?: any } = {}) => { ...@@ -291,6 +315,8 @@ export default async (opts: { webpackHMR?: any } = {}) => {
isFallback: Boolean(isFallback), isFallback: Boolean(isFallback),
subscription: ({ Component, styleSheets, props, err }, App) => subscription: ({ Component, styleSheets, props, err }, App) =>
render({ App, Component, styleSheets, props, err }), render({ App, Component, styleSheets, props, err }),
locale,
locales,
}) })
// call init-client middleware // call init-client middleware
......
...@@ -2,6 +2,7 @@ import React, { Children } from 'react' ...@@ -2,6 +2,7 @@ import React, { Children } from 'react'
import { UrlObject } from 'url' import { UrlObject } from 'url'
import { import {
addBasePath, addBasePath,
addLocale,
isLocalURL, isLocalURL,
NextRouter, NextRouter,
PrefetchOptions, PrefetchOptions,
...@@ -331,7 +332,7 @@ function Link(props: React.PropsWithChildren<LinkProps>) { ...@@ -331,7 +332,7 @@ function Link(props: React.PropsWithChildren<LinkProps>) {
// If child is an <a> tag and doesn't have a href attribute, or if the 'passHref' property is // If child is an <a> tag and doesn't have a href attribute, or if the 'passHref' property is
// defined, we specify the current 'href', so that repetition is not needed by the user // defined, we specify the current 'href', so that repetition is not needed by the user
if (props.passHref || (child.type === 'a' && !('href' in child.props))) { if (props.passHref || (child.type === 'a' && !('href' in child.props))) {
childProps.href = addBasePath(as) childProps.href = addBasePath(addLocale(as, router && router.locale))
} }
return React.cloneElement(child, childProps) return React.cloneElement(child, childProps)
......
...@@ -7,6 +7,7 @@ import { ...@@ -7,6 +7,7 @@ import {
addBasePath, addBasePath,
markLoadingError, markLoadingError,
interpolateAs, interpolateAs,
addLocale,
} from '../next-server/lib/router/router' } from '../next-server/lib/router/router'
import getAssetPathFromRoute from '../next-server/lib/router/utils/get-asset-path-from-route' import getAssetPathFromRoute from '../next-server/lib/router/utils/get-asset-path-from-route'
...@@ -202,13 +203,13 @@ export default class PageLoader { ...@@ -202,13 +203,13 @@ export default class PageLoader {
* @param {string} href the route href (file-system path) * @param {string} href the route href (file-system path)
* @param {string} asPath the URL as shown in browser (virtual path); used for dynamic routes * @param {string} asPath the URL as shown in browser (virtual path); used for dynamic routes
*/ */
getDataHref(href: string, asPath: string, ssg: boolean) { getDataHref(href: string, asPath: string, ssg: boolean, locale?: string) {
const { pathname: hrefPathname, query, search } = parseRelativeUrl(href) const { pathname: hrefPathname, query, search } = parseRelativeUrl(href)
const { pathname: asPathname } = parseRelativeUrl(asPath) const { pathname: asPathname } = parseRelativeUrl(asPath)
const route = normalizeRoute(hrefPathname) const route = normalizeRoute(hrefPathname)
const getHrefForSlug = (path: string) => { const getHrefForSlug = (path: string) => {
const dataRoute = getAssetPathFromRoute(path, '.json') const dataRoute = addLocale(getAssetPathFromRoute(path, '.json'), locale)
return addBasePath( return addBasePath(
`/_next/data/${this.buildId}${dataRoute}${ssg ? '' : search}` `/_next/data/${this.buildId}${dataRoute}${ssg ? '' : search}`
) )
......
...@@ -37,6 +37,8 @@ const urlPropertyFields = [ ...@@ -37,6 +37,8 @@ const urlPropertyFields = [
'components', 'components',
'isFallback', 'isFallback',
'basePath', 'basePath',
'locale',
'locales',
] ]
const routerEvents = [ const routerEvents = [
'routeChangeStart', 'routeChangeStart',
...@@ -144,7 +146,10 @@ export function makePublicRouterInstance(router: Router): NextRouter { ...@@ -144,7 +146,10 @@ export function makePublicRouterInstance(router: Router): NextRouter {
for (const property of urlPropertyFields) { for (const property of urlPropertyFields) {
if (typeof _router[property] === 'object') { if (typeof _router[property] === 'object') {
instance[property] = Object.assign({}, _router[property]) // makes sure query is not stateful instance[property] = Object.assign(
Array.isArray(_router[property]) ? [] : {},
_router[property]
) // makes sure query is not stateful
continue continue
} }
......
...@@ -298,6 +298,8 @@ export default async function exportApp( ...@@ -298,6 +298,8 @@ export default async function exportApp(
ampValidatorPath: nextConfig.experimental.amp?.validator || undefined, ampValidatorPath: nextConfig.experimental.amp?.validator || undefined,
ampSkipValidation: nextConfig.experimental.amp?.skipValidation || false, ampSkipValidation: nextConfig.experimental.amp?.skipValidation || false,
ampOptimizerConfig: nextConfig.experimental.amp?.optimizer || undefined, ampOptimizerConfig: nextConfig.experimental.amp?.optimizer || undefined,
locales: nextConfig.experimental.i18n?.locales,
locale: nextConfig.experimental.i18n?.defaultLocale,
} }
const { serverRuntimeConfig, publicRuntimeConfig } = nextConfig const { serverRuntimeConfig, publicRuntimeConfig } = nextConfig
......
...@@ -15,6 +15,7 @@ import { ComponentType } from 'react' ...@@ -15,6 +15,7 @@ import { ComponentType } from 'react'
import { GetStaticProps } from '../types' import { GetStaticProps } from '../types'
import { requireFontManifest } from '../next-server/server/require' import { requireFontManifest } from '../next-server/server/require'
import { FontManifest } from '../next-server/server/font-utils' import { FontManifest } from '../next-server/server/font-utils'
import { normalizeLocalePath } from '../next-server/lib/i18n/normalize-locale-path'
const envConfig = require('../next-server/lib/runtime-config') const envConfig = require('../next-server/lib/runtime-config')
...@@ -67,6 +68,8 @@ interface RenderOpts { ...@@ -67,6 +68,8 @@ interface RenderOpts {
optimizeFonts?: boolean optimizeFonts?: boolean
optimizeImages?: boolean optimizeImages?: boolean
fontManifest?: FontManifest fontManifest?: FontManifest
locales?: string[]
locale?: string
} }
type ComponentModule = ComponentType<{}> & { type ComponentModule = ComponentType<{}> & {
...@@ -100,6 +103,13 @@ export default async function exportPage({ ...@@ -100,6 +103,13 @@ export default async function exportPage({
let query = { ...originalQuery } let query = { ...originalQuery }
let params: { [key: string]: string | string[] } | undefined let params: { [key: string]: string | string[] } | undefined
const localePathResult = normalizeLocalePath(path, renderOpts.locales)
if (localePathResult.detectedLocale) {
path = localePathResult.pathname
renderOpts.locale = localePathResult.detectedLocale
}
// We need to show a warning if they try to provide query values // We need to show a warning if they try to provide query values
// for an auto-exported page since they won't be available // for an auto-exported page since they won't be available
const hasOrigQueryValues = Object.keys(originalQuery).length > 0 const hasOrigQueryValues = Object.keys(originalQuery).length > 0
...@@ -229,6 +239,8 @@ export default async function exportPage({ ...@@ -229,6 +239,8 @@ export default async function exportPage({
fontManifest: optimizeFonts fontManifest: optimizeFonts
? requireFontManifest(distDir, serverless) ? requireFontManifest(distDir, serverless)
: null, : null,
locale: renderOpts.locale!,
locales: renderOpts.locales!,
}, },
// @ts-ignore // @ts-ignore
params params
......
import { IncomingMessage } from 'http'
export function detectLocaleCookie(req: IncomingMessage, locales: string[]) {
let detectedLocale: string | undefined
if (req.headers.cookie && req.headers.cookie.includes('NEXT_LOCALE')) {
const { NEXT_LOCALE } = (req as any).cookies
if (locales.some((locale: string) => NEXT_LOCALE === locale)) {
detectedLocale = NEXT_LOCALE
}
}
return detectedLocale
}
export function normalizeLocalePath(
pathname: string,
locales?: string[]
): {
detectedLocale?: string
pathname: string
} {
let detectedLocale: string | undefined
;(locales || []).some((locale) => {
if (pathname.startsWith(`/${locale}`)) {
detectedLocale = locale
pathname = pathname.replace(new RegExp(`^/${locale}`), '') || '/'
return true
}
return false
})
return {
pathname,
detectedLocale,
}
}
...@@ -47,17 +47,39 @@ function buildCancellationError() { ...@@ -47,17 +47,39 @@ function buildCancellationError() {
}) })
} }
function addPathPrefix(path: string, prefix?: string) {
return prefix && path.startsWith('/')
? path === '/'
? normalizePathTrailingSlash(prefix)
: `${prefix}${path}`
: path
}
export function addLocale(path: string, locale?: string) {
if (process.env.__NEXT_i18n_SUPPORT) {
return locale && !path.startsWith('/' + locale)
? addPathPrefix(path, '/' + locale)
: path
}
return path
}
export function delLocale(path: string, locale?: string) {
if (process.env.__NEXT_i18n_SUPPORT) {
return locale && path.startsWith('/' + locale)
? path.substr(locale.length + 1) || '/'
: path
}
return path
}
export function hasBasePath(path: string): boolean { export function hasBasePath(path: string): boolean {
return path === basePath || path.startsWith(basePath + '/') return path === basePath || path.startsWith(basePath + '/')
} }
export function addBasePath(path: string): string { export function addBasePath(path: string): string {
// we only add the basepath on relative urls // we only add the basepath on relative urls
return basePath && path.startsWith('/') return addPathPrefix(path, basePath)
? path === '/'
? normalizePathTrailingSlash(basePath)
: basePath + path
: path
} }
export function delBasePath(path: string): string { export function delBasePath(path: string): string {
...@@ -222,6 +244,8 @@ export type BaseRouter = { ...@@ -222,6 +244,8 @@ export type BaseRouter = {
query: ParsedUrlQuery query: ParsedUrlQuery
asPath: string asPath: string
basePath: string basePath: string
locale?: string
locales?: string[]
} }
export type NextRouter = BaseRouter & export type NextRouter = BaseRouter &
...@@ -330,6 +354,8 @@ export default class Router implements BaseRouter { ...@@ -330,6 +354,8 @@ export default class Router implements BaseRouter {
isFallback: boolean isFallback: boolean
_inFlightRoute?: string _inFlightRoute?: string
_shallow?: boolean _shallow?: boolean
locale?: string
locales?: string[]
static events: MittEmitter = mitt() static events: MittEmitter = mitt()
...@@ -347,6 +373,8 @@ export default class Router implements BaseRouter { ...@@ -347,6 +373,8 @@ export default class Router implements BaseRouter {
err, err,
subscription, subscription,
isFallback, isFallback,
locale,
locales,
}: { }: {
subscription: Subscription subscription: Subscription
initialProps: any initialProps: any
...@@ -357,6 +385,8 @@ export default class Router implements BaseRouter { ...@@ -357,6 +385,8 @@ export default class Router implements BaseRouter {
wrapApp: (App: AppComponent) => any wrapApp: (App: AppComponent) => any
err?: Error err?: Error
isFallback: boolean isFallback: boolean
locale?: string
locales?: string[]
} }
) { ) {
// represents the current component key // represents the current component key
...@@ -407,6 +437,11 @@ export default class Router implements BaseRouter { ...@@ -407,6 +437,11 @@ export default class Router implements BaseRouter {
this.isFallback = isFallback this.isFallback = isFallback
if (process.env.__NEXT_i18n_SUPPORT) {
this.locale = locale
this.locales = locales
}
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
// make sure "as" doesn't start with double slashes or else it can // make sure "as" doesn't start with double slashes or else it can
// throw an error as it's considered invalid // throw an error as it's considered invalid
...@@ -561,7 +596,11 @@ export default class Router implements BaseRouter { ...@@ -561,7 +596,11 @@ export default class Router implements BaseRouter {
this.abortComponentLoad(this._inFlightRoute) this.abortComponentLoad(this._inFlightRoute)
} }
const cleanedAs = hasBasePath(as) ? delBasePath(as) : as as = addLocale(as, this.locale)
const cleanedAs = delLocale(
hasBasePath(as) ? delBasePath(as) : as,
this.locale
)
this._inFlightRoute = as this._inFlightRoute = as
// If the url change is only related to a hash change // If the url change is only related to a hash change
...@@ -650,7 +689,7 @@ export default class Router implements BaseRouter { ...@@ -650,7 +689,7 @@ export default class Router implements BaseRouter {
} }
} }
} }
resolvedAs = delBasePath(resolvedAs) resolvedAs = delLocale(delBasePath(resolvedAs), this.locale)
if (isDynamicRoute(route)) { if (isDynamicRoute(route)) {
const parsedAs = parseRelativeUrl(resolvedAs) const parsedAs = parseRelativeUrl(resolvedAs)
...@@ -751,7 +790,7 @@ export default class Router implements BaseRouter { ...@@ -751,7 +790,7 @@ export default class Router implements BaseRouter {
} }
Router.events.emit('beforeHistoryChange', as) Router.events.emit('beforeHistoryChange', as)
this.changeState(method, url, as, options) this.changeState(method, url, addLocale(as, this.locale), options)
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
const appComp: any = this.components['/_app'].Component const appComp: any = this.components['/_app'].Component
...@@ -920,7 +959,8 @@ export default class Router implements BaseRouter { ...@@ -920,7 +959,8 @@ export default class Router implements BaseRouter {
dataHref = this.pageLoader.getDataHref( dataHref = this.pageLoader.getDataHref(
formatWithValidation({ pathname, query }), formatWithValidation({ pathname, query }),
delBasePath(as), delBasePath(as),
__N_SSG __N_SSG,
this.locale
) )
} }
......
...@@ -101,6 +101,8 @@ export type NEXT_DATA = { ...@@ -101,6 +101,8 @@ export type NEXT_DATA = {
gip?: boolean gip?: boolean
appGip?: boolean appGip?: boolean
head: HeadEntry[] head: HeadEntry[]
locale?: string
locales?: string[]
} }
/** /**
...@@ -186,6 +188,7 @@ export type DocumentProps = DocumentInitialProps & { ...@@ -186,6 +188,7 @@ export type DocumentProps = DocumentInitialProps & {
headTags: any[] headTags: any[]
unstable_runtimeJS?: false unstable_runtimeJS?: false
devOnlyCacheBusterQueryString: string devOnlyCacheBusterQueryString: string
locale?: string
} }
/** /**
......
...@@ -54,6 +54,7 @@ const defaultConfig: { [key: string]: any } = { ...@@ -54,6 +54,7 @@ const defaultConfig: { [key: string]: any } = {
optimizeFonts: false, optimizeFonts: false,
optimizeImages: false, optimizeImages: false,
scrollRestoration: false, scrollRestoration: false,
i18n: false,
}, },
future: { future: {
excludeDefaultMomentLocales: false, excludeDefaultMomentLocales: false,
...@@ -206,6 +207,44 @@ function assignDefaults(userConfig: { [key: string]: any }) { ...@@ -206,6 +207,44 @@ function assignDefaults(userConfig: { [key: string]: any }) {
} }
} }
if (result.experimental?.i18n) {
const { i18n } = result.experimental
const i18nType = typeof i18n
if (i18nType !== 'object') {
throw new Error(`Specified i18n should be an object received ${i18nType}`)
}
if (!Array.isArray(i18n.locales)) {
throw new Error(
`Specified i18n.locales should be an Array received ${typeof i18n.lcoales}`
)
}
const defaultLocaleType = typeof i18n.defaultLocale
if (!i18n.defaultLocale || defaultLocaleType !== 'string') {
throw new Error(`Specified i18n.defaultLocale should be a string`)
}
if (!i18n.locales.includes(i18n.defaultLocale)) {
throw new Error(
`Specified i18n.defaultLocale should be included in i18n.locales`
)
}
const localeDetectionType = typeof i18n.locales.localeDetection
if (
localeDetectionType !== 'boolean' &&
localeDetectionType !== 'undefined'
) {
throw new Error(
`Specified i18n.localeDetection should be undefined or a boolean received ${localeDetectionType}`
)
}
}
return result return result
} }
......
...@@ -40,7 +40,13 @@ import { ...@@ -40,7 +40,13 @@ import {
} from '../lib/router/utils' } from '../lib/router/utils'
import * as envConfig from '../lib/runtime-config' import * as envConfig from '../lib/runtime-config'
import { isResSent, NextApiRequest, NextApiResponse } from '../lib/utils' import { isResSent, NextApiRequest, NextApiResponse } from '../lib/utils'
import { apiResolver, tryGetPreviewData, __ApiPreviewProps } from './api-utils' import {
apiResolver,
setLazyProp,
getCookieParser,
tryGetPreviewData,
__ApiPreviewProps,
} from './api-utils'
import loadConfig, { isTargetLikeServerless } from './config' import loadConfig, { isTargetLikeServerless } from './config'
import pathMatch from '../lib/router/utils/path-match' import pathMatch from '../lib/router/utils/path-match'
import { recursiveReadDirSync } from './lib/recursive-readdir-sync' import { recursiveReadDirSync } from './lib/recursive-readdir-sync'
...@@ -69,6 +75,9 @@ import { removePathTrailingSlash } from '../../client/normalize-trailing-slash' ...@@ -69,6 +75,9 @@ import { removePathTrailingSlash } from '../../client/normalize-trailing-slash'
import getRouteFromAssetPath from '../lib/router/utils/get-route-from-asset-path' import getRouteFromAssetPath from '../lib/router/utils/get-route-from-asset-path'
import { FontManifest } from './font-utils' import { FontManifest } from './font-utils'
import { denormalizePagePath } from './denormalize-page-path' import { denormalizePagePath } from './denormalize-page-path'
import accept from '@hapi/accept'
import { normalizeLocalePath } from '../lib/i18n/normalize-locale-path'
import { detectLocaleCookie } from '../lib/i18n/detect-locale-cookie'
import * as Log from '../../build/output/log' import * as Log from '../../build/output/log'
const getCustomRouteMatcher = pathMatch(true) const getCustomRouteMatcher = pathMatch(true)
...@@ -129,6 +138,8 @@ export default class Server { ...@@ -129,6 +138,8 @@ export default class Server {
optimizeFonts: boolean optimizeFonts: boolean
fontManifest: FontManifest fontManifest: FontManifest
optimizeImages: boolean optimizeImages: boolean
locale?: string
locales?: string[]
} }
private compression?: Middleware private compression?: Middleware
private onErrorMiddleware?: ({ err }: { err: Error }) => Promise<void> private onErrorMiddleware?: ({ err }: { err: Error }) => Promise<void>
...@@ -181,6 +192,7 @@ export default class Server { ...@@ -181,6 +192,7 @@ export default class Server {
? requireFontManifest(this.distDir, this._isLikeServerless) ? requireFontManifest(this.distDir, this._isLikeServerless)
: null, : null,
optimizeImages: this.nextConfig.experimental.optimizeImages, optimizeImages: this.nextConfig.experimental.optimizeImages,
locales: this.nextConfig.experimental.i18n?.locales,
} }
// Only the `publicRuntimeConfig` key is exposed to the client side // Only the `publicRuntimeConfig` key is exposed to the client side
...@@ -267,6 +279,8 @@ export default class Server { ...@@ -267,6 +279,8 @@ export default class Server {
res: ServerResponse, res: ServerResponse,
parsedUrl?: UrlWithParsedQuery parsedUrl?: UrlWithParsedQuery
): Promise<void> { ): Promise<void> {
setLazyProp({ req: req as any }, 'cookies', getCookieParser(req))
// Parse url if parsedUrl not provided // Parse url if parsedUrl not provided
if (!parsedUrl || typeof parsedUrl !== 'object') { if (!parsedUrl || typeof parsedUrl !== 'object') {
const url: any = req.url const url: any = req.url
...@@ -279,6 +293,7 @@ export default class Server { ...@@ -279,6 +293,7 @@ export default class Server {
} }
const { basePath } = this.nextConfig const { basePath } = this.nextConfig
const { i18n } = this.nextConfig.experimental
if (basePath && req.url?.startsWith(basePath)) { if (basePath && req.url?.startsWith(basePath)) {
// store original URL to allow checking if basePath was // store original URL to allow checking if basePath was
...@@ -287,6 +302,48 @@ export default class Server { ...@@ -287,6 +302,48 @@ export default class Server {
req.url = req.url!.replace(basePath, '') || '/' req.url = req.url!.replace(basePath, '') || '/'
} }
if (i18n) {
// get pathname from URL with basePath stripped for locale detection
const { pathname, ...parsed } = parseUrl(req.url || '/')
let detectedLocale = detectLocaleCookie(req, i18n.locales)
if (!detectedLocale) {
detectedLocale =
accept.language(req.headers['accept-language'], i18n.locales) ||
i18n.defaultLocale
}
if (
i18n.localeDetection !== false &&
denormalizePagePath(pathname || '/') === '/'
) {
res.setHeader(
'Location',
formatUrl({
// make sure to include any query values when redirecting
...parsed,
pathname: `/${detectedLocale}`,
})
)
res.statusCode = 307
res.end()
}
// TODO: domain based locales (domain to locale mapping needs to be provided in next.config.js)
const localePathResult = normalizeLocalePath(pathname!, i18n.locales)
if (localePathResult.detectedLocale) {
detectedLocale = localePathResult.detectedLocale
req.url = formatUrl({
...parsed,
pathname: localePathResult.pathname,
})
parsedUrl.pathname = localePathResult.pathname
}
;(req as any)._nextLocale = detectedLocale || i18n.defaultLocale
}
res.statusCode = 200 res.statusCode = 200
try { try {
return await this.run(req, res, parsedUrl) return await this.run(req, res, parsedUrl)
...@@ -428,10 +485,25 @@ export default class Server { ...@@ -428,10 +485,25 @@ export default class Server {
} }
// re-create page's pathname // re-create page's pathname
const pathname = getRouteFromAssetPath( let pathname = `/${params.path.join('/')}`
`/${params.path.join('/')}`,
'.json' if (this.nextConfig.experimental.i18n) {
) const localePathResult = normalizeLocalePath(
pathname,
this.renderOpts.locales
)
let detectedLocale = detectLocaleCookie(
req,
this.renderOpts.locales!
)
if (localePathResult.detectedLocale) {
pathname = localePathResult.pathname
detectedLocale = localePathResult.detectedLocale
}
;(req as any)._nextLocale = detectedLocale
}
pathname = getRouteFromAssetPath(pathname, '.json')
const parsedUrl = parseUrl(pathname, true) const parsedUrl = parseUrl(pathname, true)
...@@ -1046,6 +1118,10 @@ export default class Server { ...@@ -1046,6 +1118,10 @@ export default class Server {
(path.split(this.buildId).pop() || '/').replace(/\.json$/, '') (path.split(this.buildId).pop() || '/').replace(/\.json$/, '')
) )
} }
if (this.nextConfig.experimental.i18n) {
return normalizeLocalePath(path, this.renderOpts.locales).pathname
}
return path return path
} }
...@@ -1056,10 +1132,14 @@ export default class Server { ...@@ -1056,10 +1132,14 @@ export default class Server {
urlPathname = stripNextDataPath(urlPathname) urlPathname = stripNextDataPath(urlPathname)
} }
const locale = (req as any)._nextLocale
const ssgCacheKey = const ssgCacheKey =
isPreviewMode || !isSSG isPreviewMode || !isSSG
? undefined // Preview mode bypasses the cache ? undefined // Preview mode bypasses the cache
: `${resolvedUrlPathname}${query.amp ? '.amp' : ''}` : `${locale ? `/${locale}` : ''}${resolvedUrlPathname}${
query.amp ? '.amp' : ''
}`
// Complete the response with cached data if its present // Complete the response with cached data if its present
const cachedData = ssgCacheKey const cachedData = ssgCacheKey
...@@ -1125,6 +1205,8 @@ export default class Server { ...@@ -1125,6 +1205,8 @@ export default class Server {
'passthrough', 'passthrough',
{ {
fontManifest: this.renderOpts.fontManifest, fontManifest: this.renderOpts.fontManifest,
locale: (req as any)._nextLocale,
locales: this.renderOpts.locales,
} }
) )
...@@ -1144,6 +1226,7 @@ export default class Server { ...@@ -1144,6 +1226,7 @@ export default class Server {
...opts, ...opts,
isDataReq, isDataReq,
resolvedUrl, resolvedUrl,
locale: (req as any)._nextLocale,
// For getServerSideProps we need to ensure we use the original URL // For getServerSideProps we need to ensure we use the original URL
// and not the resolved URL to prevent a hydration mismatch on // and not the resolved URL to prevent a hydration mismatch on
// asPath // asPath
...@@ -1208,7 +1291,11 @@ export default class Server { ...@@ -1208,7 +1291,11 @@ export default class Server {
// `getStaticPaths` // `getStaticPaths`
(isProduction || (isProduction ||
!staticPaths || !staticPaths ||
!staticPaths.includes(resolvedUrlPathname)) // static paths always includes locale so make sure it's prefixed
// with it
!staticPaths.includes(
`${locale ? '/' + locale : ''}${resolvedUrlPathname}`
))
) { ) {
if ( if (
// In development, fall through to render to handle missing // In development, fall through to render to handle missing
......
...@@ -67,6 +67,8 @@ class ServerRouter implements NextRouter { ...@@ -67,6 +67,8 @@ class ServerRouter implements NextRouter {
basePath: string basePath: string
events: any events: any
isFallback: boolean isFallback: boolean
locale?: string
locales?: string[]
// TODO: Remove in the next major version, as this would mean the user is adding event listeners in server-side `render` method // TODO: Remove in the next major version, as this would mean the user is adding event listeners in server-side `render` method
static events: MittEmitter = mitt() static events: MittEmitter = mitt()
...@@ -75,7 +77,9 @@ class ServerRouter implements NextRouter { ...@@ -75,7 +77,9 @@ class ServerRouter implements NextRouter {
query: ParsedUrlQuery, query: ParsedUrlQuery,
as: string, as: string,
{ isFallback }: { isFallback: boolean }, { isFallback }: { isFallback: boolean },
basePath: string basePath: string,
locale?: string,
locales?: string[]
) { ) {
this.route = pathname.replace(/\/$/, '') || '/' this.route = pathname.replace(/\/$/, '') || '/'
this.pathname = pathname this.pathname = pathname
...@@ -83,6 +87,8 @@ class ServerRouter implements NextRouter { ...@@ -83,6 +87,8 @@ class ServerRouter implements NextRouter {
this.asPath = as this.asPath = as
this.isFallback = isFallback this.isFallback = isFallback
this.basePath = basePath this.basePath = basePath
this.locale = locale
this.locales = locales
} }
push(): any { push(): any {
noRouter() noRouter()
...@@ -156,6 +162,8 @@ export type RenderOptsPartial = { ...@@ -156,6 +162,8 @@ export type RenderOptsPartial = {
devOnlyCacheBusterQueryString?: string devOnlyCacheBusterQueryString?: string
resolvedUrl?: string resolvedUrl?: string
resolvedAsPath?: string resolvedAsPath?: string
locale?: string
locales?: string[]
} }
export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial
...@@ -193,6 +201,8 @@ function renderDocument( ...@@ -193,6 +201,8 @@ function renderDocument(
appGip, appGip,
unstable_runtimeJS, unstable_runtimeJS,
devOnlyCacheBusterQueryString, devOnlyCacheBusterQueryString,
locale,
locales,
}: RenderOpts & { }: RenderOpts & {
props: any props: any
docComponentsRendered: DocumentProps['docComponentsRendered'] docComponentsRendered: DocumentProps['docComponentsRendered']
...@@ -239,6 +249,8 @@ function renderDocument( ...@@ -239,6 +249,8 @@ function renderDocument(
customServer, // whether the user is using a custom server customServer, // whether the user is using a custom server
gip, // whether the page has getInitialProps gip, // whether the page has getInitialProps
appGip, // whether the _app has getInitialProps appGip, // whether the _app has getInitialProps
locale,
locales,
head: React.Children.toArray(docProps.head || []) head: React.Children.toArray(docProps.head || [])
.map((elem) => { .map((elem) => {
const { children } = elem?.props const { children } = elem?.props
...@@ -269,6 +281,7 @@ function renderDocument( ...@@ -269,6 +281,7 @@ function renderDocument(
headTags, headTags,
unstable_runtimeJS, unstable_runtimeJS,
devOnlyCacheBusterQueryString, devOnlyCacheBusterQueryString,
locale,
...docProps, ...docProps,
})} })}
</AmpStateContext.Provider> </AmpStateContext.Provider>
...@@ -487,6 +500,9 @@ export async function renderToHTML( ...@@ -487,6 +500,9 @@ export async function renderToHTML(
} }
if (isAutoExport) renderOpts.autoExport = true if (isAutoExport) renderOpts.autoExport = true
if (isSSG) renderOpts.nextExport = false if (isSSG) renderOpts.nextExport = false
// don't set default locale for fallback pages since this needs to be
// handled at request time
if (isFallback) renderOpts.locale = undefined
await Loadable.preloadAll() // Make sure all dynamic imports are loaded await Loadable.preloadAll() // Make sure all dynamic imports are loaded
...@@ -499,7 +515,9 @@ export async function renderToHTML( ...@@ -499,7 +515,9 @@ export async function renderToHTML(
{ {
isFallback: isFallback, isFallback: isFallback,
}, },
basePath basePath,
renderOpts.locale,
renderOpts.locales
) )
const ctx = { const ctx = {
err, err,
...@@ -581,6 +599,8 @@ export async function renderToHTML( ...@@ -581,6 +599,8 @@ export async function renderToHTML(
...(previewData !== false ...(previewData !== false
? { preview: true, previewData: previewData } ? { preview: true, previewData: previewData }
: undefined), : undefined),
locales: renderOpts.locales,
locale: renderOpts.locale,
}) })
} catch (staticPropsError) { } catch (staticPropsError) {
// remove not found error code to prevent triggering legacy // remove not found error code to prevent triggering legacy
...@@ -696,6 +716,8 @@ export async function renderToHTML( ...@@ -696,6 +716,8 @@ export async function renderToHTML(
...(previewData !== false ...(previewData !== false
? { preview: true, previewData: previewData } ? { preview: true, previewData: previewData }
: undefined), : undefined),
locales: renderOpts.locales,
locale: renderOpts.locale,
}) })
} catch (serverSidePropsError) { } catch (serverSidePropsError) {
// remove not found error code to prevent triggering legacy // remove not found error code to prevent triggering legacy
......
...@@ -76,6 +76,7 @@ ...@@ -76,6 +76,7 @@
"@babel/preset-typescript": "7.10.4", "@babel/preset-typescript": "7.10.4",
"@babel/runtime": "7.11.2", "@babel/runtime": "7.11.2",
"@babel/types": "7.11.5", "@babel/types": "7.11.5",
"@hapi/accept": "5.0.1",
"@next/env": "9.5.4", "@next/env": "9.5.4",
"@next/polyfill-module": "9.5.4", "@next/polyfill-module": "9.5.4",
"@next/react-dev-overlay": "9.5.4", "@next/react-dev-overlay": "9.5.4",
......
...@@ -123,7 +123,7 @@ export function Html( ...@@ -123,7 +123,7 @@ export function Html(
HTMLHtmlElement HTMLHtmlElement
> >
) { ) {
const { inAmpMode, docComponentsRendered } = useContext( const { inAmpMode, docComponentsRendered, locale } = useContext(
DocumentComponentContext DocumentComponentContext
) )
...@@ -132,6 +132,7 @@ export function Html( ...@@ -132,6 +132,7 @@ export function Html(
return ( return (
<html <html
{...props} {...props}
lang={props.lang || locale || undefined}
amp={inAmpMode ? '' : undefined} amp={inAmpMode ? '' : undefined}
data-ampdevmode={ data-ampdevmode={
inAmpMode && process.env.NODE_ENV !== 'production' ? '' : undefined inAmpMode && process.env.NODE_ENV !== 'production' ? '' : undefined
......
...@@ -534,6 +534,7 @@ export default class DevServer extends Server { ...@@ -534,6 +534,7 @@ export default class DevServer extends Server {
const __getStaticPaths = async () => { const __getStaticPaths = async () => {
const { publicRuntimeConfig, serverRuntimeConfig } = this.nextConfig const { publicRuntimeConfig, serverRuntimeConfig } = this.nextConfig
const { locales, defaultLocale } = this.nextConfig.experimental.i18n || {}
const paths = await this.staticPathsWorker.loadStaticPaths( const paths = await this.staticPathsWorker.loadStaticPaths(
this.distDir, this.distDir,
...@@ -542,7 +543,9 @@ export default class DevServer extends Server { ...@@ -542,7 +543,9 @@ export default class DevServer extends Server {
{ {
publicRuntimeConfig, publicRuntimeConfig,
serverRuntimeConfig, serverRuntimeConfig,
} },
locales,
defaultLocale
) )
return paths return paths
} }
......
...@@ -13,7 +13,9 @@ export async function loadStaticPaths( ...@@ -13,7 +13,9 @@ export async function loadStaticPaths(
distDir: string, distDir: string,
pathname: string, pathname: string,
serverless: boolean, serverless: boolean,
config: RuntimeConfig config: RuntimeConfig,
locales?: string[],
defaultLocale?: string
) { ) {
// we only want to use each worker once to prevent any invalid // we only want to use each worker once to prevent any invalid
// caches // caches
...@@ -35,5 +37,10 @@ export async function loadStaticPaths( ...@@ -35,5 +37,10 @@ export async function loadStaticPaths(
} }
workerWasUsed = true workerWasUsed = true
return buildStaticPaths(pathname, components.getStaticPaths) return buildStaticPaths(
pathname,
components.getStaticPaths,
locales,
defaultLocale
)
} }
...@@ -81,6 +81,8 @@ export type GetStaticPropsContext<Q extends ParsedUrlQuery = ParsedUrlQuery> = { ...@@ -81,6 +81,8 @@ export type GetStaticPropsContext<Q extends ParsedUrlQuery = ParsedUrlQuery> = {
params?: Q params?: Q
preview?: boolean preview?: boolean
previewData?: any previewData?: any
locale?: string
locales?: string[]
} }
export type GetStaticPropsResult<P> = { export type GetStaticPropsResult<P> = {
...@@ -103,7 +105,7 @@ export type InferGetStaticPropsType<T> = T extends GetStaticProps<infer P, any> ...@@ -103,7 +105,7 @@ export type InferGetStaticPropsType<T> = T extends GetStaticProps<infer P, any>
: never : never
export type GetStaticPathsResult<P extends ParsedUrlQuery = ParsedUrlQuery> = { export type GetStaticPathsResult<P extends ParsedUrlQuery = ParsedUrlQuery> = {
paths: Array<string | { params: P }> paths: Array<string | { params: P; locale?: string }>
fallback: boolean | 'unstable_blocking' fallback: boolean | 'unstable_blocking'
} }
...@@ -121,6 +123,8 @@ export type GetServerSidePropsContext< ...@@ -121,6 +123,8 @@ export type GetServerSidePropsContext<
preview?: boolean preview?: boolean
previewData?: any previewData?: any
resolvedUrl: string resolvedUrl: string
locale?: string
locales?: string[]
} }
export type GetServerSidePropsResult<P> = { export type GetServerSidePropsResult<P> = {
......
...@@ -95,16 +95,16 @@ describe('Build Output', () => { ...@@ -95,16 +95,16 @@ describe('Build Output', () => {
expect(indexSize.endsWith('B')).toBe(true) expect(indexSize.endsWith('B')).toBe(true)
// should be no bigger than 60.8 kb // should be no bigger than 60.8 kb
expect(parseFloat(indexFirstLoad) - 60.8).toBeLessThanOrEqual(0) expect(parseFloat(indexFirstLoad) - 61).toBeLessThanOrEqual(0)
expect(indexFirstLoad.endsWith('kB')).toBe(true) expect(indexFirstLoad.endsWith('kB')).toBe(true)
expect(parseFloat(err404Size) - 3.5).toBeLessThanOrEqual(0) expect(parseFloat(err404Size) - 3.5).toBeLessThanOrEqual(0)
expect(err404Size.endsWith('kB')).toBe(true) expect(err404Size.endsWith('kB')).toBe(true)
expect(parseFloat(err404FirstLoad) - 63.8).toBeLessThanOrEqual(0) expect(parseFloat(err404FirstLoad) - 64.2).toBeLessThanOrEqual(0)
expect(err404FirstLoad.endsWith('kB')).toBe(true) expect(err404FirstLoad.endsWith('kB')).toBe(true)
expect(parseFloat(sharedByAll) - 60.4).toBeLessThanOrEqual(0) expect(parseFloat(sharedByAll) - 60.7).toBeLessThanOrEqual(0)
expect(sharedByAll.endsWith('kB')).toBe(true) expect(sharedByAll.endsWith('kB')).toBe(true)
if (_appSize.endsWith('kB')) { if (_appSize.endsWith('kB')) {
......
module.exports = {
// target: 'experimental-serverless-trace',
experimental: {
i18n: {
locales: ['nl-NL', 'nl-BE', 'nl', 'en-US', 'en'],
defaultLocale: 'en',
},
},
}
import Link from 'next/link'
import { useRouter } from 'next/router'
export default function Page(props) {
const router = useRouter()
return (
<>
<p id="another">another page</p>
<p id="props">{JSON.stringify(props)}</p>
<p id="router-locale">{router.locale}</p>
<p id="router-locales">{JSON.stringify(router.locales)}</p>
<p id="router-query">{JSON.stringify(router.query)}</p>
<p id="router-pathname">{router.pathname}</p>
<p id="router-as-path">{router.asPath}</p>
<Link href="/">
<a id="to-index">to /</a>
</Link>
<br />
</>
)
}
export const getServerSideProps = ({ locale, locales }) => {
return {
props: {
locale,
locales,
},
}
}
import Link from 'next/link'
import { useRouter } from 'next/router'
export default function Page(props) {
const router = useRouter()
if (router.isFallback) return 'Loading...'
return (
<>
<p id="gsp">gsp page</p>
<p id="props">{JSON.stringify(props)}</p>
<p id="router-locale">{router.locale}</p>
<p id="router-locales">{JSON.stringify(router.locales)}</p>
<p id="router-query">{JSON.stringify(router.query)}</p>
<p id="router-pathname">{router.pathname}</p>
<p id="router-as-path">{router.asPath}</p>
<Link href="/">
<a id="to-index">to /</a>
</Link>
<br />
</>
)
}
export const getStaticProps = ({ params, locale, locales }) => {
return {
props: {
params,
locale,
locales,
},
}
}
export const getStaticPaths = () => {
return {
// the default locale will be used since one isn't defined here
paths: ['first', 'second'].map((slug) => ({
params: { slug },
})),
fallback: true,
}
}
import Link from 'next/link'
import { useRouter } from 'next/router'
export default function Page(props) {
const router = useRouter()
return (
<>
<p id="gsp">gsp page</p>
<p id="props">{JSON.stringify(props)}</p>
<p id="router-locale">{router.locale}</p>
<p id="router-locales">{JSON.stringify(router.locales)}</p>
<p id="router-query">{JSON.stringify(router.query)}</p>
<p id="router-pathname">{router.pathname}</p>
<p id="router-as-path">{router.asPath}</p>
<Link href="/">
<a id="to-index">to /</a>
</Link>
<br />
</>
)
}
// TODO: should non-dynamic GSP pages pre-render for each locale?
export const getStaticProps = ({ locale, locales }) => {
return {
props: {
locale,
locales,
},
}
}
import Link from 'next/link'
import { useRouter } from 'next/router'
export default function Page(props) {
const router = useRouter()
if (router.isFallback) return 'Loading...'
return (
<>
<p id="gsp">gsp page</p>
<p id="props">{JSON.stringify(props)}</p>
<p id="router-locale">{router.locale}</p>
<p id="router-locales">{JSON.stringify(router.locales)}</p>
<p id="router-query">{JSON.stringify(router.query)}</p>
<p id="router-pathname">{router.pathname}</p>
<p id="router-as-path">{router.asPath}</p>
<Link href="/">
<a id="to-index">to /</a>
</Link>
<br />
</>
)
}
export const getStaticProps = ({ params, locale, locales }) => {
return {
props: {
params,
locale,
locales,
},
}
}
export const getStaticPaths = () => {
return {
paths: [
{ params: { slug: 'first' } },
'/gsp/no-fallback/second',
{ params: { slug: 'first' }, locale: 'en-US' },
'/nl-NL/gsp/no-fallback/second',
],
fallback: false,
}
}
import Link from 'next/link'
import { useRouter } from 'next/router'
export default function Page(props) {
const router = useRouter()
return (
<>
<p id="gssp">gssp page</p>
<p id="props">{JSON.stringify(props)}</p>
<p id="router-locale">{router.locale}</p>
<p id="router-locales">{JSON.stringify(router.locales)}</p>
<p id="router-query">{JSON.stringify(router.query)}</p>
<p id="router-pathname">{router.pathname}</p>
<p id="router-as-path">{router.asPath}</p>
<Link href="/">
<a id="to-index">to /</a>
</Link>
<br />
</>
)
}
export const getServerSideProps = ({ params, locale, locales }) => {
return {
props: {
params,
locale,
locales,
},
}
}
import Link from 'next/link'
import { useRouter } from 'next/router'
export default function Page(props) {
const router = useRouter()
return (
<>
<p id="gssp">gssp page</p>
<p id="props">{JSON.stringify(props)}</p>
<p id="router-locale">{router.locale}</p>
<p id="router-locales">{JSON.stringify(router.locales)}</p>
<p id="router-query">{JSON.stringify(router.query)}</p>
<p id="router-pathname">{router.pathname}</p>
<p id="router-as-path">{router.asPath}</p>
<Link href="/">
<a id="to-index">to /</a>
</Link>
<br />
</>
)
}
export const getServerSideProps = ({ locale, locales }) => {
return {
props: {
locale,
locales,
},
}
}
import Link from 'next/link'
import { useRouter } from 'next/router'
export default function Page(props) {
const router = useRouter()
return (
<>
<p id="index">index page</p>
<p id="props">{JSON.stringify(props)}</p>
<p id="router-locale">{router.locale}</p>
<p id="router-locales">{JSON.stringify(router.locales)}</p>
<p id="router-query">{JSON.stringify(router.query)}</p>
<p id="router-pathname">{router.pathname}</p>
<p id="router-as-path">{router.asPath}</p>
<Link href="/another">
<a id="to-another">to /another</a>
</Link>
<br />
<Link href="/gsp">
<a id="to-gsp">to /gsp</a>
</Link>
<br />
<Link href="/gsp/fallback/first">
<a id="to-fallback-first">to /gsp/fallback/first</a>
</Link>
<br />
<Link href="/gsp/fallback/hello">
<a id="to-fallback-hello">to /gsp/fallback/hello</a>
</Link>
<br />
<Link href="/gsp/no-fallback/first">
<a id="to-no-fallback-first">to /gsp/no-fallback/first</a>
</Link>
<br />
<Link href="/gssp">
<a id="to-gssp">to /gssp</a>
</Link>
<br />
<Link href="/gssp/first">
<a id="to-gssp-slug">to /gssp/first</a>
</Link>
<br />
</>
)
}
export const getServerSideProps = ({ locale, locales }) => {
return {
props: {
locale,
locales,
},
}
}
This diff is collapsed.
...@@ -80,7 +80,7 @@ describe('Production response size', () => { ...@@ -80,7 +80,7 @@ describe('Production response size', () => {
) )
// These numbers are without gzip compression! // These numbers are without gzip compression!
const delta = responseSizesBytes - 279 * 1024 const delta = responseSizesBytes - 280 * 1024
expect(delta).toBeLessThanOrEqual(1024) // don't increase size more than 1kb expect(delta).toBeLessThanOrEqual(1024) // don't increase size more than 1kb
expect(delta).toBeGreaterThanOrEqual(-1024) // don't decrease size more than 1kb without updating target expect(delta).toBeGreaterThanOrEqual(-1024) // don't decrease size more than 1kb without updating target
}) })
...@@ -100,7 +100,7 @@ describe('Production response size', () => { ...@@ -100,7 +100,7 @@ describe('Production response size', () => {
) )
// These numbers are without gzip compression! // These numbers are without gzip compression!
const delta = responseSizesBytes - 170 * 1024 const delta = responseSizesBytes - 171 * 1024
expect(delta).toBeLessThanOrEqual(1024) // don't increase size more than 1kb expect(delta).toBeLessThanOrEqual(1024) // don't increase size more than 1kb
expect(delta).toBeGreaterThanOrEqual(-1024) // don't decrease size more than 1kb without updating target expect(delta).toBeGreaterThanOrEqual(-1024) // don't decrease size more than 1kb without updating target
}) })
......
...@@ -70,8 +70,8 @@ export function renderViaAPI(app, pathname, query) { ...@@ -70,8 +70,8 @@ export function renderViaAPI(app, pathname, query) {
return app.renderToHTML({ url }, {}, pathname, query) return app.renderToHTML({ url }, {}, pathname, query)
} }
export function renderViaHTTP(appPort, pathname, query) { export function renderViaHTTP(appPort, pathname, query, opts) {
return fetchViaHTTP(appPort, pathname, query).then((res) => res.text()) return fetchViaHTTP(appPort, pathname, query, opts).then((res) => res.text())
} }
export function fetchViaHTTP(appPort, pathname, query, opts) { export function fetchViaHTTP(appPort, pathname, query, opts) {
......
...@@ -1825,6 +1825,26 @@ ...@@ -1825,6 +1825,26 @@
lodash.camelcase "^4.3.0" lodash.camelcase "^4.3.0"
protobufjs "^6.8.6" protobufjs "^6.8.6"
"@hapi/accept@5.0.1":
version "5.0.1"
resolved "https://registry.yarnpkg.com/@hapi/accept/-/accept-5.0.1.tgz#068553e867f0f63225a506ed74e899441af53e10"
integrity sha512-fMr4d7zLzsAXo28PRRQPXR1o2Wmu+6z+VY1UzDp0iFo13Twj8WePakwXBiqn3E1aAlTpSNzCXdnnQXFhst8h8Q==
dependencies:
"@hapi/boom" "9.x.x"
"@hapi/hoek" "9.x.x"
"@hapi/boom@9.x.x":
version "9.1.0"
resolved "https://registry.yarnpkg.com/@hapi/boom/-/boom-9.1.0.tgz#0d9517657a56ff1e0b42d0aca9da1b37706fec56"
integrity sha512-4nZmpp4tXbm162LaZT45P7F7sgiem8dwAh2vHWT6XX24dozNjGMg6BvKCRvtCUcmcXqeMIUqWN8Rc5X8yKuROQ==
dependencies:
"@hapi/hoek" "9.x.x"
"@hapi/hoek@9.x.x":
version "9.1.0"
resolved "https://registry.yarnpkg.com/@hapi/hoek/-/hoek-9.1.0.tgz#6c9eafc78c1529248f8f4d92b0799a712b6052c6"
integrity sha512-i9YbZPN3QgfighY/1X1Pu118VUz2Fmmhd6b2n0/O8YVgGGfw0FbUYoA97k7FkpGJ+pLCFEDLUmAPPV4D1kpeFw==
"@istanbuljs/load-nyc-config@^1.0.0": "@istanbuljs/load-nyc-config@^1.0.0":
version "1.0.0" version "1.0.0"
resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.0.0.tgz#10602de5570baea82f8afbfa2630b24e7a8cfe5b" resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.0.0.tgz#10602de5570baea82f8afbfa2630b24e7a8cfe5b"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment