next-intl 3.21.1 → 3.22.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 (111) hide show
  1. package/dist/development/middleware/middleware.js +11 -10
  2. package/dist/development/middleware/resolveLocale.js +2 -7
  3. package/dist/development/middleware/syncCookie.js +3 -2
  4. package/dist/development/navigation/react-client/ClientLink.js +2 -2
  5. package/dist/development/navigation/react-client/createLocalizedPathnamesNavigation.js +3 -0
  6. package/dist/development/navigation/react-client/createNavigation.js +83 -0
  7. package/dist/development/navigation/react-client/createSharedPathnamesNavigation.js +3 -0
  8. package/dist/development/navigation/react-client/useBasePathname.js +0 -14
  9. package/dist/development/navigation/react-server/ServerLink.js +4 -2
  10. package/dist/development/navigation/react-server/createLocalizedPathnamesNavigation.js +4 -4
  11. package/dist/development/navigation/react-server/createNavigation.js +30 -0
  12. package/dist/development/navigation/react-server/redirects.js +2 -2
  13. package/dist/development/navigation/shared/BaseLink.js +28 -25
  14. package/dist/development/navigation/shared/LegacyBaseLink.js +55 -0
  15. package/dist/development/navigation/shared/createSharedNavigationFns.js +147 -0
  16. package/dist/development/navigation/shared/redirects.js +3 -0
  17. package/dist/development/navigation/shared/utils.js +40 -1
  18. package/dist/development/navigation.react-client.js +2 -0
  19. package/dist/development/navigation.react-server.js +2 -0
  20. package/dist/development/react-client/index.js +0 -2
  21. package/dist/development/routing/config.js +1 -1
  22. package/dist/development/server/react-server/RequestLocale.js +15 -25
  23. package/dist/development/server/react-server/RequestLocaleCache.js +23 -0
  24. package/dist/development/server/react-server/RequestLocaleLegacy.js +43 -0
  25. package/dist/development/server/react-server/getConfig.js +15 -17
  26. package/dist/development/server/react-server/getRequestConfig.js +1 -1
  27. package/dist/development/server.react-server.js +2 -2
  28. package/dist/esm/middleware/middleware.js +1 -1
  29. package/dist/esm/middleware/resolveLocale.js +1 -1
  30. package/dist/esm/middleware/syncCookie.js +1 -1
  31. package/dist/esm/navigation/react-client/ClientLink.js +1 -1
  32. package/dist/esm/navigation/react-client/createNavigation.js +1 -0
  33. package/dist/esm/navigation/react-client/useBasePathname.js +0 -1
  34. package/dist/esm/navigation/react-server/ServerLink.js +1 -1
  35. package/dist/esm/navigation/react-server/createLocalizedPathnamesNavigation.js +1 -1
  36. package/dist/esm/navigation/react-server/createNavigation.js +1 -0
  37. package/dist/esm/navigation/react-server/redirects.js +1 -1
  38. package/dist/esm/navigation/shared/BaseLink.js +1 -1
  39. package/dist/esm/navigation/shared/LegacyBaseLink.js +2 -0
  40. package/dist/esm/navigation/shared/createSharedNavigationFns.js +1 -0
  41. package/dist/esm/navigation/shared/utils.js +1 -1
  42. package/dist/esm/navigation.react-client.js +1 -1
  43. package/dist/esm/navigation.react-server.js +1 -1
  44. package/dist/esm/routing/config.js +1 -1
  45. package/dist/esm/server/react-server/RequestLocale.js +1 -1
  46. package/dist/esm/server/react-server/RequestLocaleCache.js +1 -0
  47. package/dist/esm/server/react-server/RequestLocaleLegacy.js +1 -0
  48. package/dist/esm/server/react-server/getConfig.js +1 -1
  49. package/dist/esm/server.react-server.js +1 -1
  50. package/dist/production/middleware/middleware.js +1 -1
  51. package/dist/production/middleware/resolveLocale.js +1 -1
  52. package/dist/production/middleware/syncCookie.js +1 -1
  53. package/dist/production/navigation/react-client/ClientLink.js +1 -1
  54. package/dist/production/navigation/react-client/createNavigation.js +1 -0
  55. package/dist/production/navigation/react-client/useBasePathname.js +0 -1
  56. package/dist/production/navigation/react-server/ServerLink.js +1 -1
  57. package/dist/production/navigation/react-server/createLocalizedPathnamesNavigation.js +1 -1
  58. package/dist/production/navigation/react-server/createNavigation.js +1 -0
  59. package/dist/production/navigation/react-server/redirects.js +1 -1
  60. package/dist/production/navigation/shared/BaseLink.js +1 -1
  61. package/dist/production/navigation/shared/LegacyBaseLink.js +2 -0
  62. package/dist/production/navigation/shared/createSharedNavigationFns.js +1 -0
  63. package/dist/production/navigation/shared/utils.js +1 -1
  64. package/dist/production/navigation.react-client.js +1 -1
  65. package/dist/production/navigation.react-server.js +1 -1
  66. package/dist/production/routing/config.js +1 -1
  67. package/dist/production/server/react-server/RequestLocale.js +1 -1
  68. package/dist/production/server/react-server/RequestLocaleCache.js +1 -0
  69. package/dist/production/server/react-server/RequestLocaleLegacy.js +1 -0
  70. package/dist/production/server/react-server/getConfig.js +1 -1
  71. package/dist/production/server.react-server.js +1 -1
  72. package/dist/types/src/middleware/config.d.ts +16 -2
  73. package/dist/types/src/middleware/getAlternateLinksHeaderValue.d.ts +3 -3
  74. package/dist/types/src/middleware/middleware.d.ts +2 -2
  75. package/dist/types/src/middleware/resolveLocale.d.ts +2 -2
  76. package/dist/types/src/middleware/syncCookie.d.ts +2 -1
  77. package/dist/types/src/middleware/utils.d.ts +4 -4
  78. package/dist/types/src/navigation/createNavigation.test.d.ts +1 -0
  79. package/dist/types/src/navigation/react-client/ClientLink.d.ts +6 -6
  80. package/dist/types/src/navigation/react-client/createLocalizedPathnamesNavigation.d.ts +15 -4
  81. package/dist/types/src/navigation/react-client/createNavigation.d.ts +467 -0
  82. package/dist/types/src/navigation/react-client/createNavigation.test.d.ts +1 -0
  83. package/dist/types/src/navigation/react-client/createSharedPathnamesNavigation.d.ts +7 -4
  84. package/dist/types/src/navigation/react-client/index.d.ts +1 -0
  85. package/dist/types/src/navigation/react-client/redirects.d.ts +4 -4
  86. package/dist/types/src/navigation/react-client/useBasePathname.d.ts +2 -15
  87. package/dist/types/src/navigation/react-client/useBaseRouter.d.ts +2 -2
  88. package/dist/types/src/navigation/react-server/ServerLink.d.ts +5 -5
  89. package/dist/types/src/navigation/react-server/createLocalizedPathnamesNavigation.d.ts +12 -4
  90. package/dist/types/src/navigation/react-server/createNavigation.d.ts +385 -0
  91. package/dist/types/src/navigation/react-server/createNavigation.test.d.ts +1 -0
  92. package/dist/types/src/navigation/react-server/createSharedPathnamesNavigation.d.ts +3 -3
  93. package/dist/types/src/navigation/react-server/index.d.ts +1 -0
  94. package/dist/types/src/navigation/react-server/redirects.d.ts +4 -4
  95. package/dist/types/src/navigation/shared/BaseLink.d.ts +11 -6
  96. package/dist/types/src/navigation/shared/LegacyBaseLink.d.ts +10 -0
  97. package/dist/types/src/navigation/shared/createSharedNavigationFns.d.ts +382 -0
  98. package/dist/types/src/navigation/shared/redirects.d.ts +5 -5
  99. package/dist/types/src/navigation/shared/utils.d.ts +10 -3
  100. package/dist/types/src/routing/config.d.ts +19 -11
  101. package/dist/types/src/routing/defineRouting.d.ts +2 -2
  102. package/dist/types/src/routing/types.d.ts +4 -4
  103. package/dist/types/src/server/react-server/RequestLocale.d.ts +1 -2
  104. package/dist/types/src/server/react-server/RequestLocaleCache.d.ts +2 -0
  105. package/dist/types/src/server/react-server/RequestLocaleLegacy.d.ts +1 -0
  106. package/dist/types/src/server/react-server/createRequestConfig.d.ts +2 -3
  107. package/dist/types/src/server/react-server/getRequestConfig.d.ts +32 -6
  108. package/dist/types/src/server/react-server/index.d.ts +1 -1
  109. package/dist/types/src/shared/types.d.ts +3 -0
  110. package/dist/types/src/shared/utils.d.ts +2 -2
  111. package/package.json +3 -3
