Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Require locale to be returned from getRequestConfig #1486

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading