From 81140bd48fe6b96c747c6a06b575d65594f47b9e Mon Sep 17 00:00:00 2001 From: Jan Amann Date: Tue, 29 Oct 2024 14:23:06 +0100 Subject: [PATCH 1/2] feat!: Require `locale` to be returned from `getRequestConfig` --- .../src/server/react-server/getConfig.tsx | 16 +++++----------- .../src/server/react-server/getRequestConfig.tsx | 11 ++--------- 2 files changed, 7 insertions(+), 20 deletions(-) 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 = { From ddaf6737a318202c1e1393a819e1b6641e55007f Mon Sep 17 00:00:00 2001 From: Jan Amann Date: Tue, 29 Oct 2024 14:28:50 +0100 Subject: [PATCH 2/2] fix lint --- packages/next-intl/src/server/react-client/index.test.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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.'