next-intlayer 6.1.6-canary.0 → 7.0.0-canary.2

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 (144) hide show
  1. package/dist/cjs/_virtual/rolldown_runtime.cjs +25 -0
  2. package/dist/cjs/client/IntlayerClientProvider.cjs +19 -33
  3. package/dist/cjs/client/IntlayerClientProvider.cjs.map +1 -1
  4. package/dist/cjs/client/format/index.cjs +50 -41
  5. package/dist/cjs/client/index.cjs +7 -35
  6. package/dist/cjs/client/useLocale.cjs +39 -54
  7. package/dist/cjs/client/useLocale.cjs.map +1 -1
  8. package/dist/cjs/client/useLocalePageRouter.cjs +26 -52
  9. package/dist/cjs/client/useLocalePageRouter.cjs.map +1 -1
  10. package/dist/cjs/generateStaticParams.cjs +9 -39
  11. package/dist/cjs/generateStaticParams.cjs.map +1 -1
  12. package/dist/cjs/index.cjs +102 -57
  13. package/dist/cjs/middleware/index.cjs +7 -27
  14. package/dist/cjs/middleware/intlayerMiddleware.cjs +206 -188
  15. package/dist/cjs/middleware/intlayerMiddleware.cjs.map +1 -1
  16. package/dist/cjs/middleware/localeDetector.cjs +18 -32
  17. package/dist/cjs/middleware/localeDetector.cjs.map +1 -1
  18. package/dist/cjs/middleware/multipleMiddlewares.cjs +55 -67
  19. package/dist/cjs/middleware/multipleMiddlewares.cjs.map +1 -1
  20. package/dist/cjs/proxy/index.cjs +9 -0
  21. package/dist/cjs/proxy/intlayerProxy.cjs +252 -0
  22. package/dist/cjs/proxy/intlayerProxy.cjs.map +1 -0
  23. package/dist/cjs/proxy/localeDetector.cjs +21 -0
  24. package/dist/cjs/proxy/localeDetector.cjs.map +1 -0
  25. package/dist/cjs/proxy/multipleProxies.cjs +60 -0
  26. package/dist/cjs/proxy/multipleProxies.cjs.map +1 -0
  27. package/dist/cjs/proxy/multipleProxy.cjs +60 -0
  28. package/dist/cjs/proxy/multipleProxy.cjs.map +1 -0
  29. package/dist/cjs/server/compareVersion.cjs +35 -54
  30. package/dist/cjs/server/compareVersion.cjs.map +1 -1
  31. package/dist/cjs/server/format/index.cjs +50 -41
  32. package/dist/cjs/server/getLocale.cjs +31 -50
  33. package/dist/cjs/server/getLocale.cjs.map +1 -1
  34. package/dist/cjs/server/index.cjs +69 -53
  35. package/dist/cjs/server/withIntlayer.cjs +181 -247
  36. package/dist/cjs/server/withIntlayer.cjs.map +1 -1
  37. package/dist/cjs/types/NextPage.cjs +0 -17
  38. package/dist/cjs/types/index.cjs +0 -17
  39. package/dist/esm/client/IntlayerClientProvider.mjs +15 -8
  40. package/dist/esm/client/IntlayerClientProvider.mjs.map +1 -1
  41. package/dist/esm/client/format/index.mjs +3 -21
  42. package/dist/esm/client/index.mjs +3 -9
  43. package/dist/esm/client/useLocale.mjs +31 -27
  44. package/dist/esm/client/useLocale.mjs.map +1 -1
  45. package/dist/esm/client/useLocalePageRouter.mjs +19 -26
  46. package/dist/esm/client/useLocalePageRouter.mjs.map +1 -1
  47. package/dist/esm/generateStaticParams.mjs +5 -3
  48. package/dist/esm/generateStaticParams.mjs.map +1 -1
  49. package/dist/esm/index.mjs +6 -34
  50. package/dist/esm/middleware/index.mjs +5 -4
  51. package/dist/esm/middleware/intlayerMiddleware.mjs +201 -155
  52. package/dist/esm/middleware/intlayerMiddleware.mjs.map +1 -1
  53. package/dist/esm/middleware/localeDetector.mjs +16 -8
  54. package/dist/esm/middleware/localeDetector.mjs.map +1 -1
  55. package/dist/esm/middleware/multipleMiddlewares.mjs +53 -45
  56. package/dist/esm/middleware/multipleMiddlewares.mjs.map +1 -1
  57. package/dist/esm/proxy/index.mjs +5 -0
  58. package/dist/esm/proxy/intlayerProxy.mjs +247 -0
  59. package/dist/esm/proxy/intlayerProxy.mjs.map +1 -0
  60. package/dist/esm/proxy/localeDetector.mjs +19 -0
  61. package/dist/esm/proxy/localeDetector.mjs.map +1 -0
  62. package/dist/esm/proxy/multipleProxies.mjs +57 -0
  63. package/dist/esm/proxy/multipleProxies.mjs.map +1 -0
  64. package/dist/esm/proxy/multipleProxy.mjs +57 -0
  65. package/dist/esm/proxy/multipleProxy.mjs.map +1 -0
  66. package/dist/esm/server/compareVersion.mjs +34 -30
  67. package/dist/esm/server/compareVersion.mjs.map +1 -1
  68. package/dist/esm/server/format/index.mjs +3 -21
  69. package/dist/esm/server/getLocale.mjs +24 -15
  70. package/dist/esm/server/getLocale.mjs.map +1 -1
  71. package/dist/esm/server/index.mjs +4 -28
  72. package/dist/esm/server/withIntlayer.mjs +166 -214
  73. package/dist/esm/server/withIntlayer.mjs.map +1 -1
  74. package/dist/esm/types/NextPage.mjs +0 -1
  75. package/dist/esm/types/index.mjs +0 -1
  76. package/dist/types/client/IntlayerClientProvider.d.ts +8 -4
  77. package/dist/types/client/IntlayerClientProvider.d.ts.map +1 -1
  78. package/dist/types/client/format/index.d.ts +2 -2
  79. package/dist/types/client/index.d.ts +4 -4
  80. package/dist/types/client/useLocale.d.ts +17 -7
  81. package/dist/types/client/useLocale.d.ts.map +1 -1
  82. package/dist/types/client/useLocalePageRouter.d.ts +12 -7
  83. package/dist/types/client/useLocalePageRouter.d.ts.map +1 -1
  84. package/dist/types/generateStaticParams.d.ts +7 -2
  85. package/dist/types/generateStaticParams.d.ts.map +1 -1
  86. package/dist/types/index.d.ts +7 -5
  87. package/dist/types/middleware/index.d.ts +4 -4
  88. package/dist/types/middleware/intlayerMiddleware.d.ts +7 -2
  89. package/dist/types/middleware/intlayerMiddleware.d.ts.map +1 -1
  90. package/dist/types/middleware/localeDetector.d.ts +8 -3
  91. package/dist/types/middleware/localeDetector.d.ts.map +1 -1
  92. package/dist/types/middleware/multipleMiddlewares.d.ts +7 -2
  93. package/dist/types/middleware/multipleMiddlewares.d.ts.map +1 -1
  94. package/dist/types/proxy/index.d.ts +4 -0
  95. package/dist/types/proxy/intlayerProxy.d.ts +55 -0
  96. package/dist/types/proxy/intlayerProxy.d.ts.map +1 -0
  97. package/dist/types/proxy/localeDetector.d.ts +14 -0
  98. package/dist/types/proxy/localeDetector.d.ts.map +1 -0
  99. package/dist/types/proxy/multipleProxies.d.ts +34 -0
  100. package/dist/types/proxy/multipleProxies.d.ts.map +1 -0
  101. package/dist/types/proxy/multipleProxy.d.ts +34 -0
  102. package/dist/types/proxy/multipleProxy.d.ts.map +1 -0
  103. package/dist/types/server/compareVersion.d.ts +4 -1
  104. package/dist/types/server/compareVersion.d.ts.map +1 -1
  105. package/dist/types/server/format/index.d.ts +2 -2
  106. package/dist/types/server/getLocale.d.ts +6 -2
  107. package/dist/types/server/getLocale.d.ts.map +1 -1
  108. package/dist/types/server/index.d.ts +4 -4
  109. package/dist/types/server/withIntlayer.d.ts +25 -3
  110. package/dist/types/server/withIntlayer.d.ts.map +1 -1
  111. package/dist/types/types/NextPage.d.ts +21 -17
  112. package/dist/types/types/NextPage.d.ts.map +1 -1
  113. package/dist/types/types/index.d.ts +2 -2
  114. package/package.json +48 -50
  115. package/LICENSE +0 -202
  116. package/dist/cjs/client/format/index.cjs.map +0 -1
  117. package/dist/cjs/client/index.cjs.map +0 -1
  118. package/dist/cjs/index.cjs.map +0 -1
  119. package/dist/cjs/middleware/index.cjs.map +0 -1
  120. package/dist/cjs/server/format/index.cjs.map +0 -1
  121. package/dist/cjs/server/getNextVertion.cjs +0 -39
  122. package/dist/cjs/server/getNextVertion.cjs.map +0 -1
  123. package/dist/cjs/server/index.cjs.map +0 -1
  124. package/dist/cjs/types/NextPage.cjs.map +0 -1
  125. package/dist/cjs/types/index.cjs.map +0 -1
  126. package/dist/esm/client/format/index.mjs.map +0 -1
  127. package/dist/esm/client/index.mjs.map +0 -1
  128. package/dist/esm/index.mjs.map +0 -1
  129. package/dist/esm/middleware/index.mjs.map +0 -1
  130. package/dist/esm/server/format/index.mjs.map +0 -1
  131. package/dist/esm/server/getNextVertion.mjs +0 -15
  132. package/dist/esm/server/getNextVertion.mjs.map +0 -1
  133. package/dist/esm/server/index.mjs.map +0 -1
  134. package/dist/esm/types/NextPage.mjs.map +0 -1
  135. package/dist/esm/types/index.mjs.map +0 -1
  136. package/dist/types/client/format/index.d.ts.map +0 -1
  137. package/dist/types/client/index.d.ts.map +0 -1
  138. package/dist/types/index.d.ts.map +0 -1
  139. package/dist/types/middleware/index.d.ts.map +0 -1
  140. package/dist/types/server/format/index.d.ts.map +0 -1
  141. package/dist/types/server/getNextVertion.d.ts +0 -2
  142. package/dist/types/server/getNextVertion.d.ts.map +0 -1
  143. package/dist/types/server/index.d.ts.map +0 -1
  144. package/dist/types/types/index.d.ts.map +0 -1
