Skip to content

Commit

Permalink
Expose configured default locale in GS(S)P methods (#18216)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Oct 27, 2020
1 parent 9a770bd commit 379f4c6
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/advanced-features/i18n-routing.md
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions docs/basic-features/data-fetching.md
Expand Up @@ -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:

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion packages/next/build/utils.ts
Expand Up @@ -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` +
Expand Down
2 changes: 2 additions & 0 deletions packages/next/next-server/server/render.tsx
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions packages/next/types/index.d.ts
Expand Up @@ -83,6 +83,7 @@ export type GetStaticPropsContext<Q extends ParsedUrlQuery = ParsedUrlQuery> = {
previewData?: any
locale?: string
locales?: string[]
defaultLocale?: string
}

export type GetStaticPropsResult<P> =
Expand All @@ -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> = {
Expand All @@ -128,6 +130,7 @@ export type GetServerSidePropsContext<
resolvedUrl: string
locale?: string
locales?: string[]
defaultLocale?: string
}

export type GetServerSidePropsResult<P> =
Expand Down
3 changes: 2 additions & 1 deletion test/integration/i18n-support/pages/another.js
Expand Up @@ -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,
},
}
}
9 changes: 7 additions & 2 deletions test/integration/i18n-support/pages/gsp/fallback/[slug].js
Expand Up @@ -23,24 +23,29 @@ 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(
'locales missing in getStaticPaths!! got: ' + JSON.stringify(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) => ({
Expand Down
3 changes: 2 additions & 1 deletion test/integration/i18n-support/pages/gsp/index.js
Expand Up @@ -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,
},
}
}
Expand Up @@ -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,
},
}
}
Expand Down
8 changes: 7 additions & 1 deletion test/integration/i18n-support/pages/gssp/[slug].js
Expand Up @@ -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,
},
}
}
3 changes: 2 additions & 1 deletion test/integration/i18n-support/pages/gssp/index.js
Expand Up @@ -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,
},
}
}
3 changes: 2 additions & 1 deletion test/integration/i18n-support/pages/index.js
Expand Up @@ -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,
},
}
}
19 changes: 19 additions & 0 deletions test/integration/i18n-support/test/index.test.js
Expand Up @@ -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)
Expand All @@ -1011,6 +1012,7 @@ function runTests(isDev) {
params: {
slug: 'first',
},
defaultLocale: 'en-US',
})
expect(JSON.parse($('#router-query').text())).toEqual({
slug: 'first',
Expand All @@ -1031,6 +1033,7 @@ function runTests(isDev) {
params: {
slug: 'another',
},
defaultLocale: 'en-US',
})
expect(
JSON.parse(await browser.elementByCss('#router-query').text())
Expand Down Expand Up @@ -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)
Expand All @@ -1133,6 +1137,7 @@ function runTests(isDev) {
params: {
slug: 'first',
},
defaultLocale: 'en-US',
})
expect(JSON.parse($('#router-query').text())).toEqual({
slug: 'first',
Expand All @@ -1153,6 +1158,7 @@ function runTests(isDev) {
params: {
slug: 'another',
},
defaultLocale: 'en-US',
})
expect(
JSON.parse(await browser.elementByCss('#router-query').text())
Expand All @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -1270,6 +1279,7 @@ function runTests(isDev) {
params: {
slug: 'another',
},
defaultLocale: 'en-US',
})
expect(
JSON.parse(await browser.elementByCss('#router-query').text())
Expand All @@ -1293,6 +1303,7 @@ function runTests(isDev) {
params: {
slug: 'first',
},
defaultLocale: 'en-US',
})
expect(
JSON.parse(await browser.elementByCss('#router-query').text())
Expand All @@ -1319,6 +1330,7 @@ function runTests(isDev) {
params: {
slug: 'second',
},
defaultLocale: 'en-US',
})
expect(
JSON.parse(await browser.elementByCss('#router-query').text())
Expand Down Expand Up @@ -1353,6 +1365,7 @@ function runTests(isDev) {
params: {
slug: 'second',
},
defaultLocale: 'en-US',
})
expect(JSON.parse($('#router-query').text())).toEqual({
slug: 'second',
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 379f4c6

Please sign in to comment.