From 379f4c6574b16ef842d9e86b552ab4fb15613916 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Tue, 27 Oct 2020 04:43:15 -0500 Subject: [PATCH] Expose configured default locale in GS(S)P methods (#18216) --- docs/advanced-features/i18n-routing.md | 2 +- docs/basic-features/data-fetching.md | 4 ++++ packages/next/build/utils.ts | 2 +- packages/next/next-server/server/render.tsx | 2 ++ packages/next/types/index.d.ts | 3 +++ .../integration/i18n-support/pages/another.js | 3 ++- .../i18n-support/pages/gsp/fallback/[slug].js | 9 +++++++-- .../i18n-support/pages/gsp/index.js | 3 ++- .../pages/gsp/no-fallback/[slug].js | 3 ++- .../i18n-support/pages/gssp/[slug].js | 8 +++++++- .../i18n-support/pages/gssp/index.js | 3 ++- test/integration/i18n-support/pages/index.js | 3 ++- .../i18n-support/test/index.test.js | 19 +++++++++++++++++++ 13 files changed, 54 insertions(+), 10 deletions(-) diff --git a/docs/advanced-features/i18n-routing.md b/docs/advanced-features/i18n-routing.md index 367d15586e707c6..f924a1af8e03268 100644 --- a/docs/advanced-features/i18n-routing.md +++ b/docs/advanced-features/i18n-routing.md @@ -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 diff --git a/docs/basic-features/data-fetching.md b/docs/basic-features/data-fetching.md index 2ca3fe32af60993..7f973189425d812 100644 --- a/docs/basic-features/data-fetching.md +++ b/docs/basic-features/data-fetching.md @@ -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. diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index b4b313418f66d12..6a171be27f3151b 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -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` + diff --git a/packages/next/next-server/server/render.tsx b/packages/next/next-server/server/render.tsx index 85d2423fbc3ceb1..d497c70036ac416 100644 --- a/packages/next/next-server/server/render.tsx +++ b/packages/next/next-server/server/render.tsx @@ -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 diff --git a/packages/next/types/index.d.ts b/packages/next/types/index.d.ts index 025a778eba96b95..29d8e361d488f43 100644 --- a/packages/next/types/index.d.ts +++ b/packages/next/types/index.d.ts @@ -83,6 +83,7 @@ export type GetStaticPropsContext = { previewData?: any locale?: string locales?: string[] + defaultLocale?: string } export type GetStaticPropsResult

= @@ -105,6 +106,7 @@ export type InferGetStaticPropsType = T extends GetStaticProps export type GetStaticPathsContext = { locales?: string[] + defaultLocale?: string } export type GetStaticPathsResult

= { @@ -128,6 +130,7 @@ export type GetServerSidePropsContext< resolvedUrl: string locale?: string locales?: string[] + defaultLocale?: string } export type GetServerSidePropsResult

= diff --git a/test/integration/i18n-support/pages/another.js b/test/integration/i18n-support/pages/another.js index 0713a50be1bbb2c..d7d1febc4958046 100644 --- a/test/integration/i18n-support/pages/another.js +++ b/test/integration/i18n-support/pages/another.js @@ -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, }, } } diff --git a/test/integration/i18n-support/pages/gsp/fallback/[slug].js b/test/integration/i18n-support/pages/gsp/fallback/[slug].js index d4486b24493b382..d0f6e9150391287 100644 --- a/test/integration/i18n-support/pages/gsp/fallback/[slug].js +++ b/test/integration/i18n-support/pages/gsp/fallback/[slug].js @@ -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) => ({ diff --git a/test/integration/i18n-support/pages/gsp/index.js b/test/integration/i18n-support/pages/gsp/index.js index 7bb55bb36ccd547..574c79399778f81 100644 --- a/test/integration/i18n-support/pages/gsp/index.js +++ b/test/integration/i18n-support/pages/gsp/index.js @@ -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, }, } } diff --git a/test/integration/i18n-support/pages/gsp/no-fallback/[slug].js b/test/integration/i18n-support/pages/gsp/no-fallback/[slug].js index 2df6728803f7a2d..a97bd749443a14c 100644 --- a/test/integration/i18n-support/pages/gsp/no-fallback/[slug].js +++ b/test/integration/i18n-support/pages/gsp/no-fallback/[slug].js @@ -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, }, } } diff --git a/test/integration/i18n-support/pages/gssp/[slug].js b/test/integration/i18n-support/pages/gssp/[slug].js index 759937cc84c8eab..8b768b0ac56ba25 100644 --- a/test/integration/i18n-support/pages/gssp/[slug].js +++ b/test/integration/i18n-support/pages/gssp/[slug].js @@ -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, }, } } diff --git a/test/integration/i18n-support/pages/gssp/index.js b/test/integration/i18n-support/pages/gssp/index.js index 6919f3548f7cbd2..eb21a085c24c30f 100644 --- a/test/integration/i18n-support/pages/gssp/index.js +++ b/test/integration/i18n-support/pages/gssp/index.js @@ -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, }, } } diff --git a/test/integration/i18n-support/pages/index.js b/test/integration/i18n-support/pages/index.js index ce44ddab7b858b1..345e004151c9f5e 100644 --- a/test/integration/i18n-support/pages/index.js +++ b/test/integration/i18n-support/pages/index.js @@ -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, }, } } diff --git a/test/integration/i18n-support/test/index.test.js b/test/integration/i18n-support/test/index.test.js index 589ec2d3dc01bac..583d898bd669fa4 100644 --- a/test/integration/i18n-support/test/index.test.js +++ b/test/integration/i18n-support/test/index.test.js @@ -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())