@@ -1,57 +1,102 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var index_exports = {};
20
- __export(index_exports, {
21
- IntlayerClientContext: () => import_react_intlayer.IntlayerClientContext,
22
- IntlayerClientProvider: () => import_client.IntlayerClientProvider,
23
- MarkdownProvider: () => import_react_intlayer.MarkdownProvider,
24
- generateStaticParams: () => import_generateStaticParams.generateStaticParams,
25
- t: () => import_react_intlayer.t,
26
- useDictionary: () => import_react_intlayer.useDictionary,
27
- useDictionaryAsync: () => import_react_intlayer.useDictionaryAsync,
28
- useDictionaryDynamic: () => import_react_intlayer.useDictionaryDynamic,
29
- useI18n: () => import_react_intlayer.useI18n,
30
- useIntlayer: () => import_react_intlayer.useIntlayer,
31
- useLoadDynamic: () => import_react_intlayer.useLoadDynamic,
32
- useLocale: () => import_client.useLocale,
33
- useLocaleCookie: () => import_react_intlayer.useLocaleCookie,
34
- useLocalePageRouter: () => import_client.useLocalePageRouter
35
- });
36
- module.exports = __toCommonJS(index_exports);
37
- var import_react_intlayer = require("react-intlayer");
38
- var import_client = require('./client/index.cjs');
39
- var import_generateStaticParams = require('./generateStaticParams.cjs');
40
- // Annotate the CommonJS export names for ESM import in node:
41
- 0 && (module.exports = {
42
- IntlayerClientContext,
43
- IntlayerClientProvider,
44
- MarkdownProvider,
45
- generateStaticParams,
46
- t,
47
- useDictionary,
48
- useDictionaryAsync,
49
- useDictionaryDynamic,
50
- useI18n,
51
- useIntlayer,
52
- useLoadDynamic,
53
- useLocale,
54
- useLocaleCookie,
55
- useLocalePageRouter
56
- });
57
- //# sourceMappingURL=index.cjs.map
1
+ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
2
+ const require_generateStaticParams = require('./generateStaticParams.cjs');
3
+ const require_client_useLocale = require('./client/useLocale.cjs');
4
+ const require_client_IntlayerClientProvider = require('./client/IntlayerClientProvider.cjs');
5
+ const require_client_useLocalePageRouter = require('./client/useLocalePageRouter.cjs');
6
+ let react_intlayer = require("react-intlayer");
7
+ react_intlayer = require_rolldown_runtime.__toESM(react_intlayer);
8
+
9
+ Object.defineProperty(exports, 'IntlayerClientContext', {
10
+ enumerable: true,
11
+ get: function () {
12
+ return react_intlayer.IntlayerClientContext;
13
+ }
14
+ });
15
+ exports.IntlayerClientProvider = require_client_IntlayerClientProvider.IntlayerClientProvider;
16
+ Object.defineProperty(exports, 'MarkdownProvider', {
17
+ enumerable: true,
18
+ get: function () {
19
+ return react_intlayer.MarkdownProvider;
20
+ }
21
+ });
22
+ exports.generateStaticParams = require_generateStaticParams.generateStaticParams;
23
+ Object.defineProperty(exports, 'localeCookie', {
24
+ enumerable: true,
25
+ get: function () {
26
+ return react_intlayer.localeCookie;
27
+ }
28
+ });
29
+ Object.defineProperty(exports, 'localeInStorage', {
30
+ enumerable: true,
31
+ get: function () {
32
+ return react_intlayer.localeInStorage;
33
+ }
34
+ });
35
+ Object.defineProperty(exports, 'setLocaleCookie', {
36
+ enumerable: true,
37
+ get: function () {
38
+ return react_intlayer.setLocaleCookie;
39
+ }
40
+ });
41
+ Object.defineProperty(exports, 'setLocaleInStorage', {
42
+ enumerable: true,
43
+ get: function () {
44
+ return react_intlayer.setLocaleInStorage;
45
+ }
46
+ });
47
+ Object.defineProperty(exports, 't', {
48
+ enumerable: true,
49
+ get: function () {
50
+ return react_intlayer.t;
51
+ }
52
+ });
53
+ Object.defineProperty(exports, 'useDictionary', {
54
+ enumerable: true,
55
+ get: function () {
56
+ return react_intlayer.useDictionary;
57
+ }
58
+ });
59
+ Object.defineProperty(exports, 'useDictionaryAsync', {
60
+ enumerable: true,
61
+ get: function () {
62
+ return react_intlayer.useDictionaryAsync;
63
+ }
64
+ });
65
+ Object.defineProperty(exports, 'useDictionaryDynamic', {
66
+ enumerable: true,
67
+ get: function () {
68
+ return react_intlayer.useDictionaryDynamic;
69
+ }
70
+ });
71
+ Object.defineProperty(exports, 'useI18n', {
72
+ enumerable: true,
73
+ get: function () {
74
+ return react_intlayer.useI18n;
75
+ }
76
+ });
77
+ Object.defineProperty(exports, 'useIntlayer', {
78
+ enumerable: true,
79
+ get: function () {
80
+ return react_intlayer.useIntlayer;
81
+ }
82
+ });
83
+ Object.defineProperty(exports, 'useLoadDynamic', {
84
+ enumerable: true,
85
+ get: function () {
86
+ return react_intlayer.useLoadDynamic;
87
+ }
88
+ });
89
+ exports.useLocale = require_client_useLocale.useLocale;
90
+ Object.defineProperty(exports, 'useLocaleCookie', {
91
+ enumerable: true,
92
+ get: function () {
93
+ return react_intlayer.useLocaleCookie;
94
+ }
95
+ });
96
+ exports.useLocalePageRouter = require_client_useLocalePageRouter.useLocalePageRouter;
97
+ Object.defineProperty(exports, 'useLocaleStorage', {
98
+ enumerable: true,
99
+ get: function () {
100
+ return react_intlayer.useLocaleStorage;
101
+ }
102
+ });
@@ -1,27 +1,7 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
13
- };
14
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
16
- var middleware_exports = {};
17
- module.exports = __toCommonJS(middleware_exports);
18
- __reExport(middleware_exports, require('./intlayerMiddleware.cjs'), module.exports);
19
- __reExport(middleware_exports, require('./localeDetector.cjs'), module.exports);
20
- __reExport(middleware_exports, require('./multipleMiddlewares.cjs'), module.exports);
21
- // Annotate the CommonJS export names for ESM import in node:
22
- 0 && (module.exports = {
23
- ...require('./intlayerMiddleware.cjs'),
24
- ...require('./localeDetector.cjs'),
25
- ...require('./multipleMiddlewares.cjs')
26
- });
27
- //# sourceMappingURL=index.cjs.map
1
+ const require_middleware_localeDetector = require('./localeDetector.cjs');
2
+ const require_middleware_intlayerMiddleware = require('./intlayerMiddleware.cjs');
3
+ const require_middleware_multipleMiddlewares = require('./multipleMiddlewares.cjs');
4
+
5
+ exports.intlayerMiddleware = require_middleware_intlayerMiddleware.intlayerMiddleware;
6
+ exports.localeDetector = require_middleware_localeDetector.localeDetector;
7
+ exports.multipleMiddlewares = require_middleware_multipleMiddlewares.multipleMiddlewares;
@@ -1,209 +1,227 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var intlayerMiddleware_exports = {};
30
- __export(intlayerMiddleware_exports, {
31
- intlayerMiddleware: () => intlayerMiddleware
32
- });
33
- module.exports = __toCommonJS(intlayerMiddleware_exports);
34
- var import_built = __toESM(require("@intlayer/config/built"));
35
- var import_server = require("next/server");
36
- var import_localeDetector = require('./localeDetector.cjs');
37
- const { internationalization, middleware } = import_built.default;
38
- const { locales, defaultLocale } = internationalization;
39
- const {
40
- headerName,
41
- cookieName,
42
- prefixDefault,
43
- basePath,
44
- serverSetCookie,
45
- noPrefix,
46
- detectLocaleOnPrefetchNoPrefix
47
- } = middleware;
1
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
+ const require_middleware_localeDetector = require('./localeDetector.cjs');
3
+ let __intlayer_config_built = require("@intlayer/config/built");
4
+ __intlayer_config_built = require_rolldown_runtime.__toESM(__intlayer_config_built);
5
+ let __intlayer_core = require("@intlayer/core");
6
+ __intlayer_core = require_rolldown_runtime.__toESM(__intlayer_core);
7
+ let next_server = require("next/server");
8
+ next_server = require_rolldown_runtime.__toESM(next_server);
9
+
10
+ //#region src/middleware/intlayerMiddleware.ts
11
+ const { internationalization, routing } = __intlayer_config_built.default ?? {};
12
+ const { locales, defaultLocale } = internationalization ?? {};
13
+ const { headerName, basePath, detectLocaleOnPrefetchNoPrefix, mode } = routing ?? {};
14
+ const noPrefix = mode === "no-prefix" || mode === "search-params";
15
+ const prefixDefault = mode === "prefix-all";
16
+ /**
17
+ * Detects if the request is a prefetch request from Next.js.
18
+ *
19
+ * Next.js prefetch requests can be identified by several headers:
20
+ * - purpose: 'prefetch' (standard prefetch header)
21
+ * - next-router-prefetch: '1' (Next.js router prefetch)
22
+ * - next-url: present (Next.js internal navigation)
23
+ *
24
+ * During prefetch, we should ignore cookie-based locale detection
25
+ * to prevent unwanted redirects when users are switching locales.
26
+ *
27
+ * @param request - The incoming Next.js request object.
28
+ * @returns - True if the request is a prefetch request, false otherwise.
29
+ */
48
30
  const isPrefetchRequest = (request) => {
49
- const purpose = request.headers.get("purpose");
50
- const nextRouterPrefetch = request.headers.get("next-router-prefetch");
51
- const nextUrl = request.headers.get("next-url");
52
- const xNextjsData = request.headers.get("x-nextjs-data");
53
- return purpose === "prefetch" || nextRouterPrefetch === "1" || !!nextUrl || !!xNextjsData;
31
+ const purpose = request.headers.get("purpose");
32
+ const nextRouterPrefetch = request.headers.get("next-router-prefetch");
33
+ const nextUrl = request.headers.get("next-url");
34
+ const xNextjsData = request.headers.get("x-nextjs-data");
35
+ return purpose === "prefetch" || nextRouterPrefetch === "1" || !!nextUrl || !!xNextjsData;
54
36
  };