@@ -12,11 +12,12 @@ var syncCookie = require('./syncCookie.js');
12
12
  var utils = require('./utils.js');
13
13
 
14
14
  function createMiddleware(routing, options) {
15
- var _ref, _options$alternateLin, _ref2, _options$localeDetect;
15
+ var _ref, _options$alternateLin, _ref2, _options$localeDetect, _ref3, _options$localeCookie;
16
16
  const resolvedRouting = config.receiveRoutingConfig(routing);
17
17
  const resolvedOptions = {
18
18
  alternateLinks: (_ref = (_options$alternateLin = options === null || options === void 0 ? void 0 : options.alternateLinks) !== null && _options$alternateLin !== void 0 ? _options$alternateLin : routing.alternateLinks) !== null && _ref !== void 0 ? _ref : true,
19
- localeDetection: (_ref2 = (_options$localeDetect = options === null || options === void 0 ? void 0 : options.localeDetection) !== null && _options$localeDetect !== void 0 ? _options$localeDetect : routing === null || routing === void 0 ? void 0 : routing.localeDetection) !== null && _ref2 !== void 0 ? _ref2 : true
19
+ localeDetection: (_ref2 = (_options$localeDetect = options === null || options === void 0 ? void 0 : options.localeDetection) !== null && _options$localeDetect !== void 0 ? _options$localeDetect : routing === null || routing === void 0 ? void 0 : routing.localeDetection) !== null && _ref2 !== void 0 ? _ref2 : true,
20
+ localeCookie: (_ref3 = (_options$localeCookie = options === null || options === void 0 ? void 0 : options.localeCookie) !== null && _options$localeCookie !== void 0 ? _options$localeCookie : routing === null || routing === void 0 ? void 0 : routing.localeCookie) !== null && _ref3 !== void 0 ? _ref3 : true
20
21
  };
21
22
  return function middleware(request) {
22
23
  var _resolvedRouting$doma;
@@ -55,7 +56,7 @@ function createMiddleware(routing, options) {
55
56
  }
56
57
  function redirect(url, redirectDomain) {
57
58
  const urlObj = new URL(utils$1.normalizeTrailingSlash(url), request.url);
58
- if (domainsConfig.length > 0 && !redirectDomain) {
59
+ if (domainsConfig.length > 0 && !redirectDomain && domain) {
59
60
  const bestMatchingDomain = utils.getBestMatchingDomain(domain, locale, domainsConfig);
60
61
  if (bestMatchingDomain) {
61
62
  redirectDomain = bestMatchingDomain.domain;
@@ -84,11 +85,12 @@ function createMiddleware(routing, options) {
84
85
  let response;
85
86
  let internalTemplateName;
86
87
  let unprefixedInternalPathname = unprefixedExternalPathname;
87
- if ('pathnames' in resolvedRouting) {
88
+ const pathnames = resolvedRouting.pathnames;
89
+ if (pathnames) {
88
90
  let resolvedTemplateLocale;
89
- [resolvedTemplateLocale, internalTemplateName] = utils.getInternalTemplate(resolvedRouting.pathnames, unprefixedExternalPathname, locale);
91
+ [resolvedTemplateLocale, internalTemplateName] = utils.getInternalTemplate(pathnames, unprefixedExternalPathname, locale);
90
92
  if (internalTemplateName) {
91
- const pathnameConfig = resolvedRouting.pathnames[internalTemplateName];
93
+ const pathnameConfig = pathnames[internalTemplateName];
92
94
  const localeTemplate = typeof pathnameConfig === 'string' ? pathnameConfig :
93
95
  // @ts-expect-error -- This is fine
94
96
  pathnameConfig[locale];
@@ -152,14 +154,13 @@ function createMiddleware(routing, options) {
152
154
  }
153
155
  }
154
156
  }
155
- if (resolvedOptions.localeDetection) {
156
- syncCookie.default(request, response, locale);
157
+ if (resolvedOptions.localeDetection && resolvedOptions.localeCookie) {
158
+ syncCookie.default(request, response, locale, resolvedOptions.localeCookie);
157
159
  }
158
160
  if (resolvedRouting.localePrefix.mode !== 'never' && resolvedOptions.alternateLinks && resolvedRouting.locales.length > 1) {
159
- var _resolvedRouting$path;
160
161
  response.headers.set('Link', getAlternateLinksHeaderValue.default({
161
162
  routing: resolvedRouting,
162
- localizedPathnames: internalTemplateName != null && 'pathnames' in resolvedRouting ? (_resolvedRouting$path = resolvedRouting.pathnames) === null || _resolvedRouting$path === void 0 ? void 0 : _resolvedRouting$path[internalTemplateName] : undefined,
163
+ localizedPathnames: internalTemplateName != null && pathnames ? pathnames === null || pathnames === void 0 ? void 0 : pathnames[internalTemplateName] : undefined,
163
164
  request,
164
165
  resolvedLocale: locale
165
166
  }));
@@ -12,11 +12,7 @@ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
12
12
  var Negotiator__default = /*#__PURE__*/_interopDefault(Negotiator);
13
13
 
14
14
  function findDomainFromHost(requestHeaders, domains) {
15
- var _host;
16
- let host = utils.getHost(requestHeaders);
17
-
18
- // Remove port (easier for local development)
19
- host = (_host = host) === null || _host === void 0 ? void 0 : _host.replace(/:\d+$/, '');
15
+ const host = utils.getHost(requestHeaders);
20
16
  if (host && domains) {
21
17
  return domains.find(cur => cur.domain === host);
22
18
  }
@@ -139,8 +135,7 @@ function resolveLocaleFromDomain(routing, options, requestHeaders, requestCookie
139
135
  }
140
136
  function resolveLocale(routing, options, requestHeaders, requestCookies, pathname) {
141
137
  if (routing.domains) {
142
- const routingWithDomains = routing;
143
- return resolveLocaleFromDomain(routingWithDomains, options, requestHeaders, requestCookies, pathname);
138
+ return resolveLocaleFromDomain(routing, options, requestHeaders, requestCookies, pathname);
144
139
  } else {
145
140
  return {
146
141
  locale: resolveLocaleFromPrefix(routing, options, requestHeaders, requestCookies, pathname)
@@ -4,14 +4,15 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var constants = require('../shared/constants.js');
6
6
 
7
- function syncCookie(request, response, locale) {
7
+ function syncCookie(request, response, locale, localeCookie) {
8
8
  var _request$cookies$get;
9
9
  const hasOutdatedCookie = ((_request$cookies$get = request.cookies.get(constants.COOKIE_LOCALE_NAME)) === null || _request$cookies$get === void 0 ? void 0 : _request$cookies$get.value) !== locale;
10
10
  if (hasOutdatedCookie) {
11
11
  response.cookies.set(constants.COOKIE_LOCALE_NAME, locale, {
12
12
  path: request.nextUrl.basePath || undefined,
13
13
  sameSite: constants.COOKIE_SAME_SITE,
14
- maxAge: constants.COOKIE_MAX_AGE
14
+ maxAge: constants.COOKIE_MAX_AGE,
15
+ ...(typeof localeCookie === 'object' && localeCookie)
15
16
  });
16
17
  }
17
18
  }
@@ -6,7 +6,7 @@ var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelper
6
6
  var React = require('react');
7
7
  var useLocale = require('../../react-client/useLocale.js');
8
8
  var utils = require('../../shared/utils.js');
9
- var BaseLink = require('../shared/BaseLink.js');
9
+ var LegacyBaseLink = require('../shared/LegacyBaseLink.js');
10
10
 
11
11
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
12
12
 
@@ -21,7 +21,7 @@ function ClientLink(_ref, ref) {
21
21
  const defaultLocale = useLocale.default();
22
22
  const finalLocale = locale || defaultLocale;
23
23
  const prefix = utils.getLocalePrefix(finalLocale, localePrefix);
24
- return /*#__PURE__*/React__default.default.createElement(BaseLink.default, _rollupPluginBabelHelpers.extends({
24
+ return /*#__PURE__*/React__default.default.createElement(LegacyBaseLink.default, _rollupPluginBabelHelpers.extends({
25
25
  ref: ref,
26
26
  locale: finalLocale,
27
27
  localePrefixMode: localePrefix.mode,
@@ -16,6 +16,9 @@ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
16
16
 
17
17
  var React__default = /*#__PURE__*/_interopDefault(React);
18
18
 
19
+ /**
20
+ * @deprecated Consider switching to `createNavigation` (see https://github.com/amannn/next-intl/pull/1316)
21
+ **/
19
22
  function createLocalizedPathnamesNavigation(routing) {
20
23
  const config$1 = config.receiveRoutingConfig(routing);
21
24
  function useTypedLocale() {
@@ -0,0 +1,83 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var navigation = require('next/navigation');
6
+ var React = require('react');
7
+ var useLocale = require('../../react-client/useLocale.js');
8
+ var createSharedNavigationFns = require('../shared/createSharedNavigationFns.js');
9
+ var syncLocaleCookie = require('../shared/syncLocaleCookie.js');
10
+ var utils = require('../shared/utils.js');
11
+ var useBasePathname = require('./useBasePathname.js');
12
+
13
+ function createNavigation(routing) {
14
+ function useTypedLocale() {
15
+ return useLocale.default();
16
+ }
17
+ const {
18
+ Link,
19
+ config,
20
+ getPathname,
21
+ ...redirects
22
+ } = createSharedNavigationFns.default(useTypedLocale, routing);
23
+
24
+ /** @see https://next-intl-docs.vercel.app/docs/routing/navigation#usepathname */
25
+ function usePathname() {
26
+ const pathname = useBasePathname.default(config.localePrefix);
27
+ const locale = useTypedLocale();
28
+
29
+ // @ts-expect-error -- Mirror the behavior from Next.js, where `null` is returned when `usePathname` is used outside of Next, but the types indicate that a string is always returned.
30
+ return React.useMemo(() => pathname &&
31
+ // @ts-expect-error -- This is fine
32
+ config.pathnames ? utils.getRoute(locale, pathname,
33
+ // @ts-expect-error -- This is fine
34
+ config.pathnames) : pathname, [locale, pathname]);
35
+ }
36
+ function useRouter() {
37
+ const router = navigation.useRouter();
38
+ const curLocale = useTypedLocale();
39
+ const nextPathname = navigation.usePathname();
40
+ return React.useMemo(() => {
41
+ function createHandler(fn) {
42
+ return function handler(href, options) {
43
+ const {
44
+ locale: nextLocale,
45
+ ...rest
46
+ } = options || {};
47
+
48
+ // @ts-expect-error -- We're passing a domain here just in case
49
+ const pathname = getPathname({
50
+ href,
51
+ locale: nextLocale || curLocale,
52
+ domain: window.location.host
53
+ });
54
+ const args = [pathname];
55
+ if (Object.keys(rest).length > 0) {
56
+ // @ts-expect-error -- This is fine
57
+ args.push(rest);
58
+ }
59
+ fn(...args);
60
+ syncLocaleCookie.default(nextPathname, curLocale, nextLocale);
61
+ };
62
+ }
63
+ return {
64
+ ...router,
65
+ /** @see https://next-intl-docs.vercel.app/docs/routing/navigation#userouter */
66
+ push: createHandler(router.push),
67
+ /** @see https://next-intl-docs.vercel.app/docs/routing/navigation#userouter */
68
+ replace: createHandler(router.replace),
69
+ /** @see https://next-intl-docs.vercel.app/docs/routing/navigation#userouter */
70
+ prefetch: createHandler(router.prefetch)
71
+ };
72
+ }, [curLocale, nextPathname, router]);
73
+ }
74
+ return {
75
+ ...redirects,
76
+ Link,
77
+ usePathname,
78
+ useRouter,
79
+ getPathname
80
+ };
81
+ }
82
+
83
+ exports.default = createNavigation;
@@ -14,6 +14,9 @@ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
14
14
 
15
15
  var React__default = /*#__PURE__*/_interopDefault(React);
16
16
 
17
+ /**
18
+ * @deprecated Consider switching to `createNavigation` (see https://github.com/amannn/next-intl/pull/1316)
19
+ **/
17
20
  function createSharedPathnamesNavigation(routing) {
18
21
  const localePrefix = config.receiveLocalePrefixConfig(routing === null || routing === void 0 ? void 0 : routing.localePrefix);
19
22
  function Link(props, ref) {
@@ -1,4 +1,3 @@
1
- "use client";
2
1
  'use strict';
3
2
 
4
3
  Object.defineProperty(exports, '__esModule', { value: true });
@@ -8,19 +7,6 @@ var React = require('react');
8
7
  var useLocale = require('../../react-client/useLocale.js');
9
8
  var utils = require('../../shared/utils.js');
10
9
 
11
- /**
12
- * Returns the pathname without a potential locale prefix.
13
- *
14
- * @example
15
- * ```tsx
16
- * 'use client';
17
- *
18
- * import {usePathname} from 'next-intl/client';
19
- *
20
- * // When the user is on `/en`, this will be `/`
21
- * const pathname = usePathname();
22
- * ```
23
- */
24
10
  function useBasePathname(localePrefix) {
25
11
  // The types aren't entirely correct here. Outside of Next.js
26
12
  // `useParams` can be called, but the return type is `null`.
@@ -5,13 +5,15 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
6
6
  var React = require('react');
7
7
  var utils = require('../../shared/utils.js');
8
- var BaseLink = require('../shared/BaseLink.js');
8
+ var LegacyBaseLink = require('../shared/LegacyBaseLink.js');
9
9
  var getLocale = require('../../server/react-server/getLocale.js');
10
10
 
11
11
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
12
12
 
13
13
  var React__default = /*#__PURE__*/_interopDefault(React);
14
14
 
15
+ // Only used by legacy navigation APIs, can be removed when they are removed
16
+
15
17
  async function ServerLink(_ref) {
16
18
  let {
17
19
  locale,
@@ -20,7 +22,7 @@ async function ServerLink(_ref) {
20
22
  } = _ref;
21
23
  const finalLocale = locale || (await getLocale.default());
22
24
  const prefix = utils.getLocalePrefix(finalLocale, localePrefix);
23
- return /*#__PURE__*/React__default.default.createElement(BaseLink.default, _rollupPluginBabelHelpers.extends({
25
+ return /*#__PURE__*/React__default.default.createElement(LegacyBaseLink.default, _rollupPluginBabelHelpers.extends({
24
26
  locale: finalLocale,
25
27
  localePrefixMode: localePrefix.mode,
26
28
  prefix: prefix
@@ -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
@@ -0,0 +1,30 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var RequestLocale = require('../../server/react-server/RequestLocale.js');
6
+ var createSharedNavigationFns = require('../shared/createSharedNavigationFns.js');
7
+
8
+ function createNavigation(routing) {
9
+ function getLocale() {
10
+ return RequestLocale.getRequestLocale();
11
+ }
12
+
13
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
14
+ const {
15
+ config,
16
+ ...fns
17
+ } = createSharedNavigationFns.default(getLocale, routing);
18
+ function notSupported(hookName) {
19
+ return () => {
20
+ throw new Error("`".concat(hookName, "` is not supported in Server Components. You can use this hook if you convert the calling component to a Client Component."));
21
+ };
22
+ }
23
+ return {
24
+ ...fns,
25
+ usePathname: notSupported('usePathname'),
26
+ useRouter: notSupported('useRouter')
27
+ };
28
+ }
29
+
30
+ exports.default = createNavigation;
@@ -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
  }
@@ -8,7 +8,6 @@ var NextLink = require('next/link');
8
8
  var navigation = require('next/navigation');
9
9
  var React = require('react');
10
10
  var useLocale = require('../../react-client/useLocale.js');
11
- var utils = require('../../shared/utils.js');
12
11
  var syncLocaleCookie = require('./syncLocaleCookie.js');
13
12
 
14
13
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
@@ -18,39 +17,37 @@ var React__default = /*#__PURE__*/_interopDefault(React);
18
17
 
19
18
  function BaseLink(_ref, ref) {
20
19
  let {
20
+ defaultLocale,
21
21
  href,
22
22
  locale,
23
- localePrefixMode,
24
23
  onClick,
25
24
  prefetch,
26
- prefix,
25
+ unprefixed,
27
26
  ...rest
28
27
  } = _ref;
28
+ const curLocale = useLocale.default();
29
+ const isChangingLocale = locale !== curLocale;
30
+ const linkLocale = locale || curLocale;
31
+ const host = useHost();
32
+ const finalHref =
33
+ // Only after hydration (to avoid mismatches)
34
+ host &&
35
+ // If there is an `unprefixed` prop, the
36
+ // `defaultLocale` might differ by domain
37
+ unprefixed && (
38
+ // Unprefix the pathname if a domain matches
39
+ unprefixed.domains[host] === linkLocale ||
40
+ // … and handle unknown domains by applying the
41
+ // global `defaultLocale` (e.g. on localhost)
42
+ !Object.keys(unprefixed.domains).includes(host) && curLocale === defaultLocale && !locale) ? unprefixed.pathname : href;
43
+
29
44
  // The types aren't entirely correct here. Outside of Next.js
30
45
  // `useParams` can be called, but the return type is `null`.
31
46
  const pathname = navigation.usePathname();
32
- const curLocale = useLocale.default();
33
- const isChangingLocale = locale !== curLocale;
34
- const [localizedHref, setLocalizedHref] = React.useState(() => utils.isLocalizableHref(href) && (localePrefixMode !== 'never' || isChangingLocale) ?
35
- // For the `localePrefix: 'as-needed' strategy, the href shouldn't
36
- // be prefixed if the locale is the default locale. To determine this, we
37
- // need a) the default locale and b) the information if we use prefixed
38
- // routing. The default locale can vary by domain, therefore during the
39
- // RSC as well as the SSR render, we can't determine the default locale
40
- // statically. Therefore we always prefix the href since this will
41
- // always result in a valid URL, even if it might cause a redirect. This
42
- // is better than pointing to a non-localized href during the server
43
- // render, which would potentially be wrong. The final href is
44
- // determined in the effect below.
45
- utils.prefixHref(href, prefix) : href);
46
47
  function onLinkClick(event) {
47
48
  syncLocaleCookie.default(pathname, curLocale, locale);
48
49
  if (onClick) onClick(event);
49
50
  }
50
- React.useEffect(() => {
51
- if (!pathname) return;
52
- setLocalizedHref(utils.localizeHref(href, locale, curLocale, pathname, prefix));
53
- }, [curLocale, href, locale, pathname, prefix]);
54
51
  if (isChangingLocale) {
55
52
  if (prefetch && "development" !== 'production') {
56
53
  console.error('The `prefetch` prop is currently not supported when using the `locale` prop on `Link` to switch the locale.`');
@@ -59,13 +56,19 @@ function BaseLink(_ref, ref) {
59
56
  }
60
57
  return /*#__PURE__*/React__default.default.createElement(NextLink__default.default, _rollupPluginBabelHelpers.extends({
61
58
  ref: ref,
62
- href: localizedHref,
59
+ href: finalHref,
63
60
  hrefLang: isChangingLocale ? locale : undefined,
64
61
  onClick: onLinkClick,
65
62
  prefetch: prefetch
66
63
  }, rest));
67
64
  }
68
- const BaseLinkWithRef = /*#__PURE__*/React.forwardRef(BaseLink);
69
- BaseLinkWithRef.displayName = 'ClientLink';
65
+ function useHost() {
66
+ const [host, setHost] = React.useState();
67
+ React.useEffect(() => {
68
+ setHost(window.location.host);
69
+ }, []);
70
+ return host;
71
+ }
72
+ var BaseLink$1 = /*#__PURE__*/React.forwardRef(BaseLink);
70
73
 
71
- exports.default = BaseLinkWithRef;
74
+ exports.default = BaseLink$1;
@@ -0,0 +1,55 @@
1
+ "use client";
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
7
+ var navigation = require('next/navigation');
8
+ var React = require('react');
9
+ var useLocale = require('../../react-client/useLocale.js');
10
+ var utils = require('../../shared/utils.js');
11
+ var BaseLink = require('./BaseLink.js');
12
+
13
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
14
+
15
+ var React__default = /*#__PURE__*/_interopDefault(React);
16
+
17
+ function LegacyBaseLink(_ref, ref) {
18
+ let {
19
+ href,
20
+ locale,
21
+ localePrefixMode,
22
+ prefix,
23
+ ...rest
24
+ } = _ref;
25
+ // The types aren't entirely correct here. Outside of Next.js
26
+ // `useParams` can be called, but the return type is `null`.
27
+ const pathname = navigation.usePathname();
28
+ const curLocale = useLocale.default();
29
+ const isChangingLocale = locale !== curLocale;
30
+ const [localizedHref, setLocalizedHref] = React.useState(() => utils.isLocalizableHref(href) && (localePrefixMode !== 'never' || isChangingLocale) ?
31
+ // For the `localePrefix: 'as-needed' strategy, the href shouldn't
32
+ // be prefixed if the locale is the default locale. To determine this, we
33
+ // need a) the default locale and b) the information if we use prefixed
34
+ // routing. The default locale can vary by domain, therefore during the
35
+ // RSC as well as the SSR render, we can't determine the default locale
36
+ // statically. Therefore we always prefix the href since this will
37
+ // always result in a valid URL, even if it might cause a redirect. This
38
+ // is better than pointing to a non-localized href during the server
39
+ // render, which would potentially be wrong. The final href is
40
+ // determined in the effect below.
41
+ utils.prefixHref(href, prefix) : href);
42
+ React.useEffect(() => {
43
+ if (!pathname) return;
44
+ setLocalizedHref(utils.localizeHref(href, locale, curLocale, pathname, prefix));
45
+ }, [curLocale, href, locale, pathname, prefix]);
46
+ return /*#__PURE__*/React__default.default.createElement(BaseLink.default, _rollupPluginBabelHelpers.extends({
47
+ ref: ref,
48
+ href: localizedHref,
49
+ locale: locale
50
+ }, rest));
51
+ }
52
+ const LegacyBaseLinkWithRef = /*#__PURE__*/React.forwardRef(LegacyBaseLink);
53
+ LegacyBaseLinkWithRef.displayName = 'ClientLink';
54
+
55
+ exports.default = LegacyBaseLinkWithRef;
@@ -0,0 +1,147 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
6
+ var navigation = require('next/navigation');
7
+ var React = require('react');
8
+ var config = require('../../routing/config.js');
9
+ var utils$1 = require('../../shared/utils.js');
10
+ var BaseLink = require('./BaseLink.js');
11
+ var utils = require('./utils.js');
12
+
13
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
14
+
15
+ var React__default = /*#__PURE__*/_interopDefault(React);
16
+
17
+ /**
18
+ * Shared implementations for `react-server` and `react-client`
19
+ */
20
+ function createSharedNavigationFns(getLocale, routing) {
21
+ const config$1 = config.receiveRoutingConfig(routing || {});
22
+ {
23
+ utils.validateReceivedConfig(config$1);
24
+ }
25
+ const pathnames = config$1.pathnames;
26
+
27
+ // This combination requires that the current host is known in order to
28
+ // compute a correct pathname. Since that can only be achieved by reading from
29
+ // headers, this would break static rendering. Therefore, as a workaround we
30
+ // always add a prefix in this case to be on the safe side. The downside is
31
+ // that the user might get redirected again if the middleware detects that the
32
+ // prefix is not needed.
33
+ const forcePrefixSsr = config$1.localePrefix.mode === 'as-needed' && config$1.domains || undefined;
34
+ function Link(_ref, ref) {
35
+ let {
36
+ href,
37
+ locale,
38
+ ...rest
39
+ } = _ref;
40
+ let pathname, params;
41
+ if (typeof href === 'object') {
42
+ pathname = href.pathname;
43
+ // @ts-expect-error -- This is ok
44
+ params = href.params;
45
+ } else {
46
+ pathname = href;
47
+ }
48
+
49
+ // @ts-expect-error -- This is ok
50
+ const isLocalizable = utils$1.isLocalizableHref(href);
51
+ const localePromiseOrValue = getLocale();
52
+ const curLocale = localePromiseOrValue instanceof Promise ? React.use(localePromiseOrValue) : localePromiseOrValue;
53
+ const finalPathname = isLocalizable ? getPathname(
54
+ // @ts-expect-error -- This is ok
55
+ {
56
+ locale: locale || curLocale,
57
+ href: pathnames == null ? pathname : {
58
+ pathname,
59
+ params
60
+ }
61
+ }, locale != null || forcePrefixSsr || undefined) : pathname;
62
+ return /*#__PURE__*/React__default.default.createElement(BaseLink.default, _rollupPluginBabelHelpers.extends({
63
+ ref: ref
64
+ // @ts-expect-error -- Available after the validation
65
+ ,
66
+ defaultLocale: config$1.defaultLocale,
67
+ href: {
68
+ ...(typeof href === 'object' && href),
69
+ // @ts-expect-error -- This is ok
70
+ pathname: finalPathname
71
+ },
72
+ locale: locale
73
+ // Provide the minimal relevant information to the client side in order
74
+ // to potentially remove the prefix in case of the `forcePrefixSsr` case
75
+ ,
76
+ unprefixed: forcePrefixSsr && isLocalizable ? {
77
+ domains: config$1.domains.reduce((acc, domain) => {
78
+ // @ts-expect-error -- This is ok
79
+ acc[domain.domain] = domain.defaultLocale;
80
+ return acc;
81
+ }, {}),
82
+ pathname: getPathname(
83
+ // @ts-expect-error -- This is ok
84
+ {
85
+ locale: curLocale,
86
+ href: pathnames == null ? pathname : {
87
+ pathname,
88
+ params
89
+ }
90
+ }, false)
91
+ } : undefined
92
+ }, rest));
93
+ }
94
+ const LinkWithRef = /*#__PURE__*/React.forwardRef(Link);
95
+ function getPathname(args, /** @private Removed in types returned below */
96
+ _forcePrefix) {
97
+ const {
98
+ href,
99
+ locale
100
+ } = args;
101
+ let pathname;
102
+ if (pathnames == null) {
103
+ if (typeof href === 'object') {
104
+ pathname = href.pathname;
105
+ if (href.query) {
106
+ pathname += utils.serializeSearchParams(href.query);
107
+ }
108
+ } else {
109
+ pathname = href;
110
+ }
111
+ } else {
112
+ pathname = utils.compileLocalizedPathname({
113
+ locale,
114
+ // @ts-expect-error -- This is ok
115
+ ...utils.normalizeNameOrNameWithParams(href),
116
+ // @ts-expect-error -- This is ok
117
+ pathnames: config$1.pathnames
118
+ });
119
+ }
120
+ return utils.applyPathnamePrefix(pathname, locale, config$1,
121
+ // @ts-expect-error -- This is ok
122
+ args.domain, _forcePrefix);
123
+ }
124
+ function getRedirectFn(fn) {
125
+ /** @see https://next-intl-docs.vercel.app/docs/routing/navigation#redirect */
126
+ return function redirectFn(args) {
127
+ for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
128
+ rest[_key - 1] = arguments[_key];
129
+ }
130
+ return fn(
131
+ // @ts-expect-error -- We're forcing the prefix when no domain is provided
132
+ getPathname(args, args.domain ? undefined : forcePrefixSsr), ...rest);
133
+ };
134
+ }
135
+ const redirect = getRedirectFn(navigation.redirect);
136
+ const permanentRedirect = getRedirectFn(navigation.permanentRedirect);
137
+ return {
138
+ config: config$1,
139
+ Link: LinkWithRef,
140
+ redirect,
141
+ permanentRedirect,
142
+ // Remove `_forcePrefix` from public API
143
+ getPathname: getPathname
144
+ };
145
+ }
146
+
147
+ exports.default = createSharedNavigationFns;
@@ -8,6 +8,9 @@ var utils = require('../../shared/utils.js');
8
8
  function createRedirectFn(redirectFn) {
9
9
  return function baseRedirect(params) {
10
10
  const prefix = utils.getLocalePrefix(params.locale, params.localePrefix);
11
+
12
+ // This logic is considered legacy and is replaced by `applyPathnamePrefix`.
13
+ // We keep it this way for now for backwards compatibility.
11
14
  const localizedPathname = params.localePrefix.mode === 'never' || !utils.isLocalizableHref(params.pathname) ? params.pathname : utils.prefixPathname(prefix, params.pathname);
12
15
  for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
13
16
  args[_key - 1] = arguments[_key];