Skip to content

Commit 379f4c6

Browse files
authored
Expose configured default locale in GS(S)P methods (#18216)
1 parent 9a770bd commit 379f4c6

File tree

13 files changed

+54
-10
lines changed

13 files changed

+54
-10
lines changed

docs/advanced-features/i18n-routing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ You can access the locale information via the Next.js router. For example, using
137137

138138
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.
139139

140-
When leveraging `getStaticPaths`, the supported locales are provided in the context parameter of the function under `locales`.
140+
When leveraging `getStaticPaths`, the configured locales are provided in the context parameter of the function under `locales` and the configured defaultLocale under `defaultLocale`.
141141

142142
## Transition between locales
143143

docs/basic-features/data-fetching.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ The `context` parameter is an object containing the following keys:
5656
- `previewData` contains the preview data set by `setPreviewData`. See the [Preview Mode documentation](/docs/advanced-features/preview-mode.md).
5757
- `locale` contains the active locale (if enabled).
5858
- `locales` contains all supported locales (if enabled).
59+
- `defaultLocale` contains the configured default locale (if enabled).
5960

6061
`getStaticProps` should return an object with:
6162

@@ -547,6 +548,9 @@ The `context` parameter is an object containing the following keys:
547548
- `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).
548549
- `previewData`: The preview data set by `setPreviewData`. See the [Preview Mode documentation](/docs/advanced-features/preview-mode.md).
549550
- `resolvedUrl`: A normalized version of the request URL that strips the `_next/data` prefix for client transitions and includes original query values.
551+
- `locale` contains the active locale (if enabled).
552+
- `locales` contains all supported locales (if enabled).
553+
- `defaultLocale` contains the configured default locale (if enabled).
550554

551555
> **Note**: You can import modules in top-level scope for use in `getServerSideProps`.
552556
> Imports used in `getServerSideProps` will not be bundled for the client-side.

packages/next/build/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ export async function buildStaticPaths(
544544
// Get the default list of allowed params.
545545
const _validParamKeys = Object.keys(_routeMatcher(page))
546546

547-
const staticPathsResult = await getStaticPaths({ locales })
547+
const staticPathsResult = await getStaticPaths({ locales, defaultLocale })
548548

549549
const expectedReturnVal =
550550
`Expected: { paths: [], fallback: boolean }\n` +

packages/next/next-server/server/render.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ export async function renderToHTML(
606606
: undefined),
607607
locales: renderOpts.locales,
608608
locale: renderOpts.locale,
609+
defaultLocale: renderOpts.defaultLocale,
609610
})
610611
} catch (staticPropsError) {
611612
// remove not found error code to prevent triggering legacy
@@ -744,6 +745,7 @@ export async function renderToHTML(
744745
: undefined),
745746
locales: renderOpts.locales,
746747
locale: renderOpts.locale,
748+
defaultLocale: renderOpts.defaultLocale,
747749
})
748750
} catch (serverSidePropsError) {
749751
// remove not found error code to prevent triggering legacy

packages/next/types/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export type GetStaticPropsContext<Q extends ParsedUrlQuery = ParsedUrlQuery> = {
8383
previewData?: any
8484
locale?: string
8585
locales?: string[]
86+
defaultLocale?: string
8687
}
8788

8889
export type GetStaticPropsResult<P> =
@@ -105,6 +106,7 @@ export type InferGetStaticPropsType<T> = T extends GetStaticProps<infer P, any>
105106

106107
export type GetStaticPathsContext = {
107108
locales?: string[]
109+
defaultLocale?: string
108110
}
109111

110112
export type GetStaticPathsResult<P extends ParsedUrlQuery = ParsedUrlQuery> = {
@@ -128,6 +130,7 @@ export type GetServerSidePropsContext<
128130
resolvedUrl: string
129131
locale?: string
130132
locales?: string[]
133+
defaultLocale?: string
131134
}
132135

133136
export type GetServerSidePropsResult<P> =

test/integration/i18n-support/pages/another.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ export default function Page(props) {
2121
)
2222
}
2323

24-
export const getServerSideProps = ({ locale, locales }) => {
24+
export const getServerSideProps = ({ locale, locales, defaultLocale }) => {
2525
return {
2626
props: {
2727
locale,
2828
locales,
29+
defaultLocale,
2930
},
3031
}
3132
}

test/integration/i18n-support/pages/gsp/fallback/[slug].js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,29 @@ export default function Page(props) {
2323
)
2424
}
2525

26-
export const getStaticProps = ({ params, locale, locales }) => {
26+
export const getStaticProps = ({ params, locale, locales, defaultLocale }) => {
2727
return {
2828
props: {
2929
params,
3030
locale,
3131
locales,
32+
defaultLocale,
3233
},
3334
}
3435
}
3536

36-
export const getStaticPaths = ({ locales }) => {
37+
export const getStaticPaths = ({ locales, defaultLocale }) => {
3738
// make sure locales were provided correctly
3839
if (!locales || locales.length !== 7) {
3940
throw new Error(
4041
'locales missing in getStaticPaths!! got: ' + JSON.stringify(locales)
4142
)
4243
}
4344

45+
if (!defaultLocale) {
46+
throw new Error('missing defaultLocale in getStaticPaths')
47+
}
48+
4449
return {
4550
// the default locale will be used since one isn't defined here
4651
paths: ['first', 'second'].map((slug) => ({

test/integration/i18n-support/pages/gsp/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ export default function Page(props) {
2121
)
2222
}
2323

24-
export const getStaticProps = ({ locale, locales }) => {
24+
export const getStaticProps = ({ locale, locales, defaultLocale }) => {
2525
return {
2626
props: {
2727
locale,
2828
locales,
29+
defaultLocale,
2930
},
3031
}
3132
}

test/integration/i18n-support/pages/gsp/no-fallback/[slug].js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ export default function Page(props) {
2323
)
2424
}
2525

26-
export const getStaticProps = ({ params, locale, locales }) => {
26+
export const getStaticProps = ({ params, locale, locales, defaultLocale }) => {
2727
return {
2828
props: {
2929
params,
3030
locale,
3131
locales,
32+
defaultLocale,
3233
},
3334
}
3435
}

test/integration/i18n-support/pages/gssp/[slug].js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,18 @@ export default function Page(props) {
2121
)
2222
}
2323

24-
export const getServerSideProps = ({ params, locale, locales }) => {
24+
export const getServerSideProps = ({
25+
params,
26+
locale,
27+
locales,
28+
defaultLocale,
29+
}) => {
2530
return {
2631
props: {
2732
params,
2833
locale,
2934
locales,
35+
defaultLocale,
3036
},
3137
}
3238
}

0 commit comments

Comments
 (0)