next-intl 3.20.1-canary.0 → 3.21.0-canary.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/dist/development/navigation/react-server/createLocalizedPathnamesNavigation.js +4 -4
  2. package/dist/development/navigation/react-server/redirects.js +2 -2
  3. package/dist/development/server/react-server/RequestLocale.js +15 -25
  4. package/dist/development/server/react-server/RequestLocaleCache.js +23 -0
  5. package/dist/development/server/react-server/RequestLocaleLegacy.js +43 -0
  6. package/dist/development/server/react-server/getConfig.js +15 -17
  7. package/dist/development/server/react-server/getRequestConfig.js +1 -1
  8. package/dist/development/server.react-server.js +2 -2
  9. package/dist/esm/navigation/react-server/createLocalizedPathnamesNavigation.js +1 -1
  10. package/dist/esm/navigation/react-server/redirects.js +1 -1
  11. package/dist/esm/server/react-server/RequestLocale.js +1 -1
  12. package/dist/esm/server/react-server/RequestLocaleCache.js +1 -0
  13. package/dist/esm/server/react-server/RequestLocaleLegacy.js +1 -0
  14. package/dist/esm/server/react-server/getConfig.js +1 -1
  15. package/dist/esm/server.react-server.js +1 -1
  16. package/dist/production/navigation/react-server/createLocalizedPathnamesNavigation.js +1 -1
  17. package/dist/production/navigation/react-server/redirects.js +1 -1
  18. package/dist/production/server/react-server/RequestLocale.js +1 -1
  19. package/dist/production/server/react-server/RequestLocaleCache.js +1 -0
  20. package/dist/production/server/react-server/RequestLocaleLegacy.js +1 -0
  21. package/dist/production/server/react-server/getConfig.js +1 -1
  22. package/dist/production/server.react-server.js +1 -1
  23. package/dist/types/src/server/react-server/RequestLocale.d.ts +1 -2
  24. package/dist/types/src/server/react-server/RequestLocaleCache.d.ts +2 -0
  25. package/dist/types/src/server/react-server/RequestLocaleLegacy.d.ts +1 -0
  26. package/dist/types/src/server/react-server/createRequestConfig.d.ts +2 -3
  27. package/dist/types/src/server/react-server/getRequestConfig.d.ts +32 -6
  28. package/dist/types/src/server/react-server/index.d.ts +1 -1
  29. package/package.json +3 -3
@@ -5,7 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
6
6
  var React = require('react');
7
7
  var config = require('../../routing/config.js');
8
- var RequestLocale = require('../../server/react-server/RequestLocale.js');
8
+ var RequestLocaleLegacy = require('../../server/react-server/RequestLocaleLegacy.js');
9
9
  var utils = require('../shared/utils.js');
10
10
  var ServerLink = require('./ServerLink.js');
11
11
  var redirects = require('./redirects.js');
@@ -22,7 +22,7 @@ function createLocalizedPathnamesNavigation(routing) {
22
22
  locale,
23
23
  ...rest
24
24
  } = _ref;
25
- const defaultLocale = RequestLocale.getRequestLocale();
25
+ const defaultLocale = RequestLocaleLegacy.getRequestLocale();
26
26
  const finalLocale = locale || defaultLocale;
