Unverified Commit 379f4c65 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Expose configured default locale in GS(S)P methods (#18216)

parent 9a770bde
......@@ -137,7 +137,7 @@ You can access the locale information via the Next.js router. For example, using
When [pre-rendering](/docs/basic-features/pages#static-generation-recommended) pages with `getStaticProps` or `getServerSideProps`, the locale information is provided in [the context](https://nextjs.org/docs/basic-features/data-fetching#getstaticprops-static-generation) provided to the function.
When leveraging `getStaticPaths`, the supported locales are provided in the context parameter of the function under `locales`.
When leveraging `getStaticPaths`, the configured locales are provided in the context parameter of the function under `locales` and the configured defaultLocale under `defaultLocale`.
## Transition between locales
......
......@@ -56,6 +56,7 @@ The `context` parameter is an object containing the following keys:
- `previewData` contains the preview data set by `setPreviewData`. See the [Preview Mode documentation](/docs/advanced-features/preview-mode.md).
- `locale` contains the active locale (if enabled).
- `locales` contains all supported locales (if enabled).
- `defaultLocale` contains the configured default locale (if enabled).
`getStaticProps` should return an object with:
......@@ -547,6 +548,9 @@ The `context` parameter is an object containing the following keys:
- `preview`: `preview` is `true` if the page is in the preview mode and `false` otherwise. See the [Preview Mode documentation](/docs/advanced-features/preview-mode.md).
- `previewData`: The preview data set by `setPreviewData`. See the [Preview Mode documentation](/docs/advanced-features/preview-mode.md).
- `resolvedUrl`: A normalized version of the request URL that strips the `_next/data` prefix for client transitions and includes original query values.
- `locale` contains the active locale (if enabled).
- `locales` contains all supported locales (if enabled).
- `defaultLocale` contains the configured default locale (if enabled).
> **Note**: You can import modules in top-level scope for use in `getServerSideProps`.
> Imports used in `getServerSideProps` will not be bundled for the client-side.
......
......@@ -544,7 +544,7 @@ export async function buildStaticPaths(
// Get the default list of allowed params.
const _validParamKeys = Object.keys(_routeMatcher(page))
const staticPathsResult = await getStaticPaths({ locales })
const staticPathsResult = await getStaticPaths({ locales, defaultLocale })
const expectedReturnVal =
`Expected: { paths: [], fallback: boolean }\n` +
......
......@@ -606,6 +606,7 @@ export async function renderToHTML(
: undefined),
locales: renderOpts.locales,
locale: renderOpts.locale,
defaultLocale: renderOpts.defaultLocale,
})
} catch (staticPropsError) {
// remove not found error code to prevent triggering legacy
......@@ -744,6 +745,7 @@ export async function renderToHTML(
: undefined),
locales: renderOpts.locales,
locale: renderOpts.locale,
defaultLocale: renderOpts.defaultLocale,
})
} catch (serverSidePropsError) {
// remove not found error code to prevent triggering legacy
......
......@@ -83,6 +83,7 @@ export type GetStaticPropsContext<Q extends ParsedUrlQuery = ParsedUrlQuery> = {
previewData?: any
locale?: string
locales?: string[]
defaultLocale?: string
}
export type GetStaticPropsResult<P> =
......@@ -105,6 +106,7 @@ export type InferGetStaticPropsType<T> = T extends GetStaticProps<infer P, any>
export type GetStaticPathsContext = {
locales?: string[]
defaultLocale?: string
}
export type GetStaticPathsResult<P extends ParsedUrlQuery = ParsedUrlQuery> = {
......@@ -128,6 +130,7 @@ export type GetServerSidePropsContext<
resolvedUrl: string
locale?: string
locales?: string[]
defaultLocale?: string
}
export type GetServerSidePropsResult<P> =
......
......@@ -21,11 +21,12 @@ export default function Page(props) {
)
}
export const getServerSideProps = ({ locale, locales }) => {
export const getServerSideProps = ({ locale, locales, defaultLocale }) => {
return {
props: {
locale,
locales,
defaultLocale,
},
}
}
......@@ -23,17 +23,18 @@ export default function Page(props) {
)
}
export const getStaticProps = ({ params, locale, locales }) => {
export const getStaticProps = ({ params, locale, locales, defaultLocale }) => {
return {
props: {
params,
locale,
locales,
defaultLocale,
},
}
}
export const getStaticPaths = ({ locales }) => {
export const getStaticPaths = ({ locales, defaultLocale }) => {
// make sure locales were provided correctly
if (!locales || locales.length !== 7) {
throw new Error(
......@@ -41,6 +42,10 @@ export const getStaticPaths = ({ locales }) => {
)
}
if (!defaultLocale) {
throw new Error('missing defaultLocale in getStaticPaths')
}
return {
// the default locale will be used since one isn't defined here
paths: ['first', 'second'].map((slug) => ({
......
......@@ -21,11 +21,12 @@ export default function Page(props) {
)
}
export const getStaticProps = ({ locale, locales }) => {
export const getStaticProps = ({ locale, locales, defaultLocale }) => {
return {
props: {
locale,
locales,
defaultLocale,
},
}
}
......@@ -23,12 +23,13 @@ export default function Page(props) {
)
}
export const getStaticProps = ({ params, locale, locales }) => {
export const getStaticProps = ({ params, locale, locales, defaultLocale }) => {
return {
props: {
params,
locale,
locales,
defaultLocale,
},
}
}
......
......@@ -21,12 +21,18 @@ export default function Page(props) {
)
}
export const getServerSideProps = ({ params, locale, locales }) => {
export const getServerSideProps = ({
params,
locale,
locales,
defaultLocale,
}) => {
return {
props: {
params,
locale,
locales,
defaultLocale,
},
}
}
......@@ -21,11 +21,12 @@ export default function Page(props) {
)
}
export const getServerSideProps = ({ locale, locales }) => {
export const getServerSideProps = ({ locale, locales, defaultLocale }) => {
return {
props: {
locale,
locales,
defaultLocale,
},
}
}
......@@ -45,11 +45,12 @@ export default function Page(props) {
)
}
export const getStaticProps = ({ locale, locales }) => {
export const getStaticProps = ({ locale, locales, defaultLocale }) => {
return {
props: {
locale,
locales,
defaultLocale,
},
}
}
......@@ -995,6 +995,7 @@ function runTests(isDev) {
expect(JSON.parse($('#props').text())).toEqual({
locale: 'en-US',
locales,
defaultLocale: 'en-US',
})
expect($('#router-locale').text()).toBe('en-US')
expect(JSON.parse($('#router-locales').text())).toEqual(locales)
......@@ -1011,6 +1012,7 @@ function runTests(isDev) {
params: {
slug: 'first',
},
defaultLocale: 'en-US',
})
expect(JSON.parse($('#router-query').text())).toEqual({
slug: 'first',
......@@ -1031,6 +1033,7 @@ function runTests(isDev) {
params: {
slug: 'another',
},
defaultLocale: 'en-US',
})
expect(
JSON.parse(await browser.elementByCss('#router-query').text())
......@@ -1117,6 +1120,7 @@ function runTests(isDev) {
expect(JSON.parse($('#props').text())).toEqual({
locale: 'en-US',
locales,
defaultLocale: 'en-US',
})
expect($('#router-locale').text()).toBe('en-US')
expect(JSON.parse($('#router-locales').text())).toEqual(locales)
......@@ -1133,6 +1137,7 @@ function runTests(isDev) {
params: {
slug: 'first',
},
defaultLocale: 'en-US',
})
expect(JSON.parse($('#router-query').text())).toEqual({
slug: 'first',
......@@ -1153,6 +1158,7 @@ function runTests(isDev) {
params: {
slug: 'another',
},
defaultLocale: 'en-US',
})
expect(
JSON.parse(await browser.elementByCss('#router-query').text())
......@@ -1174,6 +1180,7 @@ function runTests(isDev) {
expect(JSON.parse($('#props').text())).toEqual({
locale: 'en-US',
locales,
defaultLocale: 'en-US',
})
expect($('#router-locale').text()).toBe('en-US')
expect(JSON.parse($('#router-locales').text())).toEqual(locales)
......@@ -1208,6 +1215,7 @@ function runTests(isDev) {
expect(JSON.parse(await browser.elementByCss('#props').text())).toEqual({
locale: 'en-US',
locales,
defaultLocale: 'en-US',
})
expect(await browser.elementByCss('#router-locale').text()).toBe('en-US')
expect(
......@@ -1237,6 +1245,7 @@ function runTests(isDev) {
expect(JSON.parse(await browser.elementByCss('#props').text())).toEqual({
locale: 'en-US',
locales,
defaultLocale: 'en-US',
})
expect(await browser.elementByCss('#router-locale').text()).toBe('en-US')
expect(
......@@ -1270,6 +1279,7 @@ function runTests(isDev) {
params: {
slug: 'another',
},
defaultLocale: 'en-US',
})
expect(
JSON.parse(await browser.elementByCss('#router-query').text())
......@@ -1293,6 +1303,7 @@ function runTests(isDev) {
params: {
slug: 'first',
},
defaultLocale: 'en-US',
})
expect(
JSON.parse(await browser.elementByCss('#router-query').text())
......@@ -1319,6 +1330,7 @@ function runTests(isDev) {
params: {
slug: 'second',
},
defaultLocale: 'en-US',
})
expect(
JSON.parse(await browser.elementByCss('#router-query').text())
......@@ -1353,6 +1365,7 @@ function runTests(isDev) {
params: {
slug: 'second',
},
defaultLocale: 'en-US',
})
expect(JSON.parse($('#router-query').text())).toEqual({
slug: 'second',
......@@ -1369,6 +1382,7 @@ function runTests(isDev) {
expect(JSON.parse($('#props').text())).toEqual({
locale: 'en-US',
locales,
defaultLocale: 'en-US',
})
expect($('#router-locale').text()).toBe('en-US')
expect(JSON.parse($('#router-locales').text())).toEqual(locales)
......@@ -1381,6 +1395,7 @@ function runTests(isDev) {
expect(JSON.parse($2('#props').text())).toEqual({
locale: 'nl-NL',
locales,
defaultLocale: 'en-US',
})
expect($2('#router-locale').text()).toBe('nl-NL')
expect(JSON.parse($2('#router-locales').text())).toEqual(locales)
......@@ -1398,6 +1413,7 @@ function runTests(isDev) {
params: {
slug: 'first',
},
defaultLocale: 'en-US',
})
expect($('#router-locale').text()).toBe('en-US')
expect(JSON.parse($('#router-locales').text())).toEqual(locales)
......@@ -1413,6 +1429,7 @@ function runTests(isDev) {
params: {
slug: 'first',
},
defaultLocale: 'en-US',
})
expect($2('#router-locale').text()).toBe('nl-NL')
expect(JSON.parse($2('#router-locales').text())).toEqual(locales)
......@@ -1443,6 +1460,7 @@ function runTests(isDev) {
expect(JSON.parse(await browser.elementByCss('#props').text())).toEqual({
locale: 'en',
locales,
defaultLocale: 'en-US',
})
expect(
JSON.parse(await browser.elementByCss('#router-query').text())
......@@ -1471,6 +1489,7 @@ function runTests(isDev) {
expect(JSON.parse(await browser.elementByCss('#props').text())).toEqual({
locale: 'en',
locales,
defaultLocale: 'en-US',
})
expect(
JSON.parse(await browser.elementByCss('#router-query').text())
......
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