diff --git a/packages/next-intl/src/server/react-client/index.test.tsx b/packages/next-intl/src/server/react-client/index.test.tsx index 24ab36d9b..9ad50db29 100644 --- a/packages/next-intl/src/server/react-client/index.test.tsx +++ b/packages/next-intl/src/server/react-client/index.test.tsx @@ -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.' diff --git a/packages/next-intl/src/server/react-server/getConfig.tsx b/packages/next-intl/src/server/react-server/getConfig.tsx index 03f4add46..0661e1de0 100644 --- a/packages/next-intl/src/server/react-server/getConfig.tsx +++ b/packages/next-intl/src/server/react-server/getConfig.tsx @@ -1,4 +1,3 @@ -import {notFound} from 'next/navigation.js'; import {cache} from 'react'; import { IntlConfig, @@ -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() }; diff --git a/packages/next-intl/src/server/react-server/getRequestConfig.tsx b/packages/next-intl/src/server/react-server/getRequestConfig.tsx index c4fbf1aa8..1e3c4af4b 100644 --- a/packages/next-intl/src/server/react-server/getRequestConfig.tsx +++ b/packages/next-intl/src/server/react-server/getRequestConfig.tsx @@ -2,16 +2,9 @@ import type {IntlConfig} from 'use-intl/core'; export type RequestConfig = Omit & { /** - * 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 = {