27
27
  return /*#__PURE__*/React__default.default.createElement(ServerLink.default, _rollupPluginBabelHelpers.extends({
28
28
  href: utils.compileLocalizedPathname({
@@ -38,7 +38,7 @@ function createLocalizedPathnamesNavigation(routing) {
38
38
  }, rest));
39
39
  }
40
40
  function redirect(href) {
41
- const locale = RequestLocale.getRequestLocale();
41
+ const locale = RequestLocaleLegacy.getRequestLocale();
42
42
  const pathname = getPathname({
43
43
  href,
44
44
  locale
@@ -52,7 +52,7 @@ function createLocalizedPathnamesNavigation(routing) {
52
52
  }, ...args);
53
53
  }
54
54
  function permanentRedirect(href) {
55
- const locale = RequestLocale.getRequestLocale();
55
+ const locale = RequestLocaleLegacy.getRequestLocale();
56
56
  const pathname = getPathname({
57
57
  href,
58
58
  locale
@@ -2,12 +2,12 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var RequestLocale = require('../../server/react-server/RequestLocale.js');
5
+ var RequestLocaleLegacy = require('../../server/react-server/RequestLocaleLegacy.js');
6
6
  var redirects = require('../shared/redirects.js');
7
7
 
8
8
  function createRedirectFn(redirectFn) {
9
9
  return function serverRedirect(params) {
10
- const locale = RequestLocale.getRequestLocale();
10
+ const locale = RequestLocaleLegacy.getRequestLocale();
11
11
  for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
12
12
  args[_key - 1] = arguments[_key];
13
13
  }
@@ -3,47 +3,37 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var headers = require('next/headers');
6
- var navigation = require('next/navigation');
7
6
  var React = require('react');
8
7
  var constants = require('../../shared/constants.js');
8
+ var RequestLocaleCache = require('./RequestLocaleCache.js');
9
9
 
10
- function getLocaleFromHeaderImpl() {
10
+ async function getHeadersImpl() {
11
+ const promiseOrValue = headers.headers();
12
+
13
+ // Compatibility with Next.js <15
14
+ return promiseOrValue instanceof Promise ? await promiseOrValue : promiseOrValue;
15
+ }
16
+ const getHeaders = React.cache(getHeadersImpl);
17
+ async function getLocaleFromHeaderImpl() {
11
18
  let locale;
12
19
  try {
13
- locale = headers.headers().get(constants.HEADER_LOCALE_NAME);
20
+ locale = (await getHeaders()).get(constants.HEADER_LOCALE_NAME) || undefined;
14
21
  } catch (error) {
15
22
  if (error instanceof Error && error.digest === 'DYNAMIC_SERVER_USAGE') {
16
- throw new Error('Usage of next-intl APIs in Server Components currently opts into dynamic rendering. This limitation will eventually be lifted, but as a stopgap solution, you can use the `unstable_setRequestLocale` API to enable static rendering, see https://next-intl-docs.vercel.app/docs/getting-started/app-router/with-i18n-routing#static-rendering', {
23
+ const wrappedError = new Error('Usage of next-intl APIs in Server Components currently opts into dynamic rendering. This limitation will eventually be lifted, but as a stopgap solution, you can use the `unstable_setRequestLocale` API to enable static rendering, see https://next-intl-docs.vercel.app/docs/getting-started/app-router/with-i18n-routing#static-rendering', {
17
24
  cause: error
18
25
  });
26
+ wrappedError.digest = error.digest;
27
+ throw wrappedError;
19
28
  } else {
20
29
  throw error;
21
30
  }
22
31
  }
23
- if (!locale) {
24
- {
25
- console.error("\nUnable to find `next-intl` locale because the middleware didn't run on this request. See https://next-intl-docs.vercel.app/docs/routing/middleware#unable-to-find-locale. The `notFound()` function will be called as a result.\n");
26
- }
27
- navigation.notFound();
28
- }
29
32
  return locale;
30
33
  }
31
34
  const getLocaleFromHeader = React.cache(getLocaleFromHeaderImpl);
32
-
33
- // Workaround until `createServerContext` is available
34
- function getCacheImpl() {
35
- const value = {
36
- locale: undefined
37
- };
38
- return value;
39
- }
40
- const getCache = React.cache(getCacheImpl);
41
- function setRequestLocale(locale) {
42
- getCache().locale = locale;
43
- }
44
- function getRequestLocale() {
45
- return getCache().locale || getLocaleFromHeader();
35
+ async function getRequestLocale() {
36
+ return RequestLocaleCache.getCachedRequestLocale() || (await getLocaleFromHeader());
46
37
  }
47
38
 
48
39
  exports.getRequestLocale = getRequestLocale;
49
- exports.setRequestLocale = setRequestLocale;
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var React = require('react');
6
+
7
+ // See https://github.com/vercel/next.js/discussions/58862
8
+ function getCacheImpl() {
9
+ const value = {
10
+ locale: undefined
11
+ };
12
+ return value;
13
+ }
14
+ const getCache = React.cache(getCacheImpl);
15
+ function getCachedRequestLocale() {
16
+ return getCache().locale;
17
+ }
18
+ function setCachedRequestLocale(locale) {
19
+ getCache().locale = locale;
20
+ }
21
+
22
+ exports.getCachedRequestLocale = getCachedRequestLocale;
23
+ exports.setCachedRequestLocale = setCachedRequestLocale;
@@ -0,0 +1,43 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var headers = require('next/headers');
6
+ var navigation = require('next/navigation');
7
+ var React = require('react');
8
+ var constants = require('../../shared/constants.js');
9
+ var RequestLocaleCache = require('./RequestLocaleCache.js');
10
+
11
+ // This was originally built for Next.js <14, where `headers()` was not async.
12
+ // With https://github.com/vercel/next.js/pull/68812, the API became async.
13
+ // This file can be removed once we remove the legacy navigation APIs.
14
+ function getHeaders() {
15
+ return headers.headers();
16
+ }
17
+ function getLocaleFromHeaderImpl() {
18
+ let locale;
19
+ try {
20
+ locale = getHeaders().get(constants.HEADER_LOCALE_NAME);
21
+ } catch (error) {
22
+ if (error instanceof Error && error.digest === 'DYNAMIC_SERVER_USAGE') {
23
+ throw new Error('Usage of next-intl APIs in Server Components currently opts into dynamic rendering. This limitation will eventually be lifted, but as a stopgap solution, you can use the `unstable_setRequestLocale` API to enable static rendering, see https://next-intl-docs.vercel.app/docs/getting-started/app-router/with-i18n-routing#static-rendering', {
24
+ cause: error
25
+ });
26
+ } else {
27
+ throw error;
28
+ }
29
+ }
30
+ if (!locale) {
31
+ {
32
+ console.error("\nUnable to find `next-intl` locale because the middleware didn't run on this request. See https://next-intl-docs.vercel.app/docs/routing/middleware#unable-to-find-locale. The `notFound()` function will be called as a result.\n");
33
+ }
34
+ navigation.notFound();
35
+ }
36
+ return locale;
37
+ }
38
+ const getLocaleFromHeader = React.cache(getLocaleFromHeaderImpl);
39
+ function getRequestLocale() {
40
+ return RequestLocaleCache.getCachedRequestLocale() || getLocaleFromHeader();
41
+ }
42
+
43
+ exports.getRequestLocale = getRequestLocale;
@@ -2,9 +2,11 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var navigation = require('next/navigation');
5
6
  var React = require('react');
6
7
  var core = require('use-intl/core');
7
8
  var RequestLocale = require('./RequestLocale.js');
9
+ var RequestLocaleLegacy = require('./RequestLocaleLegacy.js');
8
10
  var getRuntimeConfig = require('next-intl/config');
9
11
 
10
12
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
@@ -27,35 +29,31 @@ async function receiveRuntimeConfigImpl(getConfig, localeOverride) {
27
29
  if (typeof getConfig !== 'function') {
28
30
  throw new Error("Invalid i18n request configuration detected.\n\nPlease verify that:\n1. In case you've specified a custom location in your Next.js config, make sure that the path is correct.\n2. You have a default export in your i18n request configuration file.\n\nSee also: https://next-intl-docs.vercel.app/docs/usage/configuration#i18n-request\n");
29
31
  }
30
- let hasReadLocale = false;
31
-
32
- // In case the consumer doesn't read `params.locale` and instead provides the
33
- // `locale` (either in a single-language workflow or because the locale is
34
- // read from the user settings), don't attempt to read the request locale.
35
32
  const params = {
33
+ // In case the consumer doesn't read `params.locale` and instead provides the
34
+ // `locale` (either in a single-language workflow or because the locale is
35
+ // read from the user settings), don't attempt to read the request locale.
36
36
  get locale() {
37
- hasReadLocale = true;
38
- return localeOverride || RequestLocale.getRequestLocale();
37
+ return localeOverride || RequestLocaleLegacy.getRequestLocale();
38
+ },
39
+ get requestLocale() {
40
+ return localeOverride ? Promise.resolve(localeOverride) : RequestLocale.getRequestLocale();
39
41
  }
40
42
  };
41
43
  let result = getConfig(params);
42
44
  if (result instanceof Promise) {
43
45
  result = await result;
44
46
  }
45
- {
46
- if (hasReadLocale) {
47
- if (result.locale) {
48
- console.error("\nYou've read the `locale` param that was passed to `getRequestConfig` but have also returned one from the function. This is likely an error, please ensure that you're consistently using a setup with or without i18n routing: https://next-intl-docs.vercel.app/docs/getting-started/app-router\n");
49
- }
50
- } else {
51
- if (!result.locale) {
52
- console.error("\nYou haven't read the `locale` param that was passed to `getRequestConfig` and also haven't returned one from the function. This is likely an error, please ensure that you're consistently using a setup with or without i18n routing: https://next-intl-docs.vercel.app/docs/getting-started/app-router\n");
53
- }
47
+ const locale = result.locale || (await params.requestLocale);
48
+ if (!locale) {
49
+ {
50
+ 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");
54
51
  }
52
+ navigation.notFound();
55
53
  }
56
54
  return {
57
55
  ...result,
58
- locale: result.locale || params.locale,
56
+ locale,
59
57
  now: result.now || getDefaultNow(),
60
58
  timeZone: result.timeZone || getDefaultTimeZone()
61
59
  };
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  /**
6
- * Should be called in `i18n.ts` to create the configuration for the current request.
6
+ * Should be called in `i18n/request.ts` to create the configuration for the current request.
7
7
  */
8
8
  function getRequestConfig(createRequestConfig) {
9
9
  return createRequestConfig;
@@ -9,7 +9,7 @@ var getNow = require('./server/react-server/getNow.js');
9
9
  var getTimeZone = require('./server/react-server/getTimeZone.js');
10
10
  var getTranslations = require('./server/react-server/getTranslations.js');
11
11
  var getLocale = require('./server/react-server/getLocale.js');
12
- var RequestLocale = require('./server/react-server/RequestLocale.js');
12
+ var RequestLocaleCache = require('./server/react-server/RequestLocaleCache.js');
13
13
 
14
14
 
15
15
 
@@ -20,4 +20,4 @@ exports.getNow = getNow.default;
20
20
  exports.getTimeZone = getTimeZone.default;
21
21
  exports.getTranslations = getTranslations.default;
22
22
  exports.getLocale = getLocale.default;
23
- exports.unstable_setRequestLocale = RequestLocale.setRequestLocale;
23
+ exports.unstable_setRequestLocale = RequestLocaleCache.setCachedRequestLocale;
@@ -1 +1 @@
1
- import{extends as e}from"../../_virtual/_rollupPluginBabelHelpers.js";import r from"react";import{receiveRoutingConfig as t}from"../../routing/config.js";import{getRequestLocale as o}from"../../server/react-server/RequestLocale.js";import{compileLocalizedPathname as n,normalizeNameOrNameWithParams as a}from"../shared/utils.js";import l from"./ServerLink.js";import{serverRedirect as c,serverPermanentRedirect as i}from"./redirects.js";function s(s){const m=t(s);function f(e){let{href:r,locale:t}=e;return n({...a(r),locale:t,pathnames:m.pathnames})}function u(e){return()=>{throw new Error("`".concat(e,"` is not supported in Server Components. You can use this hook if you convert the component to a Client Component."))}}return{Link:function(t){let{href:a,locale:c,...i}=t;const s=o(),f=c||s;return r.createElement(l,e({href:n({locale:f,pathname:a,params:"object"==typeof a?a.params:void 0,pathnames:m.pathnames}),locale:c,localePrefix:m.localePrefix},i))},redirect:function(e){const r=f({href:e,locale:o()});for(var t=arguments.length,n=new Array(t>1?t-1:0),a=1;a<t;a++)n[a-1]=arguments[a];return c({localePrefix:m.localePrefix,pathname:r},...n)},permanentRedirect:function(e){const r=f({href:e,locale:o()});for(var t=arguments.length,n=new Array(t>1?t-1:0),a=1;a<t;a++)n[a-1]=arguments[a];return i({localePrefix:m.localePrefix,pathname:r},...n)},getPathname:f,usePathname:u("usePathname"),useRouter:u("useRouter")}}export{s as default};
1
+ import{extends as e}from"../../_virtual/_rollupPluginBabelHelpers.js";import r from"react";import{receiveRoutingConfig as t}from"../../routing/config.js";import{getRequestLocale as o}from"../../server/react-server/RequestLocaleLegacy.js";import{compileLocalizedPathname as n,normalizeNameOrNameWithParams as a}from"../shared/utils.js";import l from"./ServerLink.js";import{serverRedirect as c,serverPermanentRedirect as i}from"./redirects.js";function s(s){const m=t(s);function f(e){let{href:r,locale:t}=e;return n({...a(r),locale:t,pathnames:m.pathnames})}function u(e){return()=>{throw new Error("`".concat(e,"` is not supported in Server Components. You can use this hook if you convert the component to a Client Component."))}}return{Link:function(t){let{href:a,locale:c,...i}=t;const s=o(),f=c||s;return r.createElement(l,e({href:n({locale:f,pathname:a,params:"object"==typeof a?a.params:void 0,pathnames:m.pathnames}),locale:c,localePrefix:m.localePrefix},i))},redirect:function(e){const r=f({href:e,locale:o()});for(var t=arguments.length,n=new Array(t>1?t-1:0),a=1;a<t;a++)n[a-1]=arguments[a];return c({localePrefix:m.localePrefix,pathname:r},...n)},permanentRedirect:function(e){const r=f({href:e,locale:o()});for(var t=arguments.length,n=new Array(t>1?t-1:0),a=1;a<t;a++)n[a-1]=arguments[a];return i({localePrefix:m.localePrefix,pathname:r},...n)},getPathname:f,usePathname:u("usePathname"),useRouter:u("useRouter")}}export{s as default};
@@ -1 +1 @@
1
- import{getRequestLocale as r}from"../../server/react-server/RequestLocale.js";import{baseRedirect as e,basePermanentRedirect as t}from"../shared/redirects.js";function o(e){return function(t){const o=r();for(var n=arguments.length,s=new Array(n>1?n-1:0),c=1;c<n;c++)s[c-1]=arguments[c];return e({...t,locale:o},...s)}}const n=o(e),s=o(t);export{s as serverPermanentRedirect,n as serverRedirect};
1
+ import{getRequestLocale as r}from"../../server/react-server/RequestLocaleLegacy.js";import{baseRedirect as e,basePermanentRedirect as t}from"../shared/redirects.js";function o(e){return function(t){const o=r();for(var n=arguments.length,c=new Array(n>1?n-1:0),s=1;s<n;s++)c[s-1]=arguments[s];return e({...t,locale:o},...c)}}const n=o(e),c=o(t);export{c as serverPermanentRedirect,n as serverRedirect};
@@ -1 +1 @@
1
- import{headers as e}from"next/headers";import{notFound as t}from"next/navigation";import{cache as n}from"react";import{HEADER_LOCALE_NAME as o}from"../../shared/constants.js";const r=n((function(){let n;try{n=e().get(o)}catch(e){throw e instanceof Error&&"DYNAMIC_SERVER_USAGE"===e.digest?new Error("Usage of next-intl APIs in Server Components currently opts into dynamic rendering. This limitation will eventually be lifted, but as a stopgap solution, you can use the `unstable_setRequestLocale` API to enable static rendering, see https://next-intl-docs.vercel.app/docs/getting-started/app-router/with-i18n-routing#static-rendering",{cause:e}):e}return n||(console.error("\nUnable to find `next-intl` locale because the middleware didn't run on this request. See https://next-intl-docs.vercel.app/docs/routing/middleware#unable-to-find-locale. The `notFound()` function will be called as a result.\n"),t()),n}));const i=n((function(){return{locale:void 0}}));function a(e){i().locale=e}function l(){return i().locale||r()}export{l as getRequestLocale,a as setRequestLocale};
1
+ import{headers as t}from"next/headers";import{cache as e}from"react";import{HEADER_LOCALE_NAME as n}from"../../shared/constants.js";import{getCachedRequestLocale as r}from"./RequestLocaleCache.js";const o=e((async function(){const e=t();return e instanceof Promise?await e:e}));const s=e((async function(){let t;try{t=(await o()).get(n)||void 0}catch(t){if(t instanceof Error&&"DYNAMIC_SERVER_USAGE"===t.digest){const e=new Error("Usage of next-intl APIs in Server Components currently opts into dynamic rendering. This limitation will eventually be lifted, but as a stopgap solution, you can use the `unstable_setRequestLocale` API to enable static rendering, see https://next-intl-docs.vercel.app/docs/getting-started/app-router/with-i18n-routing#static-rendering",{cause:t});throw e.digest=t.digest,e}throw t}return t}));async function i(){return r()||await s()}export{i as getRequestLocale};
@@ -0,0 +1 @@
1
+ import{cache as o}from"react";const n=o((function(){return{locale:void 0}}));function t(){return n().locale}function c(o){n().locale=o}export{t as getCachedRequestLocale,c as setCachedRequestLocale};
@@ -0,0 +1 @@
1
+ import{headers as e}from"next/headers";import{notFound as t}from"next/navigation";import{cache as n}from"react";import{HEADER_LOCALE_NAME as o}from"../../shared/constants.js";import{getCachedRequestLocale as r}from"./RequestLocaleCache.js";const i=n((function(){let n;try{n=e().get(o)}catch(e){throw e instanceof Error&&"DYNAMIC_SERVER_USAGE"===e.digest?new Error("Usage of next-intl APIs in Server Components currently opts into dynamic rendering. This limitation will eventually be lifted, but as a stopgap solution, you can use the `unstable_setRequestLocale` API to enable static rendering, see https://next-intl-docs.vercel.app/docs/getting-started/app-router/with-i18n-routing#static-rendering",{cause:e}):e}return n||(console.error("\nUnable to find `next-intl` locale because the middleware didn't run on this request. See https://next-intl-docs.vercel.app/docs/routing/middleware#unable-to-find-locale. The `notFound()` function will be called as a result.\n"),t()),n}));function s(){return r()||i()}export{s as getRequestLocale};
@@ -1 +1 @@
1
- import{cache as e}from"react";import{_createIntlFormatters as t,_createCache as o,initializeConfig as n}from"use-intl/core";import{getRequestLocale as r}from"./RequestLocale.js";import a from"next-intl/config";const i=e((function(){return new Date}));const s=e((function(){return Intl.DateTimeFormat().resolvedOptions().timeZone}));const c=e((async function(e,t){if("function"!=typeof e)throw new Error("Invalid i18n request configuration detected.\n\nPlease verify that:\n1. In case you've specified a custom location in your Next.js config, make sure that the path is correct.\n2. You have a default export in your i18n request configuration file.\n\nSee also: https://next-intl-docs.vercel.app/docs/usage/configuration#i18n-request\n");let o=!1;const n={get locale(){return o=!0,t||r()}};let a=e(n);return a instanceof Promise&&(a=await a),o?a.locale&&console.error("\nYou've read the `locale` param that was passed to `getRequestConfig` but have also returned one from the function. This is likely an error, please ensure that you're consistently using a setup with or without i18n routing: https://next-intl-docs.vercel.app/docs/getting-started/app-router\n"):a.locale||console.error("\nYou haven't read the `locale` param that was passed to `getRequestConfig` and also haven't returned one from the function. This is likely an error, please ensure that you're consistently using a setup with or without i18n routing: https://next-intl-docs.vercel.app/docs/getting-started/app-router\n"),{...a,locale:a.locale||n.locale,now:a.now||i(),timeZone:a.timeZone||s()}})),u=e(t),l=e(o);const p=e((async function(e){const t=await c(a,e);return{...n(t),_formatters:u(l())}}));export{p as default};
1
+ import{notFound as e}from"next/navigation";import{cache as t}from"react";import{_createIntlFormatters as n,_createCache as o,initializeConfig as r}from"use-intl/core";import{getRequestLocale as i}from"./RequestLocale.js";import{getRequestLocale as a}from"./RequestLocaleLegacy.js";import s from"next-intl/config";const c=t((function(){return new Date}));const l=t((function(){return Intl.DateTimeFormat().resolvedOptions().timeZone}));const u=t((async function(t,n){if("function"!=typeof t)throw new Error("Invalid i18n request configuration detected.\n\nPlease verify that:\n1. In case you've specified a custom location in your Next.js config, make sure that the path is correct.\n2. You have a default export in your i18n request configuration file.\n\nSee also: https://next-intl-docs.vercel.app/docs/usage/configuration#i18n-request\n");const o={get locale(){return n||a()},get requestLocale(){return n?Promise.resolve(n):i()}};let r=t(o);r instanceof Promise&&(r=await r);const s=r.locale||await o.requestLocale;return s||(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"),e()),{...r,locale:s,now:r.now||c(),timeZone:r.timeZone||l()}})),f=t(n),m=t(o);const d=t((async function(e){const t=await u(s,e);return{...r(t),_formatters:f(m())}}));export{d as default};
@@ -1 +1 @@
1
- export{default as getRequestConfig}from"./server/react-server/getRequestConfig.js";export{default as getFormatter}from"./server/react-server/getFormatter.js";export{default as getMessages}from"./server/react-server/getMessages.js";export{default as getNow}from"./server/react-server/getNow.js";export{default as getTimeZone}from"./server/react-server/getTimeZone.js";export{default as getTranslations}from"./server/react-server/getTranslations.js";export{default as getLocale}from"./server/react-server/getLocale.js";export{setRequestLocale as unstable_setRequestLocale}from"./server/react-server/RequestLocale.js";
1
+ export{default as getRequestConfig}from"./server/react-server/getRequestConfig.js";export{default as getFormatter}from"./server/react-server/getFormatter.js";export{default as getMessages}from"./server/react-server/getMessages.js";export{default as getNow}from"./server/react-server/getNow.js";export{default as getTimeZone}from"./server/react-server/getTimeZone.js";export{default as getTranslations}from"./server/react-server/getTranslations.js";export{default as getLocale}from"./server/react-server/getLocale.js";export{setCachedRequestLocale as unstable_setRequestLocale}from"./server/react-server/RequestLocaleCache.js";
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../../_virtual/_rollupPluginBabelHelpers.js"),r=require("react"),t=require("../../routing/config.js"),a=require("../../server/react-server/RequestLocale.js"),n=require("../shared/utils.js"),o=require("./ServerLink.js"),l=require("./redirects.js");function c(e){return e&&e.__esModule?e:{default:e}}var i=c(r);exports.default=function(r){const c=t.receiveRoutingConfig(r);function u(e){let{href:r,locale:t}=e;return n.compileLocalizedPathname({...n.normalizeNameOrNameWithParams(r),locale:t,pathnames:c.pathnames})}function s(e){return()=>{throw new Error("`".concat(e,"` is not supported in Server Components. You can use this hook if you convert the component to a Client Component."))}}return{Link:function(r){let{href:t,locale:l,...u}=r;const s=a.getRequestLocale(),f=l||s;return i.default.createElement(o.default,e.extends({href:n.compileLocalizedPathname({locale:f,pathname:t,params:"object"==typeof t?t.params:void 0,pathnames:c.pathnames}),locale:l,localePrefix:c.localePrefix},u))},redirect:function(e){const r=u({href:e,locale:a.getRequestLocale()});for(var t=arguments.length,n=new Array(t>1?t-1:0),o=1;o<t;o++)n[o-1]=arguments[o];return l.serverRedirect({localePrefix:c.localePrefix,pathname:r},...n)},permanentRedirect:function(e){const r=u({href:e,locale:a.getRequestLocale()});for(var t=arguments.length,n=new Array(t>1?t-1:0),o=1;o<t;o++)n[o-1]=arguments[o];return l.serverPermanentRedirect({localePrefix:c.localePrefix,pathname:r},...n)},getPathname:u,usePathname:s("usePathname"),useRouter:s("useRouter")}};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../../_virtual/_rollupPluginBabelHelpers.js"),r=require("react"),t=require("../../routing/config.js"),a=require("../../server/react-server/RequestLocaleLegacy.js"),n=require("../shared/utils.js"),o=require("./ServerLink.js"),l=require("./redirects.js");function c(e){return e&&e.__esModule?e:{default:e}}var i=c(r);exports.default=function(r){const c=t.receiveRoutingConfig(r);function u(e){let{href:r,locale:t}=e;return n.compileLocalizedPathname({...n.normalizeNameOrNameWithParams(r),locale:t,pathnames:c.pathnames})}function s(e){return()=>{throw new Error("`".concat(e,"` is not supported in Server Components. You can use this hook if you convert the component to a Client Component."))}}return{Link:function(r){let{href:t,locale:l,...u}=r;const s=a.getRequestLocale(),f=l||s;return i.default.createElement(o.default,e.extends({href:n.compileLocalizedPathname({locale:f,pathname:t,params:"object"==typeof t?t.params:void 0,pathnames:c.pathnames}),locale:l,localePrefix:c.localePrefix},u))},redirect:function(e){const r=u({href:e,locale:a.getRequestLocale()});for(var t=arguments.length,n=new Array(t>1?t-1:0),o=1;o<t;o++)n[o-1]=arguments[o];return l.serverRedirect({localePrefix:c.localePrefix,pathname:r},...n)},permanentRedirect:function(e){const r=u({href:e,locale:a.getRequestLocale()});for(var t=arguments.length,n=new Array(t>1?t-1:0),o=1;o<t;o++)n[o-1]=arguments[o];return l.serverPermanentRedirect({localePrefix:c.localePrefix,pathname:r},...n)},getPathname:u,usePathname:s("usePathname"),useRouter:s("useRouter")}};
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../../server/react-server/RequestLocale.js"),r=require("../shared/redirects.js");function t(r){return function(t){const s=e.getRequestLocale();for(var c=arguments.length,n=new Array(c>1?c-1:0),a=1;a<c;a++)n[a-1]=arguments[a];return r({...t,locale:s},...n)}}const s=t(r.baseRedirect),c=t(r.basePermanentRedirect);exports.serverPermanentRedirect=c,exports.serverRedirect=s;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../../server/react-server/RequestLocaleLegacy.js"),r=require("../shared/redirects.js");function t(r){return function(t){const s=e.getRequestLocale();for(var c=arguments.length,n=new Array(c>1?c-1:0),a=1;a<c;a++)n[a-1]=arguments[a];return r({...t,locale:s},...n)}}const s=t(r.baseRedirect),c=t(r.basePermanentRedirect);exports.serverPermanentRedirect=c,exports.serverRedirect=s;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("next/headers"),t=require("next/navigation"),n=require("react"),r=require("../../shared/constants.js");const o=n.cache((function(){let n;try{n=e.headers().get(r.HEADER_LOCALE_NAME)}catch(e){throw e instanceof Error&&"DYNAMIC_SERVER_USAGE"===e.digest?new Error("Usage of next-intl APIs in Server Components currently opts into dynamic rendering. This limitation will eventually be lifted, but as a stopgap solution, you can use the `unstable_setRequestLocale` API to enable static rendering, see https://next-intl-docs.vercel.app/docs/getting-started/app-router/with-i18n-routing#static-rendering",{cause:e}):e}return n||t.notFound(),n}));const s=n.cache((function(){return{locale:void 0}}));exports.getRequestLocale=function(){return s().locale||o()},exports.setRequestLocale=function(e){s().locale=e};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("next/headers"),t=require("react"),n=require("../../shared/constants.js"),r=require("./RequestLocaleCache.js");const s=t.cache((async function(){const t=e.headers();return t instanceof Promise?await t:t}));const a=t.cache((async function(){let e;try{e=(await s()).get(n.HEADER_LOCALE_NAME)||void 0}catch(e){if(e instanceof Error&&"DYNAMIC_SERVER_USAGE"===e.digest){const t=new Error("Usage of next-intl APIs in Server Components currently opts into dynamic rendering. This limitation will eventually be lifted, but as a stopgap solution, you can use the `unstable_setRequestLocale` API to enable static rendering, see https://next-intl-docs.vercel.app/docs/getting-started/app-router/with-i18n-routing#static-rendering",{cause:e});throw t.digest=e.digest,t}throw e}return e}));exports.getRequestLocale=async function(){return r.getCachedRequestLocale()||await a()};
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e=require("react").cache((function(){return{locale:void 0}}));exports.getCachedRequestLocale=function(){return e().locale},exports.setCachedRequestLocale=function(t){e().locale=t};
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("next/headers"),t=require("next/navigation"),r=require("react"),n=require("../../shared/constants.js"),s=require("./RequestLocaleCache.js");const a=r.cache((function(){let r;try{r=e.headers().get(n.HEADER_LOCALE_NAME)}catch(e){throw e instanceof Error&&"DYNAMIC_SERVER_USAGE"===e.digest?new Error("Usage of next-intl APIs in Server Components currently opts into dynamic rendering. This limitation will eventually be lifted, but as a stopgap solution, you can use the `unstable_setRequestLocale` API to enable static rendering, see https://next-intl-docs.vercel.app/docs/getting-started/app-router/with-i18n-routing#static-rendering",{cause:e}):e}return r||t.notFound(),r}));exports.getRequestLocale=function(){return s.getCachedRequestLocale()||a()};
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("use-intl/core"),n=require("./RequestLocale.js");function c(e){return e&&e.__esModule?e:{default:e}}var r=c(require("next-intl/config"));const o=e.cache((function(){return new Date}));const a=e.cache((function(){return Intl.DateTimeFormat().resolvedOptions().timeZone}));const i=e.cache((async function(e,t){const c={get locale(){return t||n.getRequestLocale()}};let r=e(c);return r instanceof Promise&&(r=await r),{...r,locale:r.locale||c.locale,now:r.now||o(),timeZone:r.timeZone||a()}})),u=e.cache(t._createIntlFormatters),s=e.cache(t._createCache);const l=e.cache((async function(e){const n=await i(r.default,e);return{...t.initializeConfig(n),_formatters:u(s())}}));exports.default=l;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("next/navigation"),t=require("react"),n=require("use-intl/core"),r=require("./RequestLocale.js"),o=require("./RequestLocaleLegacy.js");function c(e){return e&&e.__esModule?e:{default:e}}var a=c(require("next-intl/config"));const i=t.cache((function(){return new Date}));const u=t.cache((function(){return Intl.DateTimeFormat().resolvedOptions().timeZone}));const s=t.cache((async function(t,n){const c={get locale(){return n||o.getRequestLocale()},get requestLocale(){return n?Promise.resolve(n):r.getRequestLocale()}};let a=t(c);a instanceof Promise&&(a=await a);const s=a.locale||await c.requestLocale;return s||e.notFound(),{...a,locale:s,now:a.now||i(),timeZone:a.timeZone||u()}})),l=t.cache(n._createIntlFormatters),f=t.cache(n._createCache);const q=t.cache((async function(e){const t=await s(a.default,e);return{...n.initializeConfig(t),_formatters:l(f())}}));exports.default=q;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./server/react-server/getRequestConfig.js"),r=require("./server/react-server/getFormatter.js"),t=require("./server/react-server/getMessages.js"),s=require("./server/react-server/getNow.js"),a=require("./server/react-server/getTimeZone.js"),o=require("./server/react-server/getTranslations.js"),u=require("./server/react-server/getLocale.js"),g=require("./server/react-server/RequestLocale.js");exports.getRequestConfig=e.default,exports.getFormatter=r.default,exports.getMessages=t.default,exports.getNow=s.default,exports.getTimeZone=a.default,exports.getTranslations=o.default,exports.getLocale=u.default,exports.unstable_setRequestLocale=g.setRequestLocale;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./server/react-server/getRequestConfig.js"),r=require("./server/react-server/getFormatter.js"),t=require("./server/react-server/getMessages.js"),s=require("./server/react-server/getNow.js"),a=require("./server/react-server/getTimeZone.js"),o=require("./server/react-server/getTranslations.js"),u=require("./server/react-server/getLocale.js"),g=require("./server/react-server/RequestLocaleCache.js");exports.getRequestConfig=e.default,exports.getFormatter=r.default,exports.getMessages=t.default,exports.getNow=s.default,exports.getTimeZone=a.default,exports.getTranslations=o.default,exports.getLocale=u.default,exports.unstable_setRequestLocale=g.setCachedRequestLocale;
@@ -1,2 +1 @@
1
- export declare function setRequestLocale(locale: string): void;
2
- export declare function getRequestLocale(): string;
1
+ export declare function getRequestLocale(): Promise<string | undefined>;
@@ -0,0 +1,2 @@
1
+ export declare function getCachedRequestLocale(): string | undefined;
2
+ export declare function setCachedRequestLocale(locale: string): void;
@@ -0,0 +1 @@
1
+ export declare function getRequestLocale(): string;
@@ -1,4 +1,3 @@
1
- import type { IntlConfig } from 'use-intl/core';
2
- import type { GetRequestConfigParams } from './getRequestConfig';
3
- declare const _default: (params: GetRequestConfigParams) => IntlConfig | Promise<IntlConfig>;
1
+ import type { GetRequestConfigParams, RequestConfig } from './getRequestConfig';
2
+ declare const _default: (params: GetRequestConfigParams) => RequestConfig | Promise<RequestConfig>;
4
3
  export default _default;
@@ -1,20 +1,46 @@
1
1
  import type { IntlConfig } from 'use-intl/core';
2
- type RequestConfig = Omit<IntlConfig, 'locale'> & {
2
+ export type RequestConfig = Omit<IntlConfig, 'locale'> & {
3
3
  /**
4
- * Instead of reading a `locale` from the argument that's passed to the
4
+ * Instead of reading a `requestLocale` from the argument that's passed to the
5
5
  * function within `getRequestConfig`, you can include a locale as part of the
6
6
  * returned request configuration.
7
7
  *
8
- * This is helpful for apps that only support a single language and for apps
9
- * where the locale should be read from user settings instead of the pathname.
8
+ * This can be helpful for the following use cases:
9
+ * - Apps that only support a single language
10
+ * - Apps where the locale should be read from user settings instead of the pathname
11
+ * - Providing a fallback locale in case the locale was not matched by the middleware
10
12
  **/
11
13
  locale?: IntlConfig['locale'];
12
14
  };
13
15
  export type GetRequestConfigParams = {
16
+ /**
17
+ * Deprecated in favor of `requestLocale` (see https://github.com/amannn/next-intl/pull/1383).
18
+ *
19
+ * The locale that was matched by the `[locale]` path segment. Note however
20
+ * that this can be overridden in async APIs when the `locale` is explicitly
21
+ * passed (e.g. `getTranslations({locale: 'en'})`).
22
+ *
23
+ * @deprecated
24
+ */
14
25
  locale: string;
26
+ /**
27
+ * Typically corresponds to the `[locale]` segment that was matched by the middleware.
28
+ *
29
+ * However, there are three special cases to consider:
30
+ * 1. **Overrides**: When an explicit `locale` is passed to awaitable functions
31
+ * like `getTranslations({locale: 'en'})`, then this value will be used
32
+ * instead of the segment.
33
+ * 2. **`undefined`**: The value can be `undefined` when a page outside of the
34
+ * `[locale]` segment renders (e.g. a language selection page at `app/page.tsx`).
35
+ * 3. **Invalid values**: Since the `[locale]` segment effectively acts like a
36
+ * catch-all for unknown routes (e.g. `/unknown.txt`), invalid values should
37
+ * be replaced with a valid locale.
38
+ *
39
+ * @see https://next-intl-docs.vercel.app/docs/usage/configuration#i18n-request
40
+ */
41
+ requestLocale: Promise<string | undefined>;
15
42
  };
16
43
  /**
17
- * Should be called in `i18n.ts` to create the configuration for the current request.
44
+ * Should be called in `i18n/request.ts` to create the configuration for the current request.
18
45
  */
19
46
  export default function getRequestConfig(createRequestConfig: (params: GetRequestConfigParams) => RequestConfig | Promise<RequestConfig>): (params: GetRequestConfigParams) => RequestConfig | Promise<RequestConfig>;
20
- export {};
@@ -8,4 +8,4 @@ export { default as getTimeZone } from './getTimeZone';
8
8
  export { default as getTranslations } from './getTranslations';
9
9
  export { default as getMessages } from './getMessages';
10
10
  export { default as getLocale } from './getLocale';
11
- export { setRequestLocale as unstable_setRequestLocale } from './RequestLocale';
11
+ export { setCachedRequestLocale as unstable_setRequestLocale } from './RequestLocaleCache';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-intl",
3
- "version": "3.20.1-canary.0",
3
+ "version": "3.21.0-canary.0",
4
4
  "sideEffects": false,
5
5
  "author": "Jan Amann <jan@amann.work>",
6
6
  "funding": [
@@ -87,11 +87,11 @@
87
87
  "dependencies": {
88
88
  "@formatjs/intl-localematcher": "^0.5.4",
89
89
  "negotiator": "^0.6.3",
90
- "use-intl": "^3.20.1-canary.0"
90
+ "use-intl": "^3.21.0-canary.0"
91
91
  },
92
92
  "peerDependencies": {
93
93
  "next": "^10.0.0 || ^11.0.0 || ^12.0.0 || ^13.0.0 || ^14.0.0",
94
94
  "react": "^16.8.0 || ^17.0.0 || ^18.0.0"
95
95
  },
96
- "gitHead": "35e42dd87ee3cc5ec4c57494942ca897844076e7"
96
+ "gitHead": "3105da4fefa30e07be10849ca72e515034e5fe9b"
97
97
  }