Skip to content

Commit

Permalink
feat!: Require locale to be returned from getRequestConfig (#1486)
Browse files Browse the repository at this point in the history
  • Loading branch information
amannn authored Oct 29, 2024
1 parent a9c35db commit 5402851
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
6 changes: 4 additions & 2 deletions packages/next-intl/src/server/react-client/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ describe('getRequestConfig', () => {
it('can be called in the outer module closure', () => {
expect(
getRequestConfig(async ({requestLocale}) => ({
messages: {hello: 'Hello ' + (await requestLocale)}
locale: (await requestLocale) || 'en',
messages: {hello: 'Hello'}
}))
);
});

it('can not call the returned function', () => {
const getConfig = getRequestConfig(async ({requestLocale}) => ({
messages: {hello: 'Hello ' + (await requestLocale)}
locale: (await requestLocale) || 'en',
messages: {hello: 'Hello '}
}));
expect(() => getConfig({requestLocale: Promise.resolve('en')})).toThrow(
'`getRequestConfig` is not supported in Client Components.'
Expand Down
16 changes: 5 additions & 11 deletions packages/next-intl/src/server/react-server/getConfig.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {notFound} from 'next/navigation.js';
import {cache} from 'react';
import {
IntlConfig,
Expand Down Expand Up @@ -59,20 +58,15 @@ See also: https://next-intl-docs.vercel.app/docs/usage/configuration#i18n-reques
result = await result;
}

const locale = result.locale || (await params.requestLocale);

if (!locale) {
if (process.env.NODE_ENV !== 'production') {
console.error(
`\nUnable to find \`next-intl\` locale because the middleware didn't run on this request and no \`locale\` was returned in \`getRequestConfig\`. See https://next-intl-docs.vercel.app/docs/routing/middleware#unable-to-find-locale. The \`notFound()\` function will be called as a result.\n`
);
}
notFound();
if (!result.locale) {
throw new Error(
'No locale was returned from `getRequestConfig`.\n\nSee https://next-intl-docs.vercel.app/docs/usage/configuration#i18n-request'
);
}

return {
...result,
locale,
locale: result.locale,
now: result.now || getDefaultNow(),
timeZone: result.timeZone || getDefaultTimeZone()
};
Expand Down
11 changes: 2 additions & 9 deletions packages/next-intl/src/server/react-server/getRequestConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@ import type {IntlConfig} from 'use-intl/core';

export type RequestConfig = Omit<IntlConfig, 'locale'> & {
/**
* Instead of reading a `requestLocale` from the argument that's passed to the
* function within `getRequestConfig`, you can include a locale as part of the
* returned request configuration.
*
* This can be helpful for the following use cases:
* - Apps that only support a single language
* - Apps where the locale should be read from user settings instead of the pathname
* - Providing a fallback locale in case the locale was not matched by the middleware
* @see https://next-intl-docs.vercel.app/docs/usage/configuration#i18n-request
**/
locale?: IntlConfig['locale'];
locale: IntlConfig['locale'];
};

export type GetRequestConfigParams = {
Expand Down

0 comments on commit 5402851

Please sign in to comment.