next-intl 3.20.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.
- package/dist/development/middleware/middleware.js +6 -6
- package/dist/development/middleware/resolveLocale.js +2 -7
- package/dist/development/navigation/react-client/ClientLink.js +2 -2
- package/dist/development/navigation/react-client/createNavigation.js +83 -0
- package/dist/development/navigation/react-client/useBasePathname.js +0 -14
- package/dist/development/navigation/react-server/ServerLink.js +4 -2
- package/dist/development/navigation/react-server/createLocalizedPathnamesNavigation.js +4 -4
- package/dist/development/navigation/react-server/createNavigation.js +30 -0
- package/dist/development/navigation/react-server/redirects.js +2 -2
- package/dist/development/navigation/shared/BaseLink.js +28 -25
- package/dist/development/navigation/shared/LegacyBaseLink.js +55 -0
- package/dist/development/navigation/shared/createSharedNavigationFns.js +147 -0
- package/dist/development/navigation/shared/redirects.js +3 -0
- package/dist/development/navigation/shared/utils.js +40 -1
- package/dist/development/navigation.react-client.js +2 -0
- package/dist/development/navigation.react-server.js +2 -0
- package/dist/development/react-client/index.js +0 -2
- package/dist/development/routing/config.js +1 -1
- package/dist/development/server/react-server/RequestLocale.js +15 -25
- package/dist/development/server/react-server/RequestLocaleCache.js +23 -0
- package/dist/development/server/react-server/RequestLocaleLegacy.js +43 -0
- package/dist/development/server/react-server/getConfig.js +15 -17
- package/dist/development/server/react-server/getRequestConfig.js +1 -1
- package/dist/development/server.react-server.js +2 -2
- package/dist/esm/middleware/middleware.js +1 -1
- package/dist/esm/middleware/resolveLocale.js +1 -1
- package/dist/esm/navigation/react-client/ClientLink.js +1 -1
- package/dist/esm/navigation/react-client/createNavigation.js +1 -0
- package/dist/esm/navigation/react-client/useBasePathname.js +0 -1
- package/dist/esm/navigation/react-server/ServerLink.js +1 -1
- package/dist/esm/navigation/react-server/createLocalizedPathnamesNavigation.js +1 -1
- package/dist/esm/navigation/react-server/createNavigation.js +1 -0
- package/dist/esm/navigation/react-server/redirects.js +1 -1
- package/dist/esm/navigation/shared/BaseLink.js +1 -1
- package/dist/esm/navigation/shared/LegacyBaseLink.js +2 -0
- package/dist/esm/navigation/shared/createSharedNavigationFns.js +1 -0
- package/dist/esm/navigation/shared/utils.js +1 -1
- package/dist/esm/navigation.react-client.js +1 -1
- package/dist/esm/navigation.react-server.js +1 -1
- package/dist/esm/routing/config.js +1 -1
- package/dist/esm/server/react-server/RequestLocale.js +1 -1
- package/dist/esm/server/react-server/RequestLocaleCache.js +1 -0
- package/dist/esm/server/react-server/RequestLocaleLegacy.js +1 -0
- package/dist/esm/server/react-server/getConfig.js +1 -1
- package/dist/esm/server.react-server.js +1 -1
- package/dist/production/middleware/middleware.js +1 -1
- package/dist/production/middleware/resolveLocale.js +1 -1
- package/dist/production/navigation/react-client/ClientLink.js +1 -1
- package/dist/production/navigation/react-client/createNavigation.js +1 -0
- package/dist/production/navigation/react-client/useBasePathname.js +0 -1
- package/dist/production/navigation/react-server/ServerLink.js +1 -1
- package/dist/production/navigation/react-server/createLocalizedPathnamesNavigation.js +1 -1
- package/dist/production/navigation/react-server/createNavigation.js +1 -0
- package/dist/production/navigation/react-server/redirects.js +1 -1
- package/dist/production/navigation/shared/BaseLink.js +1 -1
- package/dist/production/navigation/shared/LegacyBaseLink.js +2 -0
- package/dist/production/navigation/shared/createSharedNavigationFns.js +1 -0
- package/dist/production/navigation/shared/utils.js +1 -1
- package/dist/production/navigation.react-client.js +1 -1
- package/dist/production/navigation.react-server.js +1 -1
- package/dist/production/routing/config.js +1 -1
- package/dist/production/server/react-server/RequestLocale.js +1 -1
- package/dist/production/server/react-server/RequestLocaleCache.js +1 -0
- package/dist/production/server/react-server/RequestLocaleLegacy.js +1 -0
- package/dist/production/server/react-server/getConfig.js +1 -1
- package/dist/production/server.react-server.js +1 -1
- package/dist/types/src/middleware/getAlternateLinksHeaderValue.d.ts +3 -3
- package/dist/types/src/middleware/middleware.d.ts +2 -2
- package/dist/types/src/middleware/resolveLocale.d.ts +2 -2
- package/dist/types/src/middleware/utils.d.ts +4 -4
- package/dist/types/src/navigation/createNavigation.test.d.ts +1 -0
- package/dist/types/src/navigation/react-client/ClientLink.d.ts +6 -6
- package/dist/types/src/navigation/react-client/createLocalizedPathnamesNavigation.d.ts +12 -4
- package/dist/types/src/navigation/react-client/createNavigation.d.ts +467 -0
- package/dist/types/src/navigation/react-client/createNavigation.test.d.ts +1 -0
- package/dist/types/src/navigation/react-client/createSharedPathnamesNavigation.d.ts +4 -4
- package/dist/types/src/navigation/react-client/index.d.ts +1 -0
- package/dist/types/src/navigation/react-client/redirects.d.ts +4 -4
- package/dist/types/src/navigation/react-client/useBasePathname.d.ts +2 -15
- package/dist/types/src/navigation/react-client/useBaseRouter.d.ts +2 -2
- package/dist/types/src/navigation/react-server/ServerLink.d.ts +5 -5
- package/dist/types/src/navigation/react-server/createLocalizedPathnamesNavigation.d.ts +12 -4
- package/dist/types/src/navigation/react-server/createNavigation.d.ts +385 -0
- package/dist/types/src/navigation/react-server/createNavigation.test.d.ts +1 -0
- package/dist/types/src/navigation/react-server/createSharedPathnamesNavigation.d.ts +3 -3
- package/dist/types/src/navigation/react-server/index.d.ts +1 -0
- package/dist/types/src/navigation/react-server/redirects.d.ts +4 -4
- package/dist/types/src/navigation/shared/BaseLink.d.ts +11 -6
- package/dist/types/src/navigation/shared/LegacyBaseLink.d.ts +10 -0
- package/dist/types/src/navigation/shared/createSharedNavigationFns.d.ts +382 -0
- package/dist/types/src/navigation/shared/redirects.d.ts +5 -5
- package/dist/types/src/navigation/shared/utils.d.ts +10 -3
- package/dist/types/src/routing/config.d.ts +19 -11
- package/dist/types/src/routing/defineRouting.d.ts +2 -2
- package/dist/types/src/routing/types.d.ts +4 -4
- package/dist/types/src/server/react-server/RequestLocale.d.ts +1 -2
- package/dist/types/src/server/react-server/RequestLocaleCache.d.ts +2 -0
- package/dist/types/src/server/react-server/RequestLocaleLegacy.d.ts +1 -0
- package/dist/types/src/server/react-server/createRequestConfig.d.ts +2 -3
- package/dist/types/src/server/react-server/getRequestConfig.d.ts +32 -6
- package/dist/types/src/server/react-server/index.d.ts +1 -1
- package/dist/types/src/shared/types.d.ts +3 -0
- package/dist/types/src/shared/utils.d.ts +2 -2
- package/package.json +3 -3
|
@@ -55,7 +55,7 @@ function createMiddleware(routing, options) {
|
|
|
55
55
|
}
|
|
56
56
|
function redirect(url, redirectDomain) {
|
|
57
57
|
const urlObj = new URL(utils$1.normalizeTrailingSlash(url), request.url);
|
|
58
|
-
if (domainsConfig.length > 0 && !redirectDomain) {
|
|
58
|
+
if (domainsConfig.length > 0 && !redirectDomain && domain) {
|
|
59
59
|
const bestMatchingDomain = utils.getBestMatchingDomain(domain, locale, domainsConfig);
|
|
60
60
|
if (bestMatchingDomain) {
|
|
61
61
|
redirectDomain = bestMatchingDomain.domain;
|
|
@@ -84,11 +84,12 @@ function createMiddleware(routing, options) {
|
|
|
84
84
|
let response;
|
|
85
85
|
let internalTemplateName;
|
|
86
86
|
let unprefixedInternalPathname = unprefixedExternalPathname;
|
|
87
|
-
|
|
87
|
+
const pathnames = resolvedRouting.pathnames;
|
|
88
|
+
if (pathnames) {
|
|
88
89
|
let resolvedTemplateLocale;
|
|
89
|
-
[resolvedTemplateLocale, internalTemplateName] = utils.getInternalTemplate(
|
|
90
|
+
[resolvedTemplateLocale, internalTemplateName] = utils.getInternalTemplate(pathnames, unprefixedExternalPathname, locale);
|
|
90
91
|
if (internalTemplateName) {
|
|
91
|
-
const pathnameConfig =
|
|
92
|
+
const pathnameConfig = pathnames[internalTemplateName];
|
|
92
93
|
const localeTemplate = typeof pathnameConfig === 'string' ? pathnameConfig :
|
|
93
94
|
// @ts-expect-error -- This is fine
|
|
94
95
|
pathnameConfig[locale];
|
|
@@ -156,10 +157,9 @@ function createMiddleware(routing, options) {
|
|
|
156
157
|
syncCookie.default(request, response, locale);
|
|
157
158
|
}
|
|
158
159
|
if (resolvedRouting.localePrefix.mode !== 'never' && resolvedOptions.alternateLinks && resolvedRouting.locales.length > 1) {
|
|
159
|
-
var _resolvedRouting$path;
|
|
160
160
|
response.headers.set('Link', getAlternateLinksHeaderValue.default({
|
|
161
161
|
routing: resolvedRouting,
|
|
162
|
-
localizedPathnames: internalTemplateName != null &&
|
|
162
|
+
localizedPathnames: internalTemplateName != null && pathnames ? pathnames === null || pathnames === void 0 ? void 0 : pathnames[internalTemplateName] : undefined,
|
|
163
163
|
request,
|
|
164
164
|
resolvedLocale: locale
|
|
165
165
|
}));
|
|
@@ -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
|
-
|
|
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
|
-
|
|
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)
|
|
@@ -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
|
|
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(
|
|
24
|
+
return /*#__PURE__*/React__default.default.createElement(LegacyBaseLink.default, _rollupPluginBabelHelpers.extends({
|
|
25
25
|
ref: ref,
|
|
26
26
|
locale: finalLocale,
|
|
27
27
|
localePrefixMode: localePrefix.mode,
|
|
@@ -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;
|
|
@@ -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
|
|
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(
|
|
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
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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
|
|
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 =
|
|
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
|
-
|
|
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:
|
|
59
|
+
href: finalHref,
|
|
63
60
|
hrefLang: isChangingLocale ? locale : undefined,
|
|
64
61
|
onClick: onLinkClick,
|
|
65
62
|
prefetch: prefetch
|
|
66
63
|
}, rest));
|
|
67
64
|
}
|
|
68
|
-
|
|
69
|
-
|
|
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 =
|
|
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];
|
|
@@ -7,8 +7,11 @@ var utils = require('../../shared/utils.js');
|
|
|
7
7
|
// Minor false positive: A route that has both optional and
|
|
8
8
|
// required params will allow optional params.
|
|
9
9
|
|
|
10
|
+
// For `Link`
|
|
11
|
+
|
|
12
|
+
// For `getPathname` (hence also its consumers: `redirect`, `useRouter`, …)
|
|
13
|
+
|
|
10
14
|
function normalizeNameOrNameWithParams(href) {
|
|
11
|
-
// @ts-expect-error -- `extends string` in the generic unfortunately weakens the type
|
|
12
15
|
return typeof href === 'string' ? {
|
|
13
16
|
pathname: href
|
|
14
17
|
} : href;
|
|
@@ -40,6 +43,7 @@ function compileLocalizedPathname(_ref) {
|
|
|
40
43
|
function getNamedPath(value) {
|
|
41
44
|
let namedPath = pathnames[value];
|
|
42
45
|
if (!namedPath) {
|
|
46
|
+
// Unknown pathnames
|
|
43
47
|
namedPath = value;
|
|
44
48
|
}
|
|
45
49
|
return namedPath;
|
|
@@ -118,9 +122,44 @@ function getBasePath(pathname) {
|
|
|
118
122
|
return windowPathname.replace(pathname, '');
|
|
119
123
|
}
|
|
120
124
|
}
|
|
125
|
+
function applyPathnamePrefix(pathname, locale, routing, domain, force) {
|
|
126
|
+
const {
|
|
127
|
+
mode
|
|
128
|
+
} = routing.localePrefix;
|
|
129
|
+
let shouldPrefix;
|
|
130
|
+
if (force !== undefined) {
|
|
131
|
+
shouldPrefix = force;
|
|
132
|
+
} else if (utils.isLocalizableHref(pathname)) {
|
|
133
|
+
if (mode === 'always') {
|
|
134
|
+
shouldPrefix = true;
|
|
135
|
+
} else if (mode === 'as-needed') {
|
|
136
|
+
let defaultLocale = routing.defaultLocale;
|
|
137
|
+
if (routing.domains) {
|
|
138
|
+
const domainConfig = routing.domains.find(cur => cur.domain === domain);
|
|
139
|
+
if (domainConfig) {
|
|
140
|
+
defaultLocale = domainConfig.defaultLocale;
|
|
141
|
+
} else {
|
|
142
|
+
if (!domain) {
|
|
143
|
+
console.error("You're using a routing configuration with `localePrefix: 'as-needed'` in combination with `domains`. In order to compute a correct pathname, you need to provide a `domain` parameter.\n\nSee: https://next-intl-docs.vercel.app/docs/routing#domains-localeprefix-asneeded");
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
shouldPrefix = defaultLocale !== locale;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return shouldPrefix ? utils.prefixPathname(utils.getLocalePrefix(locale, routing.localePrefix), pathname) : pathname;
|
|
151
|
+
}
|
|
152
|
+
function validateReceivedConfig(config) {
|
|
153
|
+
var _config$localePrefix;
|
|
154
|
+
if (((_config$localePrefix = config.localePrefix) === null || _config$localePrefix === void 0 ? void 0 : _config$localePrefix.mode) === 'as-needed' && !('defaultLocale' in config)) {
|
|
155
|
+
throw new Error("`localePrefix: 'as-needed' requires a `defaultLocale`.");
|
|
156
|
+
}
|
|
157
|
+
}
|
|
121
158
|
|
|
159
|
+
exports.applyPathnamePrefix = applyPathnamePrefix;
|
|
122
160
|
exports.compileLocalizedPathname = compileLocalizedPathname;
|
|
123
161
|
exports.getBasePath = getBasePath;
|
|
124
162
|
exports.getRoute = getRoute;
|
|
125
163
|
exports.normalizeNameOrNameWithParams = normalizeNameOrNameWithParams;
|
|
126
164
|
exports.serializeSearchParams = serializeSearchParams;
|
|
165
|
+
exports.validateReceivedConfig = validateReceivedConfig;
|