55
- const intlayerMiddleware = (request, _event, _response) => {
56
- const pathname = request.nextUrl.pathname;
57
- const cookieLocale = getCookieLocale(request);
58
- const basePathTrailingSlash = basePath.endsWith("/");
59
- if (noPrefix) {
60
- return handleNoPrefix(
61
- request,
62
- cookieLocale,
63
- pathname,
64
- basePathTrailingSlash
65
- );
66
- }
67
- const pathLocale = getPathLocale(pathname);
68
- return handlePrefix(
69
- request,
70
- cookieLocale,
71
- pathLocale,
72
- pathname,
73
- basePathTrailingSlash
74
- );
37
+ const appendLocaleSearchIfNeeded = (search, locale) => {
38
+ if (mode !== "search-params") return search;
39
+ const params = new URLSearchParams(search ?? "");
40
+ params.set("locale", locale);
41
+ return `?${params.toString()}`;
75
42
  };
76
- const getCookieLocale = (request) => {
77
- if (!cookieName) return void 0;
78
- const cookieValue = request.cookies.get(cookieName)?.value;
79
- if (cookieValue && locales.includes(cookieValue)) {
80
- return cookieValue;
81
- }
43
+ /**
44
+ * Middleware that handles the internationalization layer
45
+ *
46
+ * Usage:
47
+ *
48
+ * ```ts
49
+ * // ./src/middleware.ts
50
+ *
51
+ * export { intlayerMiddleware as middleware } from '@intlayer/next/middleware';
52
+ *
53
+ * // applies this middleware only to files in the app directory
54
+ * export const config = {
55
+ * matcher: '/((?!api|static|.*\\..*|_next).*)',
56
+ * };
57
+ * ```
58
+ *
59
+ * Main middleware function for handling internationalization.
60
+ *
61
+ * @param request - The incoming Next.js request object.
62
+ * @param event - The Next.js fetch event (optional).
63
+ * @param response - The Next.js response object (optional).
64
+ * @returns - The response to be returned to the client.
65
+ */
66
+ const intlayerMiddleware = (request, _event, _response) => {
67
+ const pathname = request.nextUrl.pathname;
68
+ const cookieLocale = getCookieLocale(request);
69
+ const basePathTrailingSlash = basePath.endsWith("/");
70
+ if (noPrefix) return handleNoPrefix(request, cookieLocale, pathname, basePathTrailingSlash);
71
+ return handlePrefix(request, cookieLocale, getPathLocale(pathname), pathname, basePathTrailingSlash);
82
72
  };
73
+ /**
74
+ * Retrieves the locale from the request cookies if available and valid.
75
+ *
76
+ * @param request - The incoming Next.js request object.
77
+ * @returns - The locale found in the cookies, or undefined if not found or invalid.
78
+ */
79
+ const getCookieLocale = (request) => (0, __intlayer_core.getLocaleFromStorage)({ getCookie: (name) => request.cookies.get(name)?.value ?? null });
80
+ /**
81
+ * Handles the case where URLs do not have locale prefixes.
82
+ *
83
+ * @param request - The incoming Next.js request object.
84
+ * @param cookieLocale - The locale from the cookie.
85
+ * @param pathname - The pathname from the request URL.
86
+ * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.
87
+ * @returns - The rewritten response with the locale applied.
88
+ */
83
89
  const handleNoPrefix = (request, cookieLocale, pathname, basePathTrailingSlash) => {
84
- const locale = cookieLocale ?? defaultLocale;
85
- const newPath = constructPath(
86
- locale,
87
- pathname,
88
- basePath,
89
- basePathTrailingSlash,
90
- request.nextUrl.search
91
- );
92
- return rewriteUrl(request, newPath, locale);
90
+ const locale = cookieLocale ?? defaultLocale;
91
+ return rewriteUrl(request, constructPath(locale, pathname, basePath, basePathTrailingSlash, appendLocaleSearchIfNeeded(request.nextUrl.search, locale)), locale);
93
92
  };
94
- const getPathLocale = (pathname) => locales.find(
95
- (locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
96
- );
93
+ /**
94
+ * Extracts the locale from the URL pathname if present.
95
+ *
96
+ * @param pathname - The pathname from the request URL.
97
+ * @returns - The locale found in the pathname, or undefined if not found.
98
+ */
99
+ const getPathLocale = (pathname) => locales.find((locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`);
100
+ /**
101
+ * Handles the case where URLs have locale prefixes.
102
+ *
103
+ * @param request - The incoming Next.js request object.
104
+ * @param cookieLocale - The locale from the cookie.
105
+ * @param pathLocale - The locale extracted from the pathname.
106
+ * @param pathname - The pathname from the request URL.
107
+ * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.
108
+ * @returns - The response to be returned to the client.
109
+ */
97
110
  const handlePrefix = (request, cookieLocale, pathLocale, pathname, basePathTrailingSlash) => {
98
- if (!pathLocale) {
99
- const isPrefetch = isPrefetchRequest(request);
100
- if (isPrefetch && !detectLocaleOnPrefetchNoPrefix) {
101
- return handleMissingPathLocale(
102
- request,
103
- defaultLocale,
104
- pathname,
105
- basePathTrailingSlash
106
- );
107
- }
108
- return handleMissingPathLocale(
109
- request,
110
- cookieLocale,
111
- pathname,
112
- basePathTrailingSlash
113
- );
114
- }
115
- return handleExistingPathLocale(
116
- request,
117
- cookieLocale,
118
- pathLocale,
119
- pathname,
120
- basePathTrailingSlash
121
- );
111
+ if (!pathLocale) {
112
+ if (isPrefetchRequest(request) && !detectLocaleOnPrefetchNoPrefix) return handleMissingPathLocale(request, defaultLocale, pathname, basePathTrailingSlash);
113
+ return handleMissingPathLocale(request, cookieLocale, pathname, basePathTrailingSlash);
114
+ }
115
+ return handleExistingPathLocale(request, cookieLocale, pathLocale, pathname, basePathTrailingSlash);
122
116
  };
117
+ /**
118
+ * Handles requests where the locale is missing from the URL pathname.
119
+ *
120
+ * @param request - The incoming Next.js request object.
121
+ * @param cookieLocale - The locale from the cookie.
122
+ * @param pathname - The pathname from the request URL.
123
+ * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.
124
+ * @returns - The response to be returned to the client.
125
+ */
123
126
  const handleMissingPathLocale = (request, cookieLocale, pathname, basePathTrailingSlash) => {
124
- let locale = cookieLocale ?? (0, import_localeDetector.localeDetector)?.(request) ?? defaultLocale;
125
- if (!locales.includes(locale)) {
126
- locale = defaultLocale;
127
- }
128
- const newPath = constructPath(
129
- locale,
130
- pathname,
131
- basePath,
132
- basePathTrailingSlash,
133
- request.nextUrl.search
134
- );
135
- return prefixDefault || locale !== defaultLocale ? redirectUrl(request, newPath) : rewriteUrl(request, newPath, locale);
127
+ let locale = cookieLocale ?? require_middleware_localeDetector.localeDetector?.(request) ?? defaultLocale;
128
+ if (!locales.includes(locale)) locale = defaultLocale;
129
+ const newPath = constructPath(locale, pathname, basePath, basePathTrailingSlash, appendLocaleSearchIfNeeded(request.nextUrl.search, locale));
130
+ return prefixDefault || locale !== defaultLocale ? redirectUrl(request, newPath) : rewriteUrl(request, newPath, locale);
136
131
  };
132
+ /**
133
+ * Handles requests where the locale exists in the URL pathname.
134
+ *
135
+ * @param request - The incoming Next.js request object.
136
+ * @param cookieLocale - The locale from the cookie.
137
+ * @param pathLocale - The locale extracted from the pathname.
138
+ * @param pathname - The pathname from the request URL.
139
+ * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.
140
+ * @returns - The response to be returned to the client.
141
+ */
137
142
  const handleExistingPathLocale = (request, cookieLocale, pathLocale, pathname, basePathTrailingSlash) => {
138
- if (
139
- // If the cookie locale is set and differs from the locale in the URL, and server should not always set cookie
140
- cookieLocale && cookieLocale !== pathLocale && serverSetCookie !== "always"
141
- ) {
142
- const newPath = handleCookieLocaleMismatch(
143
- request,
144
- pathname,
145
- pathLocale,
146
- cookieLocale,
147
- basePath,
148
- basePathTrailingSlash
149
- );
150
- return redirectUrl(request, newPath);
151
- }
152
- return handleDefaultLocaleRedirect(
153
- request,
154
- pathLocale,
155
- pathname,
156
- basePathTrailingSlash
157
- );
143
+ if (cookieLocale && cookieLocale !== pathLocale) return redirectUrl(request, handleCookieLocaleMismatch(request, pathname, pathLocale, cookieLocale, basePath, basePathTrailingSlash));
144
+ return handleDefaultLocaleRedirect(request, pathLocale, pathname, basePathTrailingSlash);
158
145
  };
159
- const handleCookieLocaleMismatch = (request, pathname, pathLocale, cookieLocale, basePath2, basePathTrailingSlash) => {
160
- const newPath = pathname.replace(`/${pathLocale}`, `/${cookieLocale}`);
161
- return constructPath(
162
- cookieLocale,
163
- newPath,
164
- basePath2,
165
- basePathTrailingSlash,
166
- request.nextUrl.search
167
- );
146
+ /**
147
+ * Handles the scenario where the locale in the cookie does not match the locale in the URL pathname.
148
+ *
149
+ * @param request - The incoming Next.js request object.
150
+ * @param pathname - The pathname from the request URL.
151
+ * @param pathLocale - The locale extracted from the pathname.
152
+ * @param cookieLocale - The locale from the cookie.
153
+ * @param basePath - The base path of the application.
154
+ * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.
155
+ * @returns - The new URL path with the correct locale.
156
+ */
157
+ const handleCookieLocaleMismatch = (request, pathname, pathLocale, cookieLocale, basePath$1, basePathTrailingSlash) => {
158
+ return constructPath(cookieLocale, pathname.replace(`/${pathLocale}`, `/${cookieLocale}`), basePath$1, basePathTrailingSlash, appendLocaleSearchIfNeeded(request.nextUrl.search, cookieLocale));
168
159
  };
160
+ /**
161
+ * Handles redirection when the default locale is used and prefixing is not required.
162
+ *
163
+ * @param request - The incoming Next.js request object.
164
+ * @param pathLocale - The locale extracted from the pathname.
165
+ * @param pathname - The pathname from the request URL.
166
+ * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.
167
+ * @returns - The rewritten response without the locale prefix.
168
+ */
169
169
  const handleDefaultLocaleRedirect = (request, pathLocale, pathname, basePathTrailingSlash) => {
170
- if (
171
- // If default locale should not be prefixed and the pathLocale is the defaultLocale
172
- !prefixDefault && pathLocale === defaultLocale
173
- ) {
174
- let pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) ?? "/";
175
- if (basePathTrailingSlash) {
176
- pathWithoutLocale = pathWithoutLocale.slice(1);
177
- }
178
- if (request.nextUrl.search) {
179
- pathWithoutLocale += request.nextUrl.search;
180
- }
181
- return rewriteUrl(request, `${basePath}${pathWithoutLocale}`, pathLocale);
182
- }
183
- return rewriteUrl(request, pathname, pathLocale);
170
+ if (!prefixDefault && pathLocale === defaultLocale) {
171
+ let pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) ?? "/";
172
+ if (basePathTrailingSlash) pathWithoutLocale = pathWithoutLocale.slice(1);
173
+ const searchWithLocale$1 = appendLocaleSearchIfNeeded(request.nextUrl.search, pathLocale);
174
+ if (searchWithLocale$1) pathWithoutLocale += searchWithLocale$1;
175
+ else if (request.nextUrl.search) pathWithoutLocale += request.nextUrl.search;
176
+ return rewriteUrl(request, `${basePath}${pathWithoutLocale}`, pathLocale);
177
+ }
178
+ const searchWithLocale = appendLocaleSearchIfNeeded(request.nextUrl.search, pathLocale);
179
+ return rewriteUrl(request, searchWithLocale ? `${pathname}${searchWithLocale}` : pathname, pathLocale);
184
180
  };
185
- const constructPath = (locale, path, basePath2, basePathTrailingSlash, search) => {
186
- let newPath = `${locale}${path}`;
187
- newPath = `${basePath2}${basePathTrailingSlash ? "" : "/"}${newPath}`;
188
- if (search) {
189
- newPath += search;
190
- }
191
- return newPath;
181
+ /**
182
+ * Constructs a new path by combining the locale, path, basePath, and search parameters.
183
+ *
184
+ * @param locale - The locale to include in the path.
185
+ * @param path - The original path from the request.
186
+ * @param basePath - The base path of the application.
187
+ * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.
188
+ * @param [search] - The query string from the request URL (optional).
189
+ * @returns - The constructed new path.
190
+ */
191
+ const constructPath = (locale, path, basePath$1, basePathTrailingSlash, search) => {
192
+ const pathWithLocalePrefix = mode === "search-params" ? path : `${locale}${path}`;
193
+ let newPath = `${basePath$1}${basePathTrailingSlash ? "" : "/"}${pathWithLocalePrefix}`;
194
+ if (search) newPath += search;
195
+ return newPath;
192
196
  };
197
+ /**
198
+ * Rewrites the URL to the new path and sets the locale header.
199
+ *
200
+ * @param request - The incoming Next.js request object.
201
+ * @param newPath - The new path to rewrite to.
202
+ * @param locale - The locale to set in the response header.
203
+ * @returns - The rewritten response.
204
+ */
193
205
  const rewriteUrl = (request, newPath, locale) => {
194
- const search = request.nextUrl.search;
195
- const pathWithSearch = search && !newPath.includes("?") ? `${newPath}${search}` : newPath;
196
- const response = import_server.NextResponse.rewrite(new URL(pathWithSearch, request.url));
197
- response.headers.set(headerName, locale);
198
- return response;
206
+ const search = request.nextUrl.search;
207
+ const pathWithSearch = search && !newPath.includes("?") ? `${newPath}${search}` : newPath;
208
+ const response = next_server.NextResponse.rewrite(new URL(pathWithSearch, request.url));
209
+ response.headers.set(headerName, locale);
210
+ return response;
199
211
  };
212
+ /**
213
+ * Redirects the request to the new path.
214
+ *
215
+ * @param request - The incoming Next.js request object.
216
+ * @param newPath - The new path to redirect to.
217
+ * @returns - The redirect response.
218
+ */
200
219
  const redirectUrl = (request, newPath) => {
201
- const search = request.nextUrl.search;
202
- const pathWithSearch = search && !newPath.includes("?") ? `${newPath}${search}` : newPath;
203
- return import_server.NextResponse.redirect(new URL(pathWithSearch, request.url));
220
+ const search = request.nextUrl.search;
221
+ const pathWithSearch = search && !newPath.includes("?") ? `${newPath}${search}` : newPath;
222
+ return next_server.NextResponse.redirect(new URL(pathWithSearch, request.url));
204
223
  };
205
- // Annotate the CommonJS export names for ESM import in node:
206
- 0 && (module.exports = {
207
- intlayerMiddleware
208
- });
224
+
225
+ //#endregion
226
+ exports.intlayerMiddleware = intlayerMiddleware;
209
227
  //# sourceMappingURL=intlayerMiddleware.cjs.map