next-intlayer 7.5.6 → 7.5.12

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/README.md CHANGED
@@ -5,8 +5,11 @@
5
5
  </p>
6
6
 
7
7
  <h1 align="center">
8
- <strong> Intlayer : an Open-source, per-component i18n toolkit with AI-powered translation & CMS.</strong>
8
+ <strong>Per-component i18n</strong>
9
9
  </h1>
10
+ <h2 align="center">
11
+ <strong>AI-powered translation. Visual Editor. Multilingual CMS.</strong>
12
+ </h2>
10
13
 
11
14
  <br />
12
15
 
@@ -24,6 +27,8 @@
24
27
  <a href="https://github.com/aymericzip/intlayer/blob/main/LICENSE" target="_blank" rel="noopener noreferrer nofollow"><img src="https://img.shields.io/github/license/aymericzip/intlayer?style=for-the-badge&labelColor=000000&color=FFFFFF&logoColor=000000&cacheSeconds=86400" alt="license"/></a>
25
28
  <a href="https://github.com/aymericzip/intlayer/commits/main" target="_blank" rel="noopener noreferrer nofollow"><img src="https://img.shields.io/github/last-commit/aymericzip/intlayer?style=for-the-badge&labelColor=000000&color=FFFFFF&logoColor=000000&cacheSeconds=86400" alt="last commit"/>
26
29
  </a>
30
+ <a href="https://www.bountyhub.dev/en/bounty/view/a2f24259-80ae-4a19-82e7-288718fba449/adapt-markdown-parser-in-a-custom-packages" target="_blank" rel="noopener noreferrer nofollow"><img src="https://img.shields.io/badge/Bounties-on%20BountyHub-yellow?style=for-the-badge&labelColor=000000&color=FFFFFF&logoColor=000000&cacheSeconds=86400" alt="Bounties on BountyHub"/>
31
+ </a>
27
32
  </p>
28
33
 
29
34
  ![Watch the video](https://github.com/aymericzip/intlayer/blob/main/docs/assets/demo_video.gif)
@@ -46,7 +51,7 @@ With **per-locale content files**, **TypeScript autocompletion**, **tree-shakabl
46
51
  | Feature | Description |
47
52
  | --------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
48
53
  | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/frameworks.png?raw=true" alt="Feature" width="700"> | **Cross-Frameworks Support**<br><br>Intlayer is compatible with all major frameworks and libraries, including Next.js, React, Vite, Vue.js, Nuxt, Preact, Express, and more. |
49
- | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/javascript_content_management.png?raw=true" alt="Feature" width="700"> | **JavaScript-Powered Content Management**<br><br>Harness the flexibility of JavaScript to define and manage your content efficiently. <br><br> - [Content declaration](https://intlayer.org/doc/concept/content) |
54
+ | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/javascript_content_management.jpg?raw=true" alt="Feature" width="700"> | **JavaScript-Powered Content Management**<br><br>Harness the flexibility of JavaScript to define and manage your content efficiently. <br><br> - [Content declaration](https://intlayer.org/doc/concept/content) |
50
55
  | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/per_locale_content_declaration_file.png?raw=true" alt="Feature" width="700"> | **Per-Locale Content Declaration File**<br><br>Speed up your development by declaring your content once, before auto generation.<br><br> - [Per-Locale Content Declaration File](https://intlayer.org/doc/concept/per-locale-file) |
51
56
  | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/autocompletion.png?raw=true" alt="Feature" width="700"> | **Type-Safe Environment**<br><br>Leverage TypeScript to ensure your content definitions and code are error-free, while also benefiting from IDE autocompletion.<br><br> - [TypeScript configuration](https://intlayer.org/doc/environment/vite-and-react#configure-typescript) |
52
57
  | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/config_file.png?raw=true" alt="Feature" width="700"> | **Simplified Setup**<br><br>Get up and running quickly with minimal configuration. Adjust settings for internationalization, routing, AI, build, and content handling with ease. <br><br> - [Explore Next.js integration](https://intlayer.org/doc/environment/nextjs) |
@@ -268,6 +273,8 @@ You can also follow us on :
268
273
 
269
274
  For more detailed guidelines on contributing to this project, please refer to the [`CONTRIBUTING.md`](https://github.com/aymericzip/intlayer/blob/main/CONTRIBUTING.md) file. It contains essential information on our development process, commit message conventions, and release procedures. Your contributions are valuable to us, and we appreciate your efforts in making this project better!
270
275
 
276
+ Contribute on [GitHub](https://github.com/aymericzip/intlayer), [GitLab](https://gitlab.com/ay.pineau/intlayer), or [Bitbucket](https://bitbucket.org/intlayer/intlayer/).
277
+
271
278
  ### Thank You for the Support
272
279
 
273
280
  If you like Intlayer, give us a ⭐ on GitHub. It helps others discover the project! [See why GitHub Stars matter](https://github.com/aymericzip/intlayer/blob/main/CONTRIBUTING.md#why-github-stars-matter-).
@@ -81,28 +81,15 @@ const getLocalLocale = (request) => (0, _intlayer_core.getLocaleFromStorage)({
81
81
  });
82
82
  /**
83
83
  * Handles the case where URLs do not have locale prefixes.
84
- *
85
- * @param request - The incoming Next.js request object.
86
- * @param localLocale - The locale from the cookie.
87
- * @param pathname - The pathname from the request URL.
88
- * @returns - The rewritten response with the locale applied.
89
84
  */
90
85
  const handleNoPrefix = (request, localLocale, pathname) => {
91
86
  const pathLocale = getPathLocale(pathname);
92
- if (pathLocale) {
93
- const pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || "/";
94
- const search$1 = appendLocaleSearchIfNeeded(request.nextUrl.search, pathLocale);
95
- return redirectUrl(request, search$1 ? `${pathWithoutLocale}${search$1}` : `${pathWithoutLocale}${request.nextUrl.search ?? ""}`);
96
- }
97
87
  const locale = localLocale ?? defaultLocale;
88
+ if (pathLocale) return redirectUrl(request, `${pathname.slice(`/${pathLocale}`.length) || "/"}${appendLocaleSearchIfNeeded(request.nextUrl.search, pathLocale)}`);
98
89
  if (effectiveMode === "search-params") {
99
- if (new URLSearchParams(request.nextUrl.search).get("locale") === locale) return rewriteUrl(request, `${`/${locale}${pathname}`}${request.nextUrl.search ?? ""}`, locale);
100
- const search$1 = appendLocaleSearchIfNeeded(request.nextUrl.search, locale);
101
- return redirectUrl(request, search$1 ? `${pathname}${search$1}` : `${pathname}${request.nextUrl.search ?? ""}`);
90
+ if (request.nextUrl.searchParams.get("locale") !== locale) return redirectUrl(request, `${pathname}${appendLocaleSearchIfNeeded(request.nextUrl.search, locale)}`);
102
91
  }
103
- const internalPath = `/${locale}${pathname}`;
104
- const search = appendLocaleSearchIfNeeded(request.nextUrl.search, locale);
105
- return rewriteUrl(request, search ? `${internalPath}${search}` : `${internalPath}${request.nextUrl.search ?? ""}`, locale);
92
+ return rewriteUrl(request, pathname, locale);
106
93
  };
107
94
  /**
108
95
  * Extracts the locale from the URL pathname if present.
@@ -140,8 +127,8 @@ const handlePrefix = (request, localLocale, pathLocale, pathname) => {
140
127
  const handleMissingPathLocale = (request, localLocale, pathname) => {
141
128
  let locale = localLocale ?? require_proxy_localeDetector.localeDetector?.(request) ?? defaultLocale;
142
129
  if (!locales.includes(locale)) locale = defaultLocale;
143
- const newPath = constructPath(locale, pathname, basePath, appendLocaleSearchIfNeeded(request.nextUrl.search, locale));
144
- return prefixDefault || locale !== defaultLocale ? redirectUrl(request, newPath) : rewriteUrl(request, newPath, locale);
130
+ if (prefixDefault || locale !== defaultLocale) return redirectUrl(request, constructPath(locale, pathname, basePath, appendLocaleSearchIfNeeded(request.nextUrl.search, locale)));
131
+ return rewriteUrl(request, pathname, locale);
145
132
  };
146
133
  /**
147
134
  * Handles requests where the locale exists in the URL pathname.
@@ -152,11 +139,23 @@ const handleMissingPathLocale = (request, localLocale, pathname) => {
152
139
  * @param pathname - The pathname from the request URL.
153
140
  * @returns - The response to be returned to the client.
154
141
  */
142
+ /**
143
+ * Handles requests where the locale exists in the URL pathname.
144
+ */
155
145
  const handleExistingPathLocale = (request, localLocale, pathLocale, pathname) => {
156
146
  if (localLocale && localLocale !== pathLocale) return redirectUrl(request, handleCookieLocaleMismatch(request, pathname, pathLocale, localLocale, basePath));
157
147
  return handleDefaultLocaleRedirect(request, pathLocale, pathname);
158
148
  };
159
149
  /**
150
+ * The key fix for 404s without [locale] folders
151
+ */
152
+ const handleDefaultLocaleRedirect = (request, pathLocale, pathname) => {
153
+ if (!prefixDefault && pathLocale === defaultLocale) return redirectUrl(request, `${basePath}${pathname.slice(`/${pathLocale}`.length) || "/"}`);
154
+ const internalPathname = pathname.slice(`/${pathLocale}`.length) || "/";
155
+ const searchWithLocale = appendLocaleSearchIfNeeded(request.nextUrl.search, pathLocale);
156
+ return rewriteUrl(request, searchWithLocale ? `${internalPathname}${searchWithLocale}` : internalPathname, pathLocale);
157
+ };
158
+ /**
160
159
  * Handles the scenario where the locale in the cookie does not match the locale in the URL pathname.
161
160
  *
162
161
  * @param request - The incoming Next.js request object.
@@ -170,26 +169,6 @@ const handleCookieLocaleMismatch = (request, pathname, pathLocale, localLocale,
170
169
  return constructPath(localLocale, pathname.replace(`/${pathLocale}`, `/${localLocale}`), basePath$1, appendLocaleSearchIfNeeded(request.nextUrl.search, localLocale));
171
170
  };
172
171
  /**
173
- * Handles redirection when the default locale is used and prefixing is not required.
174
- *
175
- * @param request - The incoming Next.js request object.
176
- * @param pathLocale - The locale extracted from the pathname.
177
- * @param pathname - The pathname from the request URL.
178
- * @returns - The rewritten response without the locale prefix.
179
- */
180
- const handleDefaultLocaleRedirect = (request, pathLocale, pathname) => {
181
- if (!prefixDefault && pathLocale === defaultLocale) {
182
- let pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || "/";
183
- if (basePath.endsWith("/")) pathWithoutLocale = pathWithoutLocale.slice(1);
184
- const searchWithLocale$1 = appendLocaleSearchIfNeeded(request.nextUrl.search, pathLocale);
185
- if (searchWithLocale$1) pathWithoutLocale += searchWithLocale$1;
186
- else if (request.nextUrl.search) pathWithoutLocale += request.nextUrl.search;
187
- return redirectUrl(request, `${basePath}${pathWithoutLocale}`);
188
- }
189
- const searchWithLocale = appendLocaleSearchIfNeeded(request.nextUrl.search, pathLocale);
190
- return rewriteUrl(request, searchWithLocale ? `${pathname}${searchWithLocale}` : pathname, pathLocale);
191
- };
192
- /**
193
172
  * Constructs a new path by combining the locale, path, basePath, and search parameters.
194
173
  *
195
174
  * @param locale - The locale to include in the path.
@@ -199,31 +178,36 @@ const handleDefaultLocaleRedirect = (request, pathLocale, pathname) => {
199
178
  * @returns - The constructed new path.
200
179
  */
201
180
  const constructPath = (locale, path, basePath$1, search) => {
202
- const pathWithoutPrefix = path.startsWith(`/${locale}`) ? path.slice(`/${locale}`.length) || "/" : path;
203
- if (effectiveMode === "no-prefix") {
204
- if (search) return `${pathWithoutPrefix}?${search}`;
205
- return pathWithoutPrefix;
206
- }
207
- if (effectiveMode === "search-params") {
208
- if (search) return `${pathWithoutPrefix}?${search}`;
209
- return pathWithoutPrefix;
210
- }
211
- const pathWithLocalePrefix = path.startsWith(`/${locale}`) ? path : `${locale}${path}`;
212
- return `${basePath$1}${basePath$1.endsWith("/") ? "" : "/"}${pathWithLocalePrefix}`;
181
+ const normalizedPath = path.startsWith("/") ? path : `/${path}`;
182
+ let finalPath = normalizedPath;
183
+ if (effectiveMode === "no-prefix" || effectiveMode === "search-params") {
184
+ for (const loc of locales) if (normalizedPath.startsWith(`/${loc}/`) || normalizedPath === `/${loc}`) {
185
+ finalPath = normalizedPath.slice(`/${loc}`.length) || "/";
186
+ break;
187
+ }
188
+ } else finalPath = normalizedPath.startsWith(`/${locale}`) ? normalizedPath : `/${locale}${normalizedPath}`;
189
+ const result = `${basePath$1.replace(/\/$/, "")}${finalPath}`;
190
+ return search ? `${result}${search.startsWith("?") ? "" : "?"}${search}` : result;
213
191
  };
214
192
  /**
215
- * Rewrites the URL to the new path and sets the locale header.
216
- *
217
- * @param request - The incoming Next.js request object.
218
- * @param newPath - The new path to rewrite to.
219
- * @param locale - The locale to set in the response header.
220
- * @returns - The rewritten response.
193
+ * This handles the internal path Next.js sees.
194
+ * To support optional [locale] folders, we need to decide if we
195
+ * keep the locale prefix or strip it.
221
196
  */
222
197
  const rewriteUrl = (request, newPath, locale) => {
223
- const search = request.nextUrl.search;
224
- const pathWithSearch = search && !newPath.includes("?") ? `${newPath}${search}` : newPath;
225
- const response = next_server.NextResponse.rewrite(new URL(pathWithSearch, request.url));
226
- (0, _intlayer_core.setLocaleInStorage)(locale, { setHeader: (name, value) => response.headers.set(name, value) });
198
+ const url = request.nextUrl.clone();
199
+ const pathname = newPath.split("?")[0];
200
+ if (effectiveMode !== "no-prefix" && effectiveMode !== "search-params") if (!getPathLocale(pathname)) url.pathname = `/${locale}${pathname === "/" ? "" : pathname}`;
201
+ else url.pathname = pathname;
202
+ else {
203
+ const pathLocale = getPathLocale(pathname);
204
+ if (pathLocale) url.pathname = pathname.slice(`/${pathLocale}`.length) || "/";
205
+ else url.pathname = pathname;
206
+ }
207
+ const response = next_server.NextResponse.rewrite(url);
208
+ (0, _intlayer_core.setLocaleInStorage)(locale, { setHeader: (name, value) => {
209
+ response.headers.set(name, value);
210
+ } });
227
211
  return response;
228
212
  };
229
213
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"intlayerProxy.cjs","names":["configuration","DefaultValues","search","localeDetector","basePath","searchWithLocale","NextResponse"],"sources":["../../../src/proxy/intlayerProxy.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { DefaultValues } from '@intlayer/config/client';\nimport { getLocaleFromStorage, setLocaleInStorage } from '@intlayer/core';\nimport type { Locale } from '@intlayer/types';\nimport {\n type NextFetchEvent,\n type NextRequest,\n NextResponse,\n} from 'next/server';\nimport { localeDetector } from './localeDetector';\n\n/**\n * Controls whether locale detection occurs during Next.js prefetch requests\n * - true: Detect and apply locale during prefetch\n * - false: Use default locale during prefetch (recommended)\n *\n * This setting affects how Next.js handles locale prefetching:\n *\n * Example scenario:\n * - User's browser language is 'fr'\n * - Current page is /fr/about\n * - Link prefetches /about\n *\n * With `detectLocaleOnPrefetchNoPrefix:true`\n * - Prefetch detects 'fr' locale from browser\n * - Redirects prefetch to /fr/about\n *\n * With `detectLocaleOnPrefetchNoPrefix:false` (default)\n * - Prefetch uses default locale\n * - Redirects prefetch to /en/about (assuming 'en' is default)\n *\n * When to use true:\n * - Your app uses non-localized internal links (e.g. <a href=\"/about\">)\n * - You want consistent locale detection behavior between regular and prefetch requests\n *\n * When to use false (default):\n * - Your app uses locale-prefixed links (e.g. <a href=\"/fr/about\">)\n * - You want to optimize prefetching performance\n * - You want to avoid potential redirect loops\n */\nconst DEFAULT_DETECT_LOCALE_ON_PREFETCH_NO_PREFIX = false;\n\nconst { internationalization, routing } = configuration ?? {};\nconst { locales, defaultLocale } = internationalization ?? {};\nconst { basePath, mode } = routing ?? {};\n// Note: cookie names are resolved inside LocaleStorage based on configuration\n\n// Derived flags from routing.mode\nconst effectiveMode = mode ?? DefaultValues.Routing.ROUTING_MODE;\nconst noPrefix =\n effectiveMode === 'no-prefix' || effectiveMode === 'search-params';\nconst prefixDefault = effectiveMode === 'prefix-all';\n\n/**\n * Detects if the request is a prefetch request from Next.js.\n *\n * Next.js prefetch requests can be identified by several headers:\n * - purpose: 'prefetch' (standard prefetch header)\n * - next-router-prefetch: '1' (Next.js router prefetch)\n * - next-url: present (Next.js internal navigation)\n *\n * During prefetch, we should ignore cookie-based locale detection\n * to prevent unwanted redirects when users are switching locales.\n *\n * @param request - The incoming Next.js request object.\n * @returns - True if the request is a prefetch request, false otherwise.\n */\nconst isPrefetchRequest = (request: NextRequest): boolean => {\n const purpose = request.headers.get('purpose');\n const nextRouterPrefetch = request.headers.get('next-router-prefetch');\n const nextUrl = request.headers.get('next-url');\n const xNextjsData = request.headers.get('x-nextjs-data');\n\n return (\n purpose === 'prefetch' ||\n nextRouterPrefetch === '1' ||\n !!nextUrl ||\n !!xNextjsData\n );\n};\n\n// Ensure locale is reflected in search params when routing mode is 'search-params'\nconst appendLocaleSearchIfNeeded = (\n search: string | undefined,\n locale: Locale\n): string | undefined => {\n if (effectiveMode !== 'search-params') return search;\n\n const params = new URLSearchParams(search ?? '');\n\n params.set('locale', locale);\n\n return `?${params.toString()}`;\n};\n\n/**\n * Proxy that handles the internationalization layer\n *\n * Usage:\n *\n * ```ts\n * // ./src/proxy.ts\n *\n * export { intlayerProxy as proxy } from '@intlayer/next/proxy';\n *\n * // applies this proxy only to files in the app directory\n * export const config = {\n * matcher: '/((?!api|static|.*\\\\..*|_next).*)',\n * };\n * ```\n *\n * Main proxy function for handling internationalization.\n *\n * @param request - The incoming Next.js request object.\n * @param event - The Next.js fetch event (optional).\n * @param response - The Next.js response object (optional).\n * @returns - The response to be returned to the client.\n */\nexport const intlayerProxy = (\n request: NextRequest,\n _event?: NextFetchEvent,\n _response?: NextResponse\n): NextResponse => {\n const pathname = request.nextUrl.pathname;\n\n const localLocale = getLocalLocale(request);\n\n if (\n noPrefix // If the application is configured not to use locale prefixes in URLs\n ) {\n return handleNoPrefix(request, localLocale, pathname);\n }\n\n const pathLocale = getPathLocale(pathname);\n\n return handlePrefix(request, localLocale, pathLocale, pathname);\n};\n\n/**\n * Retrieves the locale from the request cookies if available and valid.\n *\n * @param request - The incoming Next.js request object.\n * @returns - The locale found in the cookies, or undefined if not found or invalid.\n */\nconst getLocalLocale = (request: NextRequest): Locale | undefined =>\n getLocaleFromStorage({\n getCookie: (name: string) => request.cookies.get(name)?.value ?? null,\n getHeader: (name: string) => request.headers.get(name) ?? null,\n });\n\n/**\n * Handles the case where URLs do not have locale prefixes.\n *\n * @param request - The incoming Next.js request object.\n * @param localLocale - The locale from the cookie.\n * @param pathname - The pathname from the request URL.\n * @returns - The rewritten response with the locale applied.\n */\nconst handleNoPrefix = (\n request: NextRequest,\n localLocale: Locale | undefined,\n pathname: string\n): NextResponse => {\n // Check if pathname has a locale prefix (even though we're in no-prefix mode)\n const pathLocale = getPathLocale(pathname);\n\n // If a locale prefix is detected in the URL, redirect to remove it\n if (pathLocale) {\n // Strip the locale prefix from the pathname\n const pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || '/';\n\n // Build redirect URL without locale prefix but with search params if needed\n const search = appendLocaleSearchIfNeeded(\n request.nextUrl.search,\n pathLocale\n );\n const redirectPath = search\n ? `${pathWithoutLocale}${search}`\n : `${pathWithoutLocale}${request.nextUrl.search ?? ''}`;\n\n // Redirect to the path without locale prefix (URL changes in browser)\n return redirectUrl(request, redirectPath);\n }\n\n // If no locale prefix in URL, determine locale and rewrite internally\n const locale = localLocale ?? defaultLocale;\n\n // In search-params mode, we need to redirect to add the locale search param\n if (effectiveMode === 'search-params') {\n // Check if locale search param already exists and matches the detected locale\n const existingSearchParams = new URLSearchParams(request.nextUrl.search);\n const existingLocale = existingSearchParams.get('locale');\n\n // If the existing locale matches the detected locale, no redirect needed\n if (existingLocale === locale) {\n // For internal routing, we need to add the locale prefix so Next.js can match [locale] param\n const internalPath = `/${locale}${pathname}`;\n const rewritePath = `${internalPath}${request.nextUrl.search ?? ''}`;\n\n // Rewrite internally (URL stays the same in browser, but Next.js routes to /[locale]/path)\n return rewriteUrl(request, rewritePath, locale);\n }\n\n const search = appendLocaleSearchIfNeeded(request.nextUrl.search, locale);\n const redirectPath = search\n ? `${pathname}${search}`\n : `${pathname}${request.nextUrl.search ?? ''}`;\n\n // Redirect to add/update the locale search param (URL changes in browser)\n return redirectUrl(request, redirectPath);\n }\n\n // For internal routing, we need to add the locale prefix so Next.js can match [locale] param\n const internalPath = `/${locale}${pathname}`;\n\n // Add search params if needed\n const search = appendLocaleSearchIfNeeded(request.nextUrl.search, locale);\n const rewritePath = search\n ? `${internalPath}${search}`\n : `${internalPath}${request.nextUrl.search ?? ''}`;\n\n // Rewrite internally (URL stays the same in browser, but Next.js routes to /[locale]/path)\n return rewriteUrl(request, rewritePath, locale);\n};\n\n/**\n * Extracts the locale from the URL pathname if present.\n *\n * @param pathname - The pathname from the request URL.\n * @returns - The locale found in the pathname, or undefined if not found.\n */\nconst getPathLocale = (pathname: string): Locale | undefined =>\n locales.find(\n (locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`\n );\n\n/**\n * Handles the case where URLs have locale prefixes.\n *\n * @param request - The incoming Next.js request object.\n * @param localLocale - The locale from the cookie.\n * @param pathLocale - The locale extracted from the pathname.\n * @param pathname - The pathname from the request URL.\n * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The response to be returned to the client.\n */\nconst handlePrefix = (\n request: NextRequest,\n localLocale: Locale | undefined,\n pathLocale: Locale | undefined,\n pathname: string\n): NextResponse => {\n if (\n !pathLocale // If the URL does not contain a locale prefix\n ) {\n const isPrefetch = isPrefetchRequest(request);\n\n if (isPrefetch && !DEFAULT_DETECT_LOCALE_ON_PREFETCH_NO_PREFIX) {\n return handleMissingPathLocale(request, defaultLocale, pathname);\n }\n\n return handleMissingPathLocale(request, localLocale, pathname);\n }\n\n // If the URL contains a locale prefix\n return handleExistingPathLocale(request, localLocale, pathLocale, pathname);\n};\n\n/**\n * Handles requests where the locale is missing from the URL pathname.\n *\n * @param request - The incoming Next.js request object.\n * @param localLocale - The locale from the cookie.\n * @param pathname - The pathname from the request URL.\n * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The response to be returned to the client.\n */\nconst handleMissingPathLocale = (\n request: NextRequest,\n localLocale: Locale | undefined,\n pathname: string\n): NextResponse => {\n let locale = (localLocale ??\n localeDetector?.(request) ??\n defaultLocale) as Locale;\n if (!locales.includes(locale)) {\n locale = defaultLocale;\n }\n\n const newPath = constructPath(\n locale,\n pathname,\n basePath,\n appendLocaleSearchIfNeeded(request.nextUrl.search, locale)\n );\n\n return prefixDefault || locale !== defaultLocale\n ? redirectUrl(request, newPath)\n : rewriteUrl(request, newPath, locale);\n};\n\n/**\n * Handles requests where the locale exists in the URL pathname.\n *\n * @param request - The incoming Next.js request object.\n * @param localLocale - The locale from the cookie.\n * @param pathLocale - The locale extracted from the pathname.\n * @param pathname - The pathname from the request URL.\n * @returns - The response to be returned to the client.\n */\nconst handleExistingPathLocale = (\n request: NextRequest,\n localLocale: Locale | undefined,\n pathLocale: Locale,\n pathname: string\n): NextResponse => {\n if (\n // If the cookie locale is set and differs from the locale in the URL\n localLocale &&\n localLocale !== pathLocale\n ) {\n const newPath = handleCookieLocaleMismatch(\n request,\n pathname,\n pathLocale,\n localLocale,\n basePath\n );\n return redirectUrl(request, newPath);\n }\n\n // If the cookie locale matches the path locale, or cookie locale is not set, or serverSetCookie is 'always'\n return handleDefaultLocaleRedirect(request, pathLocale, pathname);\n};\n\n/**\n * Handles the scenario where the locale in the cookie does not match the locale in the URL pathname.\n *\n * @param request - The incoming Next.js request object.\n * @param pathname - The pathname from the request URL.\n * @param pathLocale - The locale extracted from the pathname.\n * @param localLocale - The locale from the cookie.\n * @param basePath - The base path of the application.\n * @returns - The new URL path with the correct locale.\n */\nconst handleCookieLocaleMismatch = (\n request: NextRequest,\n pathname: string,\n pathLocale: Locale,\n localLocale: Locale,\n basePath: string\n): string => {\n // Replace the pathLocale in the pathname with the localLocale\n const newPath = pathname.replace(`/${pathLocale}`, `/${localLocale}`);\n\n return constructPath(\n localLocale,\n newPath,\n basePath,\n appendLocaleSearchIfNeeded(request.nextUrl.search, localLocale)\n );\n};\n\n/**\n * Handles redirection when the default locale is used and prefixing is not required.\n *\n * @param request - The incoming Next.js request object.\n * @param pathLocale - The locale extracted from the pathname.\n * @param pathname - The pathname from the request URL.\n * @returns - The rewritten response without the locale prefix.\n */\nconst handleDefaultLocaleRedirect = (\n request: NextRequest,\n pathLocale: Locale,\n pathname: string\n): NextResponse => {\n if (\n // If default locale should not be prefixed and the pathLocale is the defaultLocale\n !prefixDefault &&\n pathLocale === defaultLocale\n ) {\n let pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || '/';\n\n const basePathTrailingSlash = basePath.endsWith('/');\n\n if (basePathTrailingSlash) {\n pathWithoutLocale = pathWithoutLocale.slice(1);\n }\n\n const searchWithLocale = appendLocaleSearchIfNeeded(\n request.nextUrl.search,\n pathLocale\n );\n if (searchWithLocale) {\n pathWithoutLocale += searchWithLocale;\n } else if (request.nextUrl.search) {\n pathWithoutLocale += request.nextUrl.search;\n }\n\n return redirectUrl(request, `${basePath}${pathWithoutLocale}`);\n }\n\n // If prefixing default locale is required or pathLocale is not the defaultLocale\n\n const searchWithLocale = appendLocaleSearchIfNeeded(\n request.nextUrl.search,\n pathLocale\n );\n const newPath = searchWithLocale\n ? `${pathname}${searchWithLocale}`\n : pathname;\n return rewriteUrl(request, newPath, pathLocale);\n};\n\n/**\n * Constructs a new path by combining the locale, path, basePath, and search parameters.\n *\n * @param locale - The locale to include in the path.\n * @param path - The original path from the request.\n * @param basePath - The base path of the application.\n * @param [search] - The query string from the request URL (optional).\n * @returns - The constructed new path.\n */\nconst constructPath = (\n locale: Locale,\n path: string,\n basePath: string,\n search?: string\n): string => {\n // In 'search-params' and 'no-prefix' modes, do not prefix the path with the locale\n // Also, strip any incoming locale prefix if present\n const pathWithoutPrefix = path.startsWith(`/${locale}`)\n ? path.slice(`/${locale}`.length) || '/'\n : path;\n\n if (effectiveMode === 'no-prefix') {\n if (search) {\n return `${pathWithoutPrefix}?${search}`;\n }\n\n return pathWithoutPrefix;\n }\n\n if (effectiveMode === 'search-params') {\n if (search) {\n return `${pathWithoutPrefix}?${search}`;\n }\n\n return pathWithoutPrefix;\n }\n\n const pathWithLocalePrefix = path.startsWith(`/${locale}`)\n ? path\n : `${locale}${path}`;\n\n const basePathTrailingSlash = basePath.endsWith('/');\n\n const newPath = `${basePath}${basePathTrailingSlash ? '' : '/'}${pathWithLocalePrefix}`;\n\n return newPath;\n};\n\n/**\n * Rewrites the URL to the new path and sets the locale header.\n *\n * @param request - The incoming Next.js request object.\n * @param newPath - The new path to rewrite to.\n * @param locale - The locale to set in the response header.\n * @returns - The rewritten response.\n */\nconst rewriteUrl = (\n request: NextRequest,\n newPath: string,\n locale: Locale\n): NextResponse => {\n // Ensure we preserve the original search params if they were present and not explicitly included in newPath\n const search = request.nextUrl.search;\n const pathWithSearch =\n search && !newPath.includes('?') ? `${newPath}${search}` : newPath;\n\n const response = NextResponse.rewrite(new URL(pathWithSearch, request.url));\n\n setLocaleInStorage(locale, {\n setHeader: (name: string, value: string) =>\n response.headers.set(name, value),\n });\n\n return response;\n};\n\n/**\n * Redirects the request to the new path.\n *\n * @param request - The incoming Next.js request object.\n * @param newPath - The new path to redirect to.\n * @returns - The redirect response.\n */\nconst redirectUrl = (request: NextRequest, newPath: string): NextResponse => {\n // Ensure we preserve the original search params if they were present and not explicitly included in newPath\n const search = request.nextUrl.search;\n const pathWithSearch =\n search && !newPath.includes('?') ? `${newPath}${search}` : newPath;\n\n return NextResponse.redirect(new URL(pathWithSearch, request.url));\n};\n"],"mappings":";;;;;;;;;AA0CA,MAAM,EAAE,sBAAsB,YAAYA,kCAAiB,EAAE;AAC7D,MAAM,EAAE,SAAS,kBAAkB,wBAAwB,EAAE;AAC7D,MAAM,EAAE,UAAU,SAAS,WAAW,EAAE;AAIxC,MAAM,gBAAgB,QAAQC,sCAAc,QAAQ;AACpD,MAAM,WACJ,kBAAkB,eAAe,kBAAkB;AACrD,MAAM,gBAAgB,kBAAkB;;;;;;;;;;;;;;;AAgBxC,MAAM,qBAAqB,YAAkC;CAC3D,MAAM,UAAU,QAAQ,QAAQ,IAAI,UAAU;CAC9C,MAAM,qBAAqB,QAAQ,QAAQ,IAAI,uBAAuB;CACtE,MAAM,UAAU,QAAQ,QAAQ,IAAI,WAAW;CAC/C,MAAM,cAAc,QAAQ,QAAQ,IAAI,gBAAgB;AAExD,QACE,YAAY,cACZ,uBAAuB,OACvB,CAAC,CAAC,WACF,CAAC,CAAC;;AAKN,MAAM,8BACJ,QACA,WACuB;AACvB,KAAI,kBAAkB,gBAAiB,QAAO;CAE9C,MAAM,SAAS,IAAI,gBAAgB,UAAU,GAAG;AAEhD,QAAO,IAAI,UAAU,OAAO;AAE5B,QAAO,IAAI,OAAO,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;AA0B9B,MAAa,iBACX,SACA,QACA,cACiB;CACjB,MAAM,WAAW,QAAQ,QAAQ;CAEjC,MAAM,cAAc,eAAe,QAAQ;AAE3C,KACE,SAEA,QAAO,eAAe,SAAS,aAAa,SAAS;AAKvD,QAAO,aAAa,SAAS,aAFV,cAAc,SAAS,EAEY,SAAS;;;;;;;;AASjE,MAAM,kBAAkB,qDACD;CACnB,YAAY,SAAiB,QAAQ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QACA,MACA,YACA,WACW;CAGX,MAAM,oBAAoB,KAAK,WAAW,IAAI,SAAS,GACnD,KAAK,MAAM,IAAI,SAAS,OAAO,IAAI,MACnC;AAEJ,KAAI,kBAAkB,aAAa;AACjC,MAAI,OACF,QAAO,GAAG,kBAAkB,GAAG;AAGjC,SAAO;;AAGT,KAAI,kBAAkB,iBAAiB;AACrC,MAAI,OACF,QAAO,GAAG,kBAAkB,GAAG;AAGjC,SAAO;;CAGT,MAAM,uBAAuB,KAAK,WAAW,IAAI,SAAS,GACtD,OACA,GAAG,SAAS;AAMhB,QAFgB,GAAGD,aAFWA,WAAS,SAAS,IAAI,GAEE,KAAK,MAAM;;;;;;;;;;AAanE,MAAM,cACJ,SACA,SACA,WACiB;CAEjB,MAAM,SAAS,QAAQ,QAAQ;CAC/B,MAAM,iBACJ,UAAU,CAAC,QAAQ,SAAS,IAAI,GAAG,GAAG,UAAU,WAAW;CAE7D,MAAM,WAAWE,yBAAa,QAAQ,IAAI,IAAI,gBAAgB,QAAQ,IAAI,CAAC;AAE3E,wCAAmB,QAAQ,EACzB,YAAY,MAAc,UACxB,SAAS,QAAQ,IAAI,MAAM,MAAM,EACpC,CAAC;AAEF,QAAO;;;;;;;;;AAUT,MAAM,eAAe,SAAsB,YAAkC;CAE3E,MAAM,SAAS,QAAQ,QAAQ;CAC/B,MAAM,iBACJ,UAAU,CAAC,QAAQ,SAAS,IAAI,GAAG,GAAG,UAAU,WAAW;AAE7D,QAAOA,yBAAa,SAAS,IAAI,IAAI,gBAAgB,QAAQ,IAAI,CAAC"}
1
+ {"version":3,"file":"intlayerProxy.cjs","names":["configuration","DefaultValues","localeDetector","basePath","NextResponse"],"sources":["../../../src/proxy/intlayerProxy.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { DefaultValues } from '@intlayer/config/client';\nimport { getLocaleFromStorage, setLocaleInStorage } from '@intlayer/core';\nimport type { Locale } from '@intlayer/types';\nimport {\n type NextFetchEvent,\n type NextRequest,\n NextResponse,\n} from 'next/server';\nimport { localeDetector } from './localeDetector';\n\n/**\n * Controls whether locale detection occurs during Next.js prefetch requests\n * - true: Detect and apply locale during prefetch\n * - false: Use default locale during prefetch (recommended)\n *\n * This setting affects how Next.js handles locale prefetching:\n *\n * Example scenario:\n * - User's browser language is 'fr'\n * - Current page is /fr/about\n * - Link prefetches /about\n *\n * With `detectLocaleOnPrefetchNoPrefix:true`\n * - Prefetch detects 'fr' locale from browser\n * - Redirects prefetch to /fr/about\n *\n * With `detectLocaleOnPrefetchNoPrefix:false` (default)\n * - Prefetch uses default locale\n * - Redirects prefetch to /en/about (assuming 'en' is default)\n *\n * When to use true:\n * - Your app uses non-localized internal links (e.g. <a href=\"/about\">)\n * - You want consistent locale detection behavior between regular and prefetch requests\n *\n * When to use false (default):\n * - Your app uses locale-prefixed links (e.g. <a href=\"/fr/about\">)\n * - You want to optimize prefetching performance\n * - You want to avoid potential redirect loops\n */\nconst DEFAULT_DETECT_LOCALE_ON_PREFETCH_NO_PREFIX = false;\n\nconst { internationalization, routing } = configuration ?? {};\nconst { locales, defaultLocale } = internationalization ?? {};\nconst { basePath, mode } = routing ?? {};\n\n// Note: cookie names are resolved inside LocaleStorage based on configuration\n\n// Derived flags from routing.mode\nconst effectiveMode = mode ?? DefaultValues.Routing.ROUTING_MODE;\nconst noPrefix =\n effectiveMode === 'no-prefix' || effectiveMode === 'search-params';\nconst prefixDefault = effectiveMode === 'prefix-all';\n\n/**\n * Detects if the request is a prefetch request from Next.js.\n *\n * Next.js prefetch requests can be identified by several headers:\n * - purpose: 'prefetch' (standard prefetch header)\n * - next-router-prefetch: '1' (Next.js router prefetch)\n * - next-url: present (Next.js internal navigation)\n *\n * During prefetch, we should ignore cookie-based locale detection\n * to prevent unwanted redirects when users are switching locales.\n *\n * @param request - The incoming Next.js request object.\n * @returns - True if the request is a prefetch request, false otherwise.\n */\nconst isPrefetchRequest = (request: NextRequest): boolean => {\n const purpose = request.headers.get('purpose');\n const nextRouterPrefetch = request.headers.get('next-router-prefetch');\n const nextUrl = request.headers.get('next-url');\n const xNextjsData = request.headers.get('x-nextjs-data');\n\n return (\n purpose === 'prefetch' ||\n nextRouterPrefetch === '1' ||\n !!nextUrl ||\n !!xNextjsData\n );\n};\n\n// Ensure locale is reflected in search params when routing mode is 'search-params'\nconst appendLocaleSearchIfNeeded = (\n search: string | undefined,\n locale: Locale\n): string | undefined => {\n if (effectiveMode !== 'search-params') return search;\n\n const params = new URLSearchParams(search ?? '');\n\n params.set('locale', locale);\n\n return `?${params.toString()}`;\n};\n\n/**\n * Proxy that handles the internationalization layer\n *\n * Usage:\n *\n * ```ts\n * // ./src/proxy.ts\n *\n * export { intlayerProxy as proxy } from '@intlayer/next/proxy';\n *\n * // applies this proxy only to files in the app directory\n * export const config = {\n * matcher: '/((?!api|static|.*\\\\..*|_next).*)',\n * };\n * ```\n *\n * Main proxy function for handling internationalization.\n *\n * @param request - The incoming Next.js request object.\n * @param event - The Next.js fetch event (optional).\n * @param response - The Next.js response object (optional).\n * @returns - The response to be returned to the client.\n */\nexport const intlayerProxy = (\n request: NextRequest,\n _event?: NextFetchEvent,\n _response?: NextResponse\n): NextResponse => {\n const pathname = request.nextUrl.pathname;\n\n const localLocale = getLocalLocale(request);\n\n if (\n noPrefix // If the application is configured not to use locale prefixes in URLs\n ) {\n return handleNoPrefix(request, localLocale, pathname);\n }\n\n const pathLocale = getPathLocale(pathname);\n\n return handlePrefix(request, localLocale, pathLocale, pathname);\n};\n\n/**\n * Retrieves the locale from the request cookies if available and valid.\n *\n * @param request - The incoming Next.js request object.\n * @returns - The locale found in the cookies, or undefined if not found or invalid.\n */\nconst getLocalLocale = (request: NextRequest): Locale | undefined =>\n getLocaleFromStorage({\n getCookie: (name: string) => request.cookies.get(name)?.value ?? null,\n getHeader: (name: string) => request.headers.get(name) ?? null,\n });\n\n/**\n * Handles the case where URLs do not have locale prefixes.\n */\nconst handleNoPrefix = (\n request: NextRequest,\n localLocale: Locale | undefined,\n pathname: string\n): NextResponse => {\n const pathLocale = getPathLocale(pathname);\n const locale = localLocale ?? defaultLocale;\n\n // If user typed /fr/about but mode is no-prefix,\n // we REDIRECT to /about (clean URL)\n if (pathLocale) {\n const pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || '/';\n const search = appendLocaleSearchIfNeeded(\n request.nextUrl.search,\n pathLocale\n );\n return redirectUrl(request, `${pathWithoutLocale}${search}`);\n }\n\n // Handle search-params redirect if locale is missing from URL\n if (effectiveMode === 'search-params') {\n const currentParam = request.nextUrl.searchParams.get('locale');\n if (currentParam !== locale) {\n const search = appendLocaleSearchIfNeeded(request.nextUrl.search, locale);\n return redirectUrl(request, `${pathname}${search}`);\n }\n }\n\n // INTERNAL REWRITE\n // We rewrite to the clean pathname.\n // If they have a [locale] folder, rewriteUrl (above) will handle adding it back.\n return rewriteUrl(request, pathname, locale);\n};\n\n/**\n * Extracts the locale from the URL pathname if present.\n *\n * @param pathname - The pathname from the request URL.\n * @returns - The locale found in the pathname, or undefined if not found.\n */\nconst getPathLocale = (pathname: string): Locale | undefined =>\n locales.find(\n (locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`\n );\n\n/**\n * Handles the case where URLs have locale prefixes.\n *\n * @param request - The incoming Next.js request object.\n * @param localLocale - The locale from the cookie.\n * @param pathLocale - The locale extracted from the pathname.\n * @param pathname - The pathname from the request URL.\n * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The response to be returned to the client.\n */\nconst handlePrefix = (\n request: NextRequest,\n localLocale: Locale | undefined,\n pathLocale: Locale | undefined,\n pathname: string\n): NextResponse => {\n if (\n !pathLocale // If the URL does not contain a locale prefix\n ) {\n const isPrefetch = isPrefetchRequest(request);\n\n if (isPrefetch && !DEFAULT_DETECT_LOCALE_ON_PREFETCH_NO_PREFIX) {\n return handleMissingPathLocale(request, defaultLocale, pathname);\n }\n\n return handleMissingPathLocale(request, localLocale, pathname);\n }\n\n // If the URL contains a locale prefix\n return handleExistingPathLocale(request, localLocale, pathLocale, pathname);\n};\n\n/**\n * Handles requests where the locale is missing from the URL pathname.\n *\n * @param request - The incoming Next.js request object.\n * @param localLocale - The locale from the cookie.\n * @param pathname - The pathname from the request URL.\n * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The response to be returned to the client.\n */\nconst handleMissingPathLocale = (\n request: NextRequest,\n localLocale: Locale | undefined,\n pathname: string\n): NextResponse => {\n let locale = (localLocale ??\n localeDetector?.(request) ??\n defaultLocale) as Locale;\n\n if (!locales.includes(locale)) {\n locale = defaultLocale;\n }\n\n // Determine if we should redirect or rewrite\n // If we are in 'prefix-all', we MUST redirect / -> /en/\n // If we are in 'prefix-no-default' and locale is NOT default, redirect / -> /fr/\n const shouldRedirect = prefixDefault || locale !== defaultLocale;\n\n if (shouldRedirect) {\n const newPath = constructPath(\n locale,\n pathname,\n basePath,\n appendLocaleSearchIfNeeded(request.nextUrl.search, locale)\n );\n return redirectUrl(request, newPath);\n }\n\n // --- THE FIX FOR / 404 ---\n // If we are at the root (or any path) and it's the default locale\n // (or we are in no-prefix mode), rewrite to the actual physical pathname.\n return rewriteUrl(request, pathname, locale);\n};\n/**\n * Handles requests where the locale exists in the URL pathname.\n *\n * @param request - The incoming Next.js request object.\n * @param localLocale - The locale from the cookie.\n * @param pathLocale - The locale extracted from the pathname.\n * @param pathname - The pathname from the request URL.\n * @returns - The response to be returned to the client.\n */\n/**\n * Handles requests where the locale exists in the URL pathname.\n */\nconst handleExistingPathLocale = (\n request: NextRequest,\n localLocale: Locale | undefined,\n pathLocale: Locale,\n pathname: string\n): NextResponse => {\n // 1. If cookie locale differs from path locale, redirect to the cookie locale\n // (Standard Intlayer behavior)\n if (localLocale && localLocale !== pathLocale) {\n const newPath = handleCookieLocaleMismatch(\n request,\n pathname,\n pathLocale,\n localLocale,\n basePath\n );\n return redirectUrl(request, newPath);\n }\n\n // 2. Handle the rewrite logic\n return handleDefaultLocaleRedirect(request, pathLocale, pathname);\n};\n\n/**\n * The key fix for 404s without [locale] folders\n */\nconst handleDefaultLocaleRedirect = (\n request: NextRequest,\n pathLocale: Locale,\n pathname: string\n): NextResponse => {\n // Determine if we need to remove the prefix for the default locale\n const isDefaultAndNoPrefix = !prefixDefault && pathLocale === defaultLocale;\n\n if (isDefaultAndNoPrefix) {\n const pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || '/';\n // ... (rest of your existing search param logic)\n return redirectUrl(request, `${basePath}${pathWithoutLocale}`);\n }\n\n // --- THE FIX ---\n // If the path contains a locale (like /fr) but we DON'T have a [locale] folder,\n // we rewrite the path to its \"clean\" version internally.\n\n // Strip the /fr prefix from the pathname for the internal rewrite\n const internalPathname = pathname.slice(`/${pathLocale}`.length) || '/';\n\n const searchWithLocale = appendLocaleSearchIfNeeded(\n request.nextUrl.search,\n pathLocale\n );\n\n const rewritePath = searchWithLocale\n ? `${internalPathname}${searchWithLocale}`\n : internalPathname;\n\n // Internal rewrite: Next.js sees / (or /path), user sees /fr/path\n return rewriteUrl(request, rewritePath, pathLocale);\n};\n\n/**\n * Handles the scenario where the locale in the cookie does not match the locale in the URL pathname.\n *\n * @param request - The incoming Next.js request object.\n * @param pathname - The pathname from the request URL.\n * @param pathLocale - The locale extracted from the pathname.\n * @param localLocale - The locale from the cookie.\n * @param basePath - The base path of the application.\n * @returns - The new URL path with the correct locale.\n */\nconst handleCookieLocaleMismatch = (\n request: NextRequest,\n pathname: string,\n pathLocale: Locale,\n localLocale: Locale,\n basePath: string\n): string => {\n // Replace the pathLocale in the pathname with the localLocale\n const newPath = pathname.replace(`/${pathLocale}`, `/${localLocale}`);\n\n return constructPath(\n localLocale,\n newPath,\n basePath,\n appendLocaleSearchIfNeeded(request.nextUrl.search, localLocale)\n );\n};\n\n/**\n * Constructs a new path by combining the locale, path, basePath, and search parameters.\n *\n * @param locale - The locale to include in the path.\n * @param path - The original path from the request.\n * @param basePath - The base path of the application.\n * @param [search] - The query string from the request URL (optional).\n * @returns - The constructed new path.\n */\nconst constructPath = (\n locale: Locale,\n path: string,\n basePath: string,\n search?: string\n): string => {\n const normalizedPath = path.startsWith('/') ? path : `/${path}`;\n let finalPath = normalizedPath;\n\n // If we are in a mode that doesn't want prefixes in the URL\n if (effectiveMode === 'no-prefix' || effectiveMode === 'search-params') {\n // Strip the locale from the path if it exists\n // This allows /fr/about to be treated as /about internally\n for (const loc of locales) {\n if (\n normalizedPath.startsWith(`/${loc}/`) ||\n normalizedPath === `/${loc}`\n ) {\n finalPath = normalizedPath.slice(`/${loc}`.length) || '/';\n break;\n }\n }\n } else {\n // Prefix modes: ensure the locale IS there\n finalPath = normalizedPath.startsWith(`/${locale}`)\n ? normalizedPath\n : `/${locale}${normalizedPath}`;\n }\n\n const cleanBasePath = basePath.replace(/\\/$/, '');\n const result = `${cleanBasePath}${finalPath}`;\n\n return search\n ? `${result}${search.startsWith('?') ? '' : '?'}${search}`\n : result;\n};\n\n/**\n * This handles the internal path Next.js sees.\n * To support optional [locale] folders, we need to decide if we\n * keep the locale prefix or strip it.\n */\nconst rewriteUrl = (\n request: NextRequest,\n newPath: string,\n locale: Locale\n): NextResponse => {\n const url = request.nextUrl.clone();\n const pathname = newPath.split('?')[0];\n\n // We determine if we should internally prefix based on the routing mode.\n // If user has [locale] folder, 'prefix-all' or 'prefix-no-default' usually works.\n // If user does NOT have [locale] folder, they usually use 'no-prefix'.\n\n const shouldInternallyPrefix =\n effectiveMode !== 'no-prefix' && effectiveMode !== 'search-params';\n\n if (shouldInternallyPrefix) {\n // If the path doesn't already have the locale, we add it for internal routing\n if (!getPathLocale(pathname)) {\n url.pathname = `/${locale}${pathname === '/' ? '' : pathname}`;\n } else {\n url.pathname = pathname;\n }\n } else {\n // For no-prefix or search-params, we ensure the internal path is CLEAN\n const pathLocale = getPathLocale(pathname);\n if (pathLocale) {\n url.pathname = pathname.slice(`/${pathLocale}`.length) || '/';\n } else {\n url.pathname = pathname;\n }\n }\n\n const response = NextResponse.rewrite(url);\n\n // CRITICAL: Set the locale in a header.\n // If the [locale] folder is missing, the Intlayer server-component\n // will read this header as a fallback to prevent \"Missing Tags\"\n setLocaleInStorage(locale, {\n setHeader: (name: string, value: string) => {\n response.headers.set(name, value);\n },\n });\n\n return response;\n};\n\n/**\n * Redirects the request to the new path.\n *\n * @param request - The incoming Next.js request object.\n * @param newPath - The new path to redirect to.\n * @returns - The redirect response.\n */\nconst redirectUrl = (request: NextRequest, newPath: string): NextResponse => {\n // Ensure we preserve the original search params if they were present and not explicitly included in newPath\n const search = request.nextUrl.search;\n const pathWithSearch =\n search && !newPath.includes('?') ? `${newPath}${search}` : newPath;\n\n return NextResponse.redirect(new URL(pathWithSearch, request.url));\n};\n"],"mappings":";;;;;;;;;AA0CA,MAAM,EAAE,sBAAsB,YAAYA,kCAAiB,EAAE;AAC7D,MAAM,EAAE,SAAS,kBAAkB,wBAAwB,EAAE;AAC7D,MAAM,EAAE,UAAU,SAAS,WAAW,EAAE;AAKxC,MAAM,gBAAgB,QAAQC,sCAAc,QAAQ;AACpD,MAAM,WACJ,kBAAkB,eAAe,kBAAkB;AACrD,MAAM,gBAAgB,kBAAkB;;;;;;;;;;;;;;;AAgBxC,MAAM,qBAAqB,YAAkC;CAC3D,MAAM,UAAU,QAAQ,QAAQ,IAAI,UAAU;CAC9C,MAAM,qBAAqB,QAAQ,QAAQ,IAAI,uBAAuB;CACtE,MAAM,UAAU,QAAQ,QAAQ,IAAI,WAAW;CAC/C,MAAM,cAAc,QAAQ,QAAQ,IAAI,gBAAgB;AAExD,QACE,YAAY,cACZ,uBAAuB,OACvB,CAAC,CAAC,WACF,CAAC,CAAC;;AAKN,MAAM,8BACJ,QACA,WACuB;AACvB,KAAI,kBAAkB,gBAAiB,QAAO;CAE9C,MAAM,SAAS,IAAI,gBAAgB,UAAU,GAAG;AAEhD,QAAO,IAAI,UAAU,OAAO;AAE5B,QAAO,IAAI,OAAO,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;AA0B9B,MAAa,iBACX,SACA,QACA,cACiB;CACjB,MAAM,W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,OAAO,GAAG,GACf;AAElC,QAAO,SACH,GAAG,SAAS,OAAO,WAAW,IAAI,GAAG,KAAK,MAAM,WAChD;;;;;;;AAQN,MAAM,cACJ,SACA,SACA,WACiB;CACjB,MAAM,MAAM,QAAQ,QAAQ,OAAO;CACnC,MAAM,WAAW,QAAQ,MAAM,IAAI,CAAC;AASpC,KAFE,kBAAkB,eAAe,kBAAkB,gBAInD,KAAI,CAAC,cAAc,SAAS,CAC1B,KAAI,WAAW,IAAI,SAAS,aAAa,MAAM,KAAK;KAEpD,KAAI,WAAW;MAEZ;EAEL,MAAM,aAAa,cAAc,SAAS;AAC1C,MAAI,WACF,KAAI,WAAW,SAAS,MAAM,IAAI,aAAa,OAAO,IAAI;MAE1D,KAAI,WAAW;;CAInB,MAAM,WAAWC,yBAAa,QAAQ,IAAI;AAK1C,wCAAmB,QAAQ,EACzB,YAAY,MAAc,UAAkB;AAC1C,WAAS,QAAQ,IAAI,MAAM,MAAM;IAEpC,CAAC;AAEF,QAAO;;;;;;;;;AAUT,MAAM,eAAe,SAAsB,YAAkC;CAE3E,MAAM,SAAS,QAAQ,QAAQ;CAC/B,MAAM,iBACJ,UAAU,CAAC,QAAQ,SAAS,IAAI,GAAG,GAAG,UAAU,WAAW;AAE7D,QAAOA,yBAAa,SAAS,IAAI,IAAI,gBAAgB,QAAQ,IAAI,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"getLocale.cjs","names":["configuration","Locales","negotiatorHeaders: Record<string, string>"],"sources":["../../../src/server/getLocale.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { getLocaleFromStorage, localeDetector } from '@intlayer/core';\nimport { type Locale, Locales } from '@intlayer/types';\nimport { cookies, headers } from 'next/headers.js';\n\n// Helper function to extract locale from headers/cookies\nexport const getLocale = async (): Promise<Locale> => {\n const defaultLocale =\n configuration?.internationalization?.defaultLocale ?? Locales.ENGLISH;\n\n // 1 - Try locale from header\n const headersList = await headers();\n const cookiesList = await cookies();\n\n const storedLocale = getLocaleFromStorage({\n getCookie: (name: string) => cookiesList.get(name)?.value ?? null,\n getHeader: (name: string) => headersList.get(name) ?? null,\n });\n\n if (storedLocale) return storedLocale as Locale;\n\n // 3 - Fallback to Accept-Language negotiation\n const negotiatorHeaders: Record<string, string> = {};\n headersList.forEach((value, key) => {\n negotiatorHeaders[key] = value;\n });\n\n const userFallbackLocale = localeDetector(negotiatorHeaders);\n if (userFallbackLocale) return userFallbackLocale as Locale;\n\n // 4 - Default locale\n return defaultLocale;\n};\n"],"mappings":";;;;;;;;AAMA,MAAa,YAAY,YAA6B;CACpD,MAAM,gBACJA,gCAAe,sBAAsB,iBAAiBC,wBAAQ;CAGhE,MAAM,cAAc,oCAAe;CACnC,MAAM,cAAc,oCAAe;CAEnC,MAAM,wDAAoC;EACxC,YAAY,SAAiB,YAAY,IAAI,KAAK,EAAE,SAAS;EAC7D,YAAY,SAAiB,YAAY,IAAI,KAAK,IAAI;EACvD,CAAC;AAEF,KAAI,aAAc,QAAO;CAGzB,MAAMC,oBAA4C,EAAE;AACpD,aAAY,SAAS,OAAO,QAAQ;AAClC,oBAAkB,OAAO;GACzB;CAEF,MAAM,wDAAoC,kBAAkB;AAC5D,KAAI,mBAAoB,QAAO;AAG/B,QAAO"}
1
+ {"version":3,"file":"getLocale.cjs","names":["configuration","Locales","negotiatorHeaders: Record<string, string>"],"sources":["../../../src/server/getLocale.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { getLocaleFromStorage, localeDetector } from '@intlayer/core';\nimport { type Locale, Locales } from '@intlayer/types';\nimport { cookies, headers } from 'next/headers.js';\n\n// Helper function to extract locale from headers/cookies\nexport const getLocale = async (): Promise<Locale> => {\n const defaultLocale =\n configuration?.internationalization?.defaultLocale ?? Locales.ENGLISH;\n\n // Try locale from header\n const headersList = await headers();\n const cookiesList = await cookies();\n\n const storedLocale = getLocaleFromStorage({\n getCookie: (name: string) => cookiesList.get(name)?.value ?? null,\n getHeader: (name: string) => headersList.get(name) ?? null,\n });\n\n if (storedLocale) return storedLocale as Locale;\n\n // Fallback to Accept-Language negotiation\n const negotiatorHeaders: Record<string, string> = {};\n headersList.forEach((value, key) => {\n negotiatorHeaders[key] = value;\n });\n\n const userFallbackLocale = localeDetector(negotiatorHeaders);\n if (userFallbackLocale) return userFallbackLocale as Locale;\n\n // Default locale\n return defaultLocale;\n};\n"],"mappings":";;;;;;;;AAMA,MAAa,YAAY,YAA6B;CACpD,MAAM,gBACJA,gCAAe,sBAAsB,iBAAiBC,wBAAQ;CAGhE,MAAM,cAAc,oCAAe;CACnC,MAAM,cAAc,oCAAe;CAEnC,MAAM,wDAAoC;EACxC,YAAY,SAAiB,YAAY,IAAI,KAAK,EAAE,SAAS;EAC7D,YAAY,SAAiB,YAAY,IAAI,KAAK,IAAI;EACvD,CAAC;AAEF,KAAI,aAAc,QAAO;CAGzB,MAAMC,oBAA4C,EAAE;AACpD,aAAY,SAAS,OAAO,QAAQ;AAClC,oBAAkB,OAAO;GACzB;CAEF,MAAM,wDAAoC,kBAAkB;AAC5D,KAAI,mBAAoB,QAAO;AAG/B,QAAO"}
@@ -79,28 +79,15 @@ const getLocalLocale = (request) => getLocaleFromStorage({
79
79
  });
80
80
  /**
81
81
  * Handles the case where URLs do not have locale prefixes.
82
- *
83
- * @param request - The incoming Next.js request object.
84
- * @param localLocale - The locale from the cookie.
85
- * @param pathname - The pathname from the request URL.
86
- * @returns - The rewritten response with the locale applied.
87
82
  */
88
83
  const handleNoPrefix = (request, localLocale, pathname) => {
89
84
  const pathLocale = getPathLocale(pathname);
90
- if (pathLocale) {
91
- const pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || "/";
92
- const search$1 = appendLocaleSearchIfNeeded(request.nextUrl.search, pathLocale);
93
- return redirectUrl(request, search$1 ? `${pathWithoutLocale}${search$1}` : `${pathWithoutLocale}${request.nextUrl.search ?? ""}`);
94
- }
95
85
  const locale = localLocale ?? defaultLocale;
86
+ if (pathLocale) return redirectUrl(request, `${pathname.slice(`/${pathLocale}`.length) || "/"}${appendLocaleSearchIfNeeded(request.nextUrl.search, pathLocale)}`);
96
87
  if (effectiveMode === "search-params") {
97
- if (new URLSearchParams(request.nextUrl.search).get("locale") === locale) return rewriteUrl(request, `${`/${locale}${pathname}`}${request.nextUrl.search ?? ""}`, locale);
98
- const search$1 = appendLocaleSearchIfNeeded(request.nextUrl.search, locale);
99
- return redirectUrl(request, search$1 ? `${pathname}${search$1}` : `${pathname}${request.nextUrl.search ?? ""}`);
88
+ if (request.nextUrl.searchParams.get("locale") !== locale) return redirectUrl(request, `${pathname}${appendLocaleSearchIfNeeded(request.nextUrl.search, locale)}`);
100
89
  }
101
- const internalPath = `/${locale}${pathname}`;
102
- const search = appendLocaleSearchIfNeeded(request.nextUrl.search, locale);
103
- return rewriteUrl(request, search ? `${internalPath}${search}` : `${internalPath}${request.nextUrl.search ?? ""}`, locale);
90
+ return rewriteUrl(request, pathname, locale);
104
91
  };
105
92
  /**
106
93
  * Extracts the locale from the URL pathname if present.
@@ -138,8 +125,8 @@ const handlePrefix = (request, localLocale, pathLocale, pathname) => {
138
125
  const handleMissingPathLocale = (request, localLocale, pathname) => {
139
126
  let locale = localLocale ?? localeDetector$1?.(request) ?? defaultLocale;
140
127
  if (!locales.includes(locale)) locale = defaultLocale;
141
- const newPath = constructPath(locale, pathname, basePath, appendLocaleSearchIfNeeded(request.nextUrl.search, locale));
142
- return prefixDefault || locale !== defaultLocale ? redirectUrl(request, newPath) : rewriteUrl(request, newPath, locale);
128
+ if (prefixDefault || locale !== defaultLocale) return redirectUrl(request, constructPath(locale, pathname, basePath, appendLocaleSearchIfNeeded(request.nextUrl.search, locale)));
129
+ return rewriteUrl(request, pathname, locale);
143
130
  };
144
131
  /**
145
132
  * Handles requests where the locale exists in the URL pathname.
@@ -150,11 +137,23 @@ const handleMissingPathLocale = (request, localLocale, pathname) => {
150
137
  * @param pathname - The pathname from the request URL.
151
138
  * @returns - The response to be returned to the client.
152
139
  */
140
+ /**
141
+ * Handles requests where the locale exists in the URL pathname.
142
+ */
153
143
  const handleExistingPathLocale = (request, localLocale, pathLocale, pathname) => {
154
144
  if (localLocale && localLocale !== pathLocale) return redirectUrl(request, handleCookieLocaleMismatch(request, pathname, pathLocale, localLocale, basePath));
155
145
  return handleDefaultLocaleRedirect(request, pathLocale, pathname);
156
146
  };
157
147
  /**
148
+ * The key fix for 404s without [locale] folders
149
+ */
150
+ const handleDefaultLocaleRedirect = (request, pathLocale, pathname) => {
151
+ if (!prefixDefault && pathLocale === defaultLocale) return redirectUrl(request, `${basePath}${pathname.slice(`/${pathLocale}`.length) || "/"}`);
152
+ const internalPathname = pathname.slice(`/${pathLocale}`.length) || "/";
153
+ const searchWithLocale = appendLocaleSearchIfNeeded(request.nextUrl.search, pathLocale);
154
+ return rewriteUrl(request, searchWithLocale ? `${internalPathname}${searchWithLocale}` : internalPathname, pathLocale);
155
+ };
156
+ /**
158
157
  * Handles the scenario where the locale in the cookie does not match the locale in the URL pathname.
159
158
  *
160
159
  * @param request - The incoming Next.js request object.
@@ -168,26 +167,6 @@ const handleCookieLocaleMismatch = (request, pathname, pathLocale, localLocale,
168
167
  return constructPath(localLocale, pathname.replace(`/${pathLocale}`, `/${localLocale}`), basePath$1, appendLocaleSearchIfNeeded(request.nextUrl.search, localLocale));
169
168
  };
170
169
  /**
171
- * Handles redirection when the default locale is used and prefixing is not required.
172
- *
173
- * @param request - The incoming Next.js request object.
174
- * @param pathLocale - The locale extracted from the pathname.
175
- * @param pathname - The pathname from the request URL.
176
- * @returns - The rewritten response without the locale prefix.
177
- */
178
- const handleDefaultLocaleRedirect = (request, pathLocale, pathname) => {
179
- if (!prefixDefault && pathLocale === defaultLocale) {
180
- let pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || "/";
181
- if (basePath.endsWith("/")) pathWithoutLocale = pathWithoutLocale.slice(1);
182
- const searchWithLocale$1 = appendLocaleSearchIfNeeded(request.nextUrl.search, pathLocale);
183
- if (searchWithLocale$1) pathWithoutLocale += searchWithLocale$1;
184
- else if (request.nextUrl.search) pathWithoutLocale += request.nextUrl.search;
185
- return redirectUrl(request, `${basePath}${pathWithoutLocale}`);
186
- }
187
- const searchWithLocale = appendLocaleSearchIfNeeded(request.nextUrl.search, pathLocale);
188
- return rewriteUrl(request, searchWithLocale ? `${pathname}${searchWithLocale}` : pathname, pathLocale);
189
- };
190
- /**
191
170
  * Constructs a new path by combining the locale, path, basePath, and search parameters.
192
171
  *
193
172
  * @param locale - The locale to include in the path.
@@ -197,31 +176,36 @@ const handleDefaultLocaleRedirect = (request, pathLocale, pathname) => {
197
176
  * @returns - The constructed new path.
198
177
  */
199
178
  const constructPath = (locale, path, basePath$1, search) => {
200
- const pathWithoutPrefix = path.startsWith(`/${locale}`) ? path.slice(`/${locale}`.length) || "/" : path;
201
- if (effectiveMode === "no-prefix") {
202
- if (search) return `${pathWithoutPrefix}?${search}`;
203
- return pathWithoutPrefix;
204
- }
205
- if (effectiveMode === "search-params") {
206
- if (search) return `${pathWithoutPrefix}?${search}`;
207
- return pathWithoutPrefix;
208
- }
209
- const pathWithLocalePrefix = path.startsWith(`/${locale}`) ? path : `${locale}${path}`;
210
- return `${basePath$1}${basePath$1.endsWith("/") ? "" : "/"}${pathWithLocalePrefix}`;
179
+ const normalizedPath = path.startsWith("/") ? path : `/${path}`;
180
+ let finalPath = normalizedPath;
181
+ if (effectiveMode === "no-prefix" || effectiveMode === "search-params") {
182
+ for (const loc of locales) if (normalizedPath.startsWith(`/${loc}/`) || normalizedPath === `/${loc}`) {
183
+ finalPath = normalizedPath.slice(`/${loc}`.length) || "/";
184
+ break;
185
+ }
186
+ } else finalPath = normalizedPath.startsWith(`/${locale}`) ? normalizedPath : `/${locale}${normalizedPath}`;
187
+ const result = `${basePath$1.replace(/\/$/, "")}${finalPath}`;
188
+ return search ? `${result}${search.startsWith("?") ? "" : "?"}${search}` : result;
211
189
  };
212
190
  /**
213
- * Rewrites the URL to the new path and sets the locale header.
214
- *
215
- * @param request - The incoming Next.js request object.
216
- * @param newPath - The new path to rewrite to.
217
- * @param locale - The locale to set in the response header.
218
- * @returns - The rewritten response.
191
+ * This handles the internal path Next.js sees.
192
+ * To support optional [locale] folders, we need to decide if we
193
+ * keep the locale prefix or strip it.
219
194
  */
220
195
  const rewriteUrl = (request, newPath, locale) => {
221
- const search = request.nextUrl.search;
222
- const pathWithSearch = search && !newPath.includes("?") ? `${newPath}${search}` : newPath;
223
- const response = NextResponse.rewrite(new URL(pathWithSearch, request.url));
224
- setLocaleInStorage(locale, { setHeader: (name, value) => response.headers.set(name, value) });
196
+ const url = request.nextUrl.clone();
197
+ const pathname = newPath.split("?")[0];
198
+ if (effectiveMode !== "no-prefix" && effectiveMode !== "search-params") if (!getPathLocale(pathname)) url.pathname = `/${locale}${pathname === "/" ? "" : pathname}`;
199
+ else url.pathname = pathname;
200
+ else {
201
+ const pathLocale = getPathLocale(pathname);
202
+ if (pathLocale) url.pathname = pathname.slice(`/${pathLocale}`.length) || "/";
203
+ else url.pathname = pathname;
204
+ }
205
+ const response = NextResponse.rewrite(url);
206
+ setLocaleInStorage(locale, { setHeader: (name, value) => {
207
+ response.headers.set(name, value);
208
+ } });
225
209
  return response;
226
210
  };
227
211
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"intlayerProxy.mjs","names":["search","localeDetector","basePath","searchWithLocale"],"sources":["../../../src/proxy/intlayerProxy.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { DefaultValues } from '@intlayer/config/client';\nimport { getLocaleFromStorage, setLocaleInStorage } from '@intlayer/core';\nimport type { Locale } from '@intlayer/types';\nimport {\n type NextFetchEvent,\n type NextRequest,\n NextResponse,\n} from 'next/server';\nimport { localeDetector } from './localeDetector';\n\n/**\n * Controls whether locale detection occurs during Next.js prefetch requests\n * - true: Detect and apply locale during prefetch\n * - false: Use default locale during prefetch (recommended)\n *\n * This setting affects how Next.js handles locale prefetching:\n *\n * Example scenario:\n * - User's browser language is 'fr'\n * - Current page is /fr/about\n * - Link prefetches /about\n *\n * With `detectLocaleOnPrefetchNoPrefix:true`\n * - Prefetch detects 'fr' locale from browser\n * - Redirects prefetch to /fr/about\n *\n * With `detectLocaleOnPrefetchNoPrefix:false` (default)\n * - Prefetch uses default locale\n * - Redirects prefetch to /en/about (assuming 'en' is default)\n *\n * When to use true:\n * - Your app uses non-localized internal links (e.g. <a href=\"/about\">)\n * - You want consistent locale detection behavior between regular and prefetch requests\n *\n * When to use false (default):\n * - Your app uses locale-prefixed links (e.g. <a href=\"/fr/about\">)\n * - You want to optimize prefetching performance\n * - You want to avoid potential redirect loops\n */\nconst DEFAULT_DETECT_LOCALE_ON_PREFETCH_NO_PREFIX = false;\n\nconst { internationalization, routing } = configuration ?? {};\nconst { locales, defaultLocale } = internationalization ?? {};\nconst { basePath, mode } = routing ?? {};\n// Note: cookie names are resolved inside LocaleStorage based on configuration\n\n// Derived flags from routing.mode\nconst effectiveMode = mode ?? DefaultValues.Routing.ROUTING_MODE;\nconst noPrefix =\n effectiveMode === 'no-prefix' || effectiveMode === 'search-params';\nconst prefixDefault = effectiveMode === 'prefix-all';\n\n/**\n * Detects if the request is a prefetch request from Next.js.\n *\n * Next.js prefetch requests can be identified by several headers:\n * - purpose: 'prefetch' (standard prefetch header)\n * - next-router-prefetch: '1' (Next.js router prefetch)\n * - next-url: present (Next.js internal navigation)\n *\n * During prefetch, we should ignore cookie-based locale detection\n * to prevent unwanted redirects when users are switching locales.\n *\n * @param request - The incoming Next.js request object.\n * @returns - True if the request is a prefetch request, false otherwise.\n */\nconst isPrefetchRequest = (request: NextRequest): boolean => {\n const purpose = request.headers.get('purpose');\n const nextRouterPrefetch = request.headers.get('next-router-prefetch');\n const nextUrl = request.headers.get('next-url');\n const xNextjsData = request.headers.get('x-nextjs-data');\n\n return (\n purpose === 'prefetch' ||\n nextRouterPrefetch === '1' ||\n !!nextUrl ||\n !!xNextjsData\n );\n};\n\n// Ensure locale is reflected in search params when routing mode is 'search-params'\nconst appendLocaleSearchIfNeeded = (\n search: string | undefined,\n locale: Locale\n): string | undefined => {\n if (effectiveMode !== 'search-params') return search;\n\n const params = new URLSearchParams(search ?? '');\n\n params.set('locale', locale);\n\n return `?${params.toString()}`;\n};\n\n/**\n * Proxy that handles the internationalization layer\n *\n * Usage:\n *\n * ```ts\n * // ./src/proxy.ts\n *\n * export { intlayerProxy as proxy } from '@intlayer/next/proxy';\n *\n * // applies this proxy only to files in the app directory\n * export const config = {\n * matcher: '/((?!api|static|.*\\\\..*|_next).*)',\n * };\n * ```\n *\n * Main proxy function for handling internationalization.\n *\n * @param request - The incoming Next.js request object.\n * @param event - The Next.js fetch event (optional).\n * @param response - The Next.js response object (optional).\n * @returns - The response to be returned to the client.\n */\nexport const intlayerProxy = (\n request: NextRequest,\n _event?: NextFetchEvent,\n _response?: NextResponse\n): NextResponse => {\n const pathname = request.nextUrl.pathname;\n\n const localLocale = getLocalLocale(request);\n\n if (\n noPrefix // If the application is configured not to use locale prefixes in URLs\n ) {\n return handleNoPrefix(request, localLocale, pathname);\n }\n\n const pathLocale = getPathLocale(pathname);\n\n return handlePrefix(request, localLocale, pathLocale, pathname);\n};\n\n/**\n * Retrieves the locale from the request cookies if available and valid.\n *\n * @param request - The incoming Next.js request object.\n * @returns - The locale found in the cookies, or undefined if not found or invalid.\n */\nconst getLocalLocale = (request: NextRequest): Locale | undefined =>\n getLocaleFromStorage({\n getCookie: (name: string) => request.cookies.get(name)?.value ?? null,\n getHeader: (name: string) => request.headers.get(name) ?? null,\n });\n\n/**\n * Handles the case where URLs do not have locale prefixes.\n *\n * @param request - The incoming Next.js request object.\n * @param localLocale - The locale from the cookie.\n * @param pathname - The pathname from the request URL.\n * @returns - The rewritten response with the locale applied.\n */\nconst handleNoPrefix = (\n request: NextRequest,\n localLocale: Locale | undefined,\n pathname: string\n): NextResponse => {\n // Check if pathname has a locale prefix (even though we're in no-prefix mode)\n const pathLocale = getPathLocale(pathname);\n\n // If a locale prefix is detected in the URL, redirect to remove it\n if (pathLocale) {\n // Strip the locale prefix from the pathname\n const pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || '/';\n\n // Build redirect URL without locale prefix but with search params if needed\n const search = appendLocaleSearchIfNeeded(\n request.nextUrl.search,\n pathLocale\n );\n const redirectPath = search\n ? `${pathWithoutLocale}${search}`\n : `${pathWithoutLocale}${request.nextUrl.search ?? ''}`;\n\n // Redirect to the path without locale prefix (URL changes in browser)\n return redirectUrl(request, redirectPath);\n }\n\n // If no locale prefix in URL, determine locale and rewrite internally\n const locale = localLocale ?? defaultLocale;\n\n // In search-params mode, we need to redirect to add the locale search param\n if (effectiveMode === 'search-params') {\n // Check if locale search param already exists and matches the detected locale\n const existingSearchParams = new URLSearchParams(request.nextUrl.search);\n const existingLocale = existingSearchParams.get('locale');\n\n // If the existing locale matches the detected locale, no redirect needed\n if (existingLocale === locale) {\n // For internal routing, we need to add the locale prefix so Next.js can match [locale] param\n const internalPath = `/${locale}${pathname}`;\n const rewritePath = `${internalPath}${request.nextUrl.search ?? ''}`;\n\n // Rewrite internally (URL stays the same in browser, but Next.js routes to /[locale]/path)\n return rewriteUrl(request, rewritePath, locale);\n }\n\n const search = appendLocaleSearchIfNeeded(request.nextUrl.search, locale);\n const redirectPath = search\n ? `${pathname}${search}`\n : `${pathname}${request.nextUrl.search ?? ''}`;\n\n // Redirect to add/update the locale search param (URL changes in browser)\n return redirectUrl(request, redirectPath);\n }\n\n // For internal routing, we need to add the locale prefix so Next.js can match [locale] param\n const internalPath = `/${locale}${pathname}`;\n\n // Add search params if needed\n const search = appendLocaleSearchIfNeeded(request.nextUrl.search, locale);\n const rewritePath = search\n ? `${internalPath}${search}`\n : `${internalPath}${request.nextUrl.search ?? ''}`;\n\n // Rewrite internally (URL stays the same in browser, but Next.js routes to /[locale]/path)\n return rewriteUrl(request, rewritePath, locale);\n};\n\n/**\n * Extracts the locale from the URL pathname if present.\n *\n * @param pathname - The pathname from the request URL.\n * @returns - The locale found in the pathname, or undefined if not found.\n */\nconst getPathLocale = (pathname: string): Locale | undefined =>\n locales.find(\n (locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`\n );\n\n/**\n * Handles the case where URLs have locale prefixes.\n *\n * @param request - The incoming Next.js request object.\n * @param localLocale - The locale from the cookie.\n * @param pathLocale - The locale extracted from the pathname.\n * @param pathname - The pathname from the request URL.\n * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The response to be returned to the client.\n */\nconst handlePrefix = (\n request: NextRequest,\n localLocale: Locale | undefined,\n pathLocale: Locale | undefined,\n pathname: string\n): NextResponse => {\n if (\n !pathLocale // If the URL does not contain a locale prefix\n ) {\n const isPrefetch = isPrefetchRequest(request);\n\n if (isPrefetch && !DEFAULT_DETECT_LOCALE_ON_PREFETCH_NO_PREFIX) {\n return handleMissingPathLocale(request, defaultLocale, pathname);\n }\n\n return handleMissingPathLocale(request, localLocale, pathname);\n }\n\n // If the URL contains a locale prefix\n return handleExistingPathLocale(request, localLocale, pathLocale, pathname);\n};\n\n/**\n * Handles requests where the locale is missing from the URL pathname.\n *\n * @param request - The incoming Next.js request object.\n * @param localLocale - The locale from the cookie.\n * @param pathname - The pathname from the request URL.\n * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The response to be returned to the client.\n */\nconst handleMissingPathLocale = (\n request: NextRequest,\n localLocale: Locale | undefined,\n pathname: string\n): NextResponse => {\n let locale = (localLocale ??\n localeDetector?.(request) ??\n defaultLocale) as Locale;\n if (!locales.includes(locale)) {\n locale = defaultLocale;\n }\n\n const newPath = constructPath(\n locale,\n pathname,\n basePath,\n appendLocaleSearchIfNeeded(request.nextUrl.search, locale)\n );\n\n return prefixDefault || locale !== defaultLocale\n ? redirectUrl(request, newPath)\n : rewriteUrl(request, newPath, locale);\n};\n\n/**\n * Handles requests where the locale exists in the URL pathname.\n *\n * @param request - The incoming Next.js request object.\n * @param localLocale - The locale from the cookie.\n * @param pathLocale - The locale extracted from the pathname.\n * @param pathname - The pathname from the request URL.\n * @returns - The response to be returned to the client.\n */\nconst handleExistingPathLocale = (\n request: NextRequest,\n localLocale: Locale | undefined,\n pathLocale: Locale,\n pathname: string\n): NextResponse => {\n if (\n // If the cookie locale is set and differs from the locale in the URL\n localLocale &&\n localLocale !== pathLocale\n ) {\n const newPath = handleCookieLocaleMismatch(\n request,\n pathname,\n pathLocale,\n localLocale,\n basePath\n );\n return redirectUrl(request, newPath);\n }\n\n // If the cookie locale matches the path locale, or cookie locale is not set, or serverSetCookie is 'always'\n return handleDefaultLocaleRedirect(request, pathLocale, pathname);\n};\n\n/**\n * Handles the scenario where the locale in the cookie does not match the locale in the URL pathname.\n *\n * @param request - The incoming Next.js request object.\n * @param pathname - The pathname from the request URL.\n * @param pathLocale - The locale extracted from the pathname.\n * @param localLocale - The locale from the cookie.\n * @param basePath - The base path of the application.\n * @returns - The new URL path with the correct locale.\n */\nconst handleCookieLocaleMismatch = (\n request: NextRequest,\n pathname: string,\n pathLocale: Locale,\n localLocale: Locale,\n basePath: string\n): string => {\n // Replace the pathLocale in the pathname with the localLocale\n const newPath = pathname.replace(`/${pathLocale}`, `/${localLocale}`);\n\n return constructPath(\n localLocale,\n newPath,\n basePath,\n appendLocaleSearchIfNeeded(request.nextUrl.search, localLocale)\n );\n};\n\n/**\n * Handles redirection when the default locale is used and prefixing is not required.\n *\n * @param request - The incoming Next.js request object.\n * @param pathLocale - The locale extracted from the pathname.\n * @param pathname - The pathname from the request URL.\n * @returns - The rewritten response without the locale prefix.\n */\nconst handleDefaultLocaleRedirect = (\n request: NextRequest,\n pathLocale: Locale,\n pathname: string\n): NextResponse => {\n if (\n // If default locale should not be prefixed and the pathLocale is the defaultLocale\n !prefixDefault &&\n pathLocale === defaultLocale\n ) {\n let pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || '/';\n\n const basePathTrailingSlash = basePath.endsWith('/');\n\n if (basePathTrailingSlash) {\n pathWithoutLocale = pathWithoutLocale.slice(1);\n }\n\n const searchWithLocale = appendLocaleSearchIfNeeded(\n request.nextUrl.search,\n pathLocale\n );\n if (searchWithLocale) {\n pathWithoutLocale += searchWithLocale;\n } else if (request.nextUrl.search) {\n pathWithoutLocale += request.nextUrl.search;\n }\n\n return redirectUrl(request, `${basePath}${pathWithoutLocale}`);\n }\n\n // If prefixing default locale is required or pathLocale is not the defaultLocale\n\n const searchWithLocale = appendLocaleSearchIfNeeded(\n request.nextUrl.search,\n pathLocale\n );\n const newPath = searchWithLocale\n ? `${pathname}${searchWithLocale}`\n : pathname;\n return rewriteUrl(request, newPath, pathLocale);\n};\n\n/**\n * Constructs a new path by combining the locale, path, basePath, and search parameters.\n *\n * @param locale - The locale to include in the path.\n * @param path - The original path from the request.\n * @param basePath - The base path of the application.\n * @param [search] - The query string from the request URL (optional).\n * @returns - The constructed new path.\n */\nconst constructPath = (\n locale: Locale,\n path: string,\n basePath: string,\n search?: string\n): string => {\n // In 'search-params' and 'no-prefix' modes, do not prefix the path with the locale\n // Also, strip any incoming locale prefix if present\n const pathWithoutPrefix = path.startsWith(`/${locale}`)\n ? path.slice(`/${locale}`.length) || '/'\n : path;\n\n if (effectiveMode === 'no-prefix') {\n if (search) {\n return `${pathWithoutPrefix}?${search}`;\n }\n\n return pathWithoutPrefix;\n }\n\n if (effectiveMode === 'search-params') {\n if (search) {\n return `${pathWithoutPrefix}?${search}`;\n }\n\n return pathWithoutPrefix;\n }\n\n const pathWithLocalePrefix = path.startsWith(`/${locale}`)\n ? path\n : `${locale}${path}`;\n\n const basePathTrailingSlash = basePath.endsWith('/');\n\n const newPath = `${basePath}${basePathTrailingSlash ? '' : '/'}${pathWithLocalePrefix}`;\n\n return newPath;\n};\n\n/**\n * Rewrites the URL to the new path and sets the locale header.\n *\n * @param request - The incoming Next.js request object.\n * @param newPath - The new path to rewrite to.\n * @param locale - The locale to set in the response header.\n * @returns - The rewritten response.\n */\nconst rewriteUrl = (\n request: NextRequest,\n newPath: string,\n locale: Locale\n): NextResponse => {\n // Ensure we preserve the original search params if they were present and not explicitly included in newPath\n const search = request.nextUrl.search;\n const pathWithSearch =\n search && !newPath.includes('?') ? `${newPath}${search}` : newPath;\n\n const response = NextResponse.rewrite(new URL(pathWithSearch, request.url));\n\n setLocaleInStorage(locale, {\n setHeader: (name: string, value: string) =>\n response.headers.set(name, value),\n });\n\n return response;\n};\n\n/**\n * Redirects the request to the new path.\n *\n * @param request - The incoming Next.js request object.\n * @param newPath - The new path to redirect to.\n * @returns - The redirect response.\n */\nconst redirectUrl = (request: NextRequest, newPath: string): NextResponse => {\n // Ensure we preserve the original search params if they were present and not explicitly included in newPath\n const search = request.nextUrl.search;\n const pathWithSearch =\n search && !newPath.includes('?') ? `${newPath}${search}` : newPath;\n\n return NextResponse.redirect(new URL(pathWithSearch, request.url));\n};\n"],"mappings":";;;;;;;AA0CA,MAAM,EAAE,sBAAsB,YAAY,iBAAiB,EAAE;AAC7D,MAAM,EAAE,SAAS,kBAAkB,wBAAwB,EAAE;AAC7D,MAAM,EAAE,UAAU,SAAS,WAAW,EAAE;AAIxC,MAAM,gBAAgB,QAAQ,cAAc,QAAQ;AACpD,MAAM,WACJ,kBAAkB,eAAe,kBAAkB;AACrD,MAAM,gBAAgB,kBAAkB;;;;;;;;;;;;;;;AAgBxC,MAAM,qBAAqB,YAAkC;CAC3D,MAAM,UAAU,QAAQ,QAAQ,IAAI,UAAU;CAC9C,MAAM,qBAAqB,QAAQ,QAAQ,IAAI,uBAAuB;CACtE,MAAM,UAAU,QAAQ,QAAQ,IAAI,WAAW;CAC/C,MAAM,cAAc,QAAQ,QAAQ,IAAI,gBAAgB;AAExD,QACE,YAAY,cACZ,uBAAuB,OACvB,CAAC,CAAC,WACF,CAAC,CAAC;;AAKN,MAAM,8BACJ,QACA,WACuB;AACvB,KAAI,kBAAkB,gBAAiB,QAAO;CAE9C,MAAM,SAAS,IAAI,gBAAgB,UAAU,GAAG;AAEhD,QAAO,IAAI,UAAU,OAAO;AAE5B,QAAO,IAAI,OAAO,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;AA0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1
+ {"version":3,"file":"intlayerProxy.mjs","names":["localeDetector","basePath"],"sources":["../../../src/proxy/intlayerProxy.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { DefaultValues } from '@intlayer/config/client';\nimport { getLocaleFromStorage, setLocaleInStorage } from '@intlayer/core';\nimport type { Locale } from '@intlayer/types';\nimport {\n type NextFetchEvent,\n type NextRequest,\n NextResponse,\n} from 'next/server';\nimport { localeDetector } from './localeDetector';\n\n/**\n * Controls whether locale detection occurs during Next.js prefetch requests\n * - true: Detect and apply locale during prefetch\n * - false: Use default locale during prefetch (recommended)\n *\n * This setting affects how Next.js handles locale prefetching:\n *\n * Example scenario:\n * - User's browser language is 'fr'\n * - Current page is /fr/about\n * - Link prefetches /about\n *\n * With `detectLocaleOnPrefetchNoPrefix:true`\n * - Prefetch detects 'fr' locale from browser\n * - Redirects prefetch to /fr/about\n *\n * With `detectLocaleOnPrefetchNoPrefix:false` (default)\n * - Prefetch uses default locale\n * - Redirects prefetch to /en/about (assuming 'en' is default)\n *\n * When to use true:\n * - Your app uses non-localized internal links (e.g. <a href=\"/about\">)\n * - You want consistent locale detection behavior between regular and prefetch requests\n *\n * When to use false (default):\n * - Your app uses locale-prefixed links (e.g. <a href=\"/fr/about\">)\n * - You want to optimize prefetching performance\n * - You want to avoid potential redirect loops\n */\nconst DEFAULT_DETECT_LOCALE_ON_PREFETCH_NO_PREFIX = false;\n\nconst { internationalization, routing } = configuration ?? {};\nconst { locales, defaultLocale } = internationalization ?? {};\nconst { basePath, mode } = routing ?? {};\n\n// Note: cookie names are resolved inside LocaleStorage based on configuration\n\n// Derived flags from routing.mode\nconst effectiveMode = mode ?? DefaultValues.Routing.ROUTING_MODE;\nconst noPrefix =\n effectiveMode === 'no-prefix' || effectiveMode === 'search-params';\nconst prefixDefault = effectiveMode === 'prefix-all';\n\n/**\n * Detects if the request is a prefetch request from Next.js.\n *\n * Next.js prefetch requests can be identified by several headers:\n * - purpose: 'prefetch' (standard prefetch header)\n * - next-router-prefetch: '1' (Next.js router prefetch)\n * - next-url: present (Next.js internal navigation)\n *\n * During prefetch, we should ignore cookie-based locale detection\n * to prevent unwanted redirects when users are switching locales.\n *\n * @param request - The incoming Next.js request object.\n * @returns - True if the request is a prefetch request, false otherwise.\n */\nconst isPrefetchRequest = (request: NextRequest): boolean => {\n const purpose = request.headers.get('purpose');\n const nextRouterPrefetch = request.headers.get('next-router-prefetch');\n const nextUrl = request.headers.get('next-url');\n const xNextjsData = request.headers.get('x-nextjs-data');\n\n return (\n purpose === 'prefetch' ||\n nextRouterPrefetch === '1' ||\n !!nextUrl ||\n !!xNextjsData\n );\n};\n\n// Ensure locale is reflected in search params when routing mode is 'search-params'\nconst appendLocaleSearchIfNeeded = (\n search: string | undefined,\n locale: Locale\n): string | undefined => {\n if (effectiveMode !== 'search-params') return search;\n\n const params = new URLSearchParams(search ?? '');\n\n params.set('locale', locale);\n\n return `?${params.toString()}`;\n};\n\n/**\n * Proxy that handles the internationalization layer\n *\n * Usage:\n *\n * ```ts\n * // ./src/proxy.ts\n *\n * export { intlayerProxy as proxy } from '@intlayer/next/proxy';\n *\n * // applies this proxy only to files in the app directory\n * export const config = {\n * matcher: '/((?!api|static|.*\\\\..*|_next).*)',\n * };\n * ```\n *\n * Main proxy function for handling internationalization.\n *\n * @param request - The incoming Next.js request object.\n * @param event - The Next.js fetch event (optional).\n * @param response - The Next.js response object (optional).\n * @returns - The response to be returned to the client.\n */\nexport const intlayerProxy = (\n request: NextRequest,\n _event?: NextFetchEvent,\n _response?: NextResponse\n): NextResponse => {\n const pathname = request.nextUrl.pathname;\n\n const localLocale = getLocalLocale(request);\n\n if (\n noPrefix // If the application is configured not to use locale prefixes in URLs\n ) {\n return handleNoPrefix(request, localLocale, pathname);\n }\n\n const pathLocale = getPathLocale(pathname);\n\n return handlePrefix(request, localLocale, pathLocale, pathname);\n};\n\n/**\n * Retrieves the locale from the request cookies if available and valid.\n *\n * @param request - The incoming Next.js request object.\n * @returns - The locale found in the cookies, or undefined if not found or invalid.\n */\nconst getLocalLocale = (request: NextRequest): Locale | undefined =>\n getLocaleFromStorage({\n getCookie: (name: string) => request.cookies.get(name)?.value ?? null,\n getHeader: (name: string) => request.headers.get(name) ?? null,\n });\n\n/**\n * Handles the case where URLs do not have locale prefixes.\n */\nconst handleNoPrefix = (\n request: NextRequest,\n localLocale: Locale | undefined,\n pathname: string\n): NextResponse => {\n const pathLocale = getPathLocale(pathname);\n const locale = localLocale ?? defaultLocale;\n\n // If user typed /fr/about but mode is no-prefix,\n // we REDIRECT to /about (clean URL)\n if (pathLocale) {\n const pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || '/';\n const search = appendLocaleSearchIfNeeded(\n request.nextUrl.search,\n pathLocale\n );\n return redirectUrl(request, `${pathWithoutLocale}${search}`);\n }\n\n // Handle search-params redirect if locale is missing from URL\n if (effectiveMode === 'search-params') {\n const currentParam = request.nextUrl.searchParams.get('locale');\n if (currentParam !== locale) {\n const search = appendLocaleSearchIfNeeded(request.nextUrl.search, locale);\n return redirectUrl(request, `${pathname}${search}`);\n }\n }\n\n // INTERNAL REWRITE\n // We rewrite to the clean pathname.\n // If they have a [locale] folder, rewriteUrl (above) will handle adding it back.\n return rewriteUrl(request, pathname, locale);\n};\n\n/**\n * Extracts the locale from the URL pathname if present.\n *\n * @param pathname - The pathname from the request URL.\n * @returns - The locale found in the pathname, or undefined if not found.\n */\nconst getPathLocale = (pathname: string): Locale | undefined =>\n locales.find(\n (locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`\n );\n\n/**\n * Handles the case where URLs have locale prefixes.\n *\n * @param request - The incoming Next.js request object.\n * @param localLocale - The locale from the cookie.\n * @param pathLocale - The locale extracted from the pathname.\n * @param pathname - The pathname from the request URL.\n * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The response to be returned to the client.\n */\nconst handlePrefix = (\n request: NextRequest,\n localLocale: Locale | undefined,\n pathLocale: Locale | undefined,\n pathname: string\n): NextResponse => {\n if (\n !pathLocale // If the URL does not contain a locale prefix\n ) {\n const isPrefetch = isPrefetchRequest(request);\n\n if (isPrefetch && !DEFAULT_DETECT_LOCALE_ON_PREFETCH_NO_PREFIX) {\n return handleMissingPathLocale(request, defaultLocale, pathname);\n }\n\n return handleMissingPathLocale(request, localLocale, pathname);\n }\n\n // If the URL contains a locale prefix\n return handleExistingPathLocale(request, localLocale, pathLocale, pathname);\n};\n\n/**\n * Handles requests where the locale is missing from the URL pathname.\n *\n * @param request - The incoming Next.js request object.\n * @param localLocale - The locale from the cookie.\n * @param pathname - The pathname from the request URL.\n * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The response to be returned to the client.\n */\nconst handleMissingPathLocale = (\n request: NextRequest,\n localLocale: Locale | undefined,\n pathname: string\n): NextResponse => {\n let locale = (localLocale ??\n localeDetector?.(request) ??\n defaultLocale) as Locale;\n\n if (!locales.includes(locale)) {\n locale = defaultLocale;\n }\n\n // Determine if we should redirect or rewrite\n // If we are in 'prefix-all', we MUST redirect / -> /en/\n // If we are in 'prefix-no-default' and locale is NOT default, redirect / -> /fr/\n const shouldRedirect = prefixDefault || locale !== defaultLocale;\n\n if (shouldRedirect) {\n const newPath = constructPath(\n locale,\n pathname,\n basePath,\n appendLocaleSearchIfNeeded(request.nextUrl.search, locale)\n );\n return redirectUrl(request, newPath);\n }\n\n // --- THE FIX FOR / 404 ---\n // If we are at the root (or any path) and it's the default locale\n // (or we are in no-prefix mode), rewrite to the actual physical pathname.\n return rewriteUrl(request, pathname, locale);\n};\n/**\n * Handles requests where the locale exists in the URL pathname.\n *\n * @param request - The incoming Next.js request object.\n * @param localLocale - The locale from the cookie.\n * @param pathLocale - The locale extracted from the pathname.\n * @param pathname - The pathname from the request URL.\n * @returns - The response to be returned to the client.\n */\n/**\n * Handles requests where the locale exists in the URL pathname.\n */\nconst handleExistingPathLocale = (\n request: NextRequest,\n localLocale: Locale | undefined,\n pathLocale: Locale,\n pathname: string\n): NextResponse => {\n // 1. If cookie locale differs from path locale, redirect to the cookie locale\n // (Standard Intlayer behavior)\n if (localLocale && localLocale !== pathLocale) {\n const newPath = handleCookieLocaleMismatch(\n request,\n pathname,\n pathLocale,\n localLocale,\n basePath\n );\n return redirectUrl(request, newPath);\n }\n\n // 2. Handle the rewrite logic\n return handleDefaultLocaleRedirect(request, pathLocale, pathname);\n};\n\n/**\n * The key fix for 404s without [locale] folders\n */\nconst handleDefaultLocaleRedirect = (\n request: NextRequest,\n pathLocale: Locale,\n pathname: string\n): NextResponse => {\n // Determine if we need to remove the prefix for the default locale\n const isDefaultAndNoPrefix = !prefixDefault && pathLocale === defaultLocale;\n\n if (isDefaultAndNoPrefix) {\n const pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || '/';\n // ... (rest of your existing search param logic)\n return redirectUrl(request, `${basePath}${pathWithoutLocale}`);\n }\n\n // --- THE FIX ---\n // If the path contains a locale (like /fr) but we DON'T have a [locale] folder,\n // we rewrite the path to its \"clean\" version internally.\n\n // Strip the /fr prefix from the pathname for the internal rewrite\n const internalPathname = pathname.slice(`/${pathLocale}`.length) || '/';\n\n const searchWithLocale = appendLocaleSearchIfNeeded(\n request.nextUrl.search,\n pathLocale\n );\n\n const rewritePath = searchWithLocale\n ? `${internalPathname}${searchWithLocale}`\n : internalPathname;\n\n // Internal rewrite: Next.js sees / (or /path), user sees /fr/path\n return rewriteUrl(request, rewritePath, pathLocale);\n};\n\n/**\n * Handles the scenario where the locale in the cookie does not match the locale in the URL pathname.\n *\n * @param request - The incoming Next.js request object.\n * @param pathname - The pathname from the request URL.\n * @param pathLocale - The locale extracted from the pathname.\n * @param localLocale - The locale from the cookie.\n * @param basePath - The base path of the application.\n * @returns - The new URL path with the correct locale.\n */\nconst handleCookieLocaleMismatch = (\n request: NextRequest,\n pathname: string,\n pathLocale: Locale,\n localLocale: Locale,\n basePath: string\n): string => {\n // Replace the pathLocale in the pathname with the localLocale\n const newPath = pathname.replace(`/${pathLocale}`, `/${localLocale}`);\n\n return constructPath(\n localLocale,\n newPath,\n basePath,\n appendLocaleSearchIfNeeded(request.nextUrl.search, localLocale)\n );\n};\n\n/**\n * Constructs a new path by combining the locale, path, basePath, and search parameters.\n *\n * @param locale - The locale to include in the path.\n * @param path - The original path from the request.\n * @param basePath - The base path of the application.\n * @param [search] - The query string from the request URL (optional).\n * @returns - The constructed new path.\n */\nconst constructPath = (\n locale: Locale,\n path: string,\n basePath: string,\n search?: string\n): string => {\n const normalizedPath = path.startsWith('/') ? path : `/${path}`;\n let finalPath = normalizedPath;\n\n // If we are in a mode that doesn't want prefixes in the URL\n if (effectiveMode === 'no-prefix' || effectiveMode === 'search-params') {\n // Strip the locale from the path if it exists\n // This allows /fr/about to be treated as /about internally\n for (const loc of locales) {\n if (\n normalizedPath.startsWith(`/${loc}/`) ||\n normalizedPath === `/${loc}`\n ) {\n finalPath = normalizedPath.slice(`/${loc}`.length) || '/';\n break;\n }\n }\n } else {\n // Prefix modes: ensure the locale IS there\n finalPath = normalizedPath.startsWith(`/${locale}`)\n ? normalizedPath\n : `/${locale}${normalizedPath}`;\n }\n\n const cleanBasePath = basePath.replace(/\\/$/, '');\n const result = `${cleanBasePath}${finalPath}`;\n\n return search\n ? `${result}${search.startsWith('?') ? '' : '?'}${search}`\n : result;\n};\n\n/**\n * This handles the internal path Next.js sees.\n * To support optional [locale] folders, we need to decide if we\n * keep the locale prefix or strip it.\n */\nconst rewriteUrl = (\n request: NextRequest,\n newPath: string,\n locale: Locale\n): NextResponse => {\n const url = request.nextUrl.clone();\n const pathname = newPath.split('?')[0];\n\n // We determine if we should internally prefix based on the routing mode.\n // If user has [locale] folder, 'prefix-all' or 'prefix-no-default' usually works.\n // If user does NOT have [locale] folder, they usually use 'no-prefix'.\n\n const shouldInternallyPrefix =\n effectiveMode !== 'no-prefix' && effectiveMode !== 'search-params';\n\n if (shouldInternallyPrefix) {\n // If the path doesn't already have the locale, we add it for internal routing\n if (!getPathLocale(pathname)) {\n url.pathname = `/${locale}${pathname === '/' ? '' : pathname}`;\n } else {\n url.pathname = pathname;\n }\n } else {\n // For no-prefix or search-params, we ensure the internal path is CLEAN\n const pathLocale = getPathLocale(pathname);\n if (pathLocale) {\n url.pathname = pathname.slice(`/${pathLocale}`.length) || '/';\n } else {\n url.pathname = pathname;\n }\n }\n\n const response = NextResponse.rewrite(url);\n\n // CRITICAL: Set the locale in a header.\n // If the [locale] folder is missing, the Intlayer server-component\n // will read this header as a fallback to prevent \"Missing Tags\"\n setLocaleInStorage(locale, {\n setHeader: (name: string, value: string) => {\n response.headers.set(name, value);\n },\n });\n\n return response;\n};\n\n/**\n * Redirects the request to the new path.\n *\n * @param request - The incoming Next.js request object.\n * @param newPath - The new path to redirect to.\n * @returns - The redirect response.\n */\nconst redirectUrl = (request: NextRequest, newPath: string): NextResponse => {\n // Ensure we preserve the original search params if they were present and not explicitly included in newPath\n const search = request.nextUrl.search;\n const pathWithSearch =\n search && !newPath.includes('?') ? `${newPath}${search}` : newPath;\n\n return NextResponse.redirect(new URL(pathWithSearch, request.url));\n};\n"],"mappings":";;;;;;;AA0CA,MAAM,EAAE,sBAAsB,YAAY,iBAAiB,EAAE;AAC7D,MAAM,EAAE,SAAS,kBAAkB,wBAAwB,EAAE;AAC7D,MAAM,EAAE,UAAU,SAAS,WAAW,EAAE;AAKxC,MAAM,gBAAgB,QAAQ,cAAc,QAAQ;AACpD,MAAM,WACJ,kBAAkB,eAAe,kBAAkB;AACrD,MAAM,gBAAgB,kBAAkB;;;;;;;;;;;;;;;AAgBxC,MAAM,qBAAqB,YAAkC;CAC3D,MAAM,UAAU,QAAQ,QAAQ,IAAI,UAAU;CAC9C,MAAM,qBAAqB,QAAQ,QAAQ,IAAI,uBAAuB;CACtE,MAAM,UAAU,QAAQ,QAAQ,IAAI,WAAW;CAC/C,MAAM,cAAc,QAAQ,QAAQ,IAAI,gBAAgB;AAExD,QACE,YAAY,cACZ,uBAAuB,OACvB,CAAC,CAAC,WACF,CAAC,CAAC;;AAKN,MAAM,8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iBAGrD;OAAK,MAAM,OAAO,QAChB,KACE,eAAe,WAAW,IAAI,IAAI,GAAG,IACrC,mBAAmB,IAAI,OACvB;AACA,eAAY,eAAe,MAAM,IAAI,MAAM,OAAO,IAAI;AACtD;;OAKJ,aAAY,eAAe,WAAW,IAAI,SAAS,GAC/C,iBACA,IAAI,SAAS;CAInB,MAAM,SAAS,GADOA,WAAS,QAAQ,OAAO,GAAG,GACf;AAElC,QAAO,SACH,GAAG,SAAS,OAAO,WAAW,IAAI,GAAG,KAAK,MAAM,WAChD;;;;;;;AAQN,MAAM,cACJ,SACA,SACA,WACiB;CACjB,MAAM,MAAM,QAAQ,QAAQ,OAAO;CACnC,MAAM,WAAW,QAAQ,MAAM,IAAI,CAAC;AASpC,KAFE,kBAAkB,eAAe,kBAAkB,gBAInD,KAAI,CAAC,cAAc,SAAS,CAC1B,KAAI,WAAW,IAAI,SAAS,aAAa,MAAM,KAAK;KAEpD,KAAI,WAAW;MAEZ;EAEL,MAAM,aAAa,cAAc,SAAS;AAC1C,MAAI,WACF,KAAI,WAAW,SAAS,MAAM,IAAI,aAAa,OAAO,IAAI;MAE1D,KAAI,WAAW;;CAInB,MAAM,WAAW,aAAa,QAAQ,IAAI;AAK1C,oBAAmB,QAAQ,EACzB,YAAY,MAAc,UAAkB;AAC1C,WAAS,QAAQ,IAAI,MAAM,MAAM;IAEpC,CAAC;AAEF,QAAO;;;;;;;;;AAUT,MAAM,eAAe,SAAsB,YAAkC;CAE3E,MAAM,SAAS,QAAQ,QAAQ;CAC/B,MAAM,iBACJ,UAAU,CAAC,QAAQ,SAAS,IAAI,GAAG,GAAG,UAAU,WAAW;AAE7D,QAAO,aAAa,SAAS,IAAI,IAAI,gBAAgB,QAAQ,IAAI,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"getLocale.mjs","names":["negotiatorHeaders: Record<string, string>"],"sources":["../../../src/server/getLocale.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { getLocaleFromStorage, localeDetector } from '@intlayer/core';\nimport { type Locale, Locales } from '@intlayer/types';\nimport { cookies, headers } from 'next/headers.js';\n\n// Helper function to extract locale from headers/cookies\nexport const getLocale = async (): Promise<Locale> => {\n const defaultLocale =\n configuration?.internationalization?.defaultLocale ?? Locales.ENGLISH;\n\n // 1 - Try locale from header\n const headersList = await headers();\n const cookiesList = await cookies();\n\n const storedLocale = getLocaleFromStorage({\n getCookie: (name: string) => cookiesList.get(name)?.value ?? null,\n getHeader: (name: string) => headersList.get(name) ?? null,\n });\n\n if (storedLocale) return storedLocale as Locale;\n\n // 3 - Fallback to Accept-Language negotiation\n const negotiatorHeaders: Record<string, string> = {};\n headersList.forEach((value, key) => {\n negotiatorHeaders[key] = value;\n });\n\n const userFallbackLocale = localeDetector(negotiatorHeaders);\n if (userFallbackLocale) return userFallbackLocale as Locale;\n\n // 4 - Default locale\n return defaultLocale;\n};\n"],"mappings":";;;;;;AAMA,MAAa,YAAY,YAA6B;CACpD,MAAM,gBACJ,eAAe,sBAAsB,iBAAiB,QAAQ;CAGhE,MAAM,cAAc,MAAM,SAAS;CACnC,MAAM,cAAc,MAAM,SAAS;CAEnC,MAAM,eAAe,qBAAqB;EACxC,YAAY,SAAiB,YAAY,IAAI,KAAK,EAAE,SAAS;EAC7D,YAAY,SAAiB,YAAY,IAAI,KAAK,IAAI;EACvD,CAAC;AAEF,KAAI,aAAc,QAAO;CAGzB,MAAMA,oBAA4C,EAAE;AACpD,aAAY,SAAS,OAAO,QAAQ;AAClC,oBAAkB,OAAO;GACzB;CAEF,MAAM,qBAAqB,eAAe,kBAAkB;AAC5D,KAAI,mBAAoB,QAAO;AAG/B,QAAO"}
1
+ {"version":3,"file":"getLocale.mjs","names":["negotiatorHeaders: Record<string, string>"],"sources":["../../../src/server/getLocale.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { getLocaleFromStorage, localeDetector } from '@intlayer/core';\nimport { type Locale, Locales } from '@intlayer/types';\nimport { cookies, headers } from 'next/headers.js';\n\n// Helper function to extract locale from headers/cookies\nexport const getLocale = async (): Promise<Locale> => {\n const defaultLocale =\n configuration?.internationalization?.defaultLocale ?? Locales.ENGLISH;\n\n // Try locale from header\n const headersList = await headers();\n const cookiesList = await cookies();\n\n const storedLocale = getLocaleFromStorage({\n getCookie: (name: string) => cookiesList.get(name)?.value ?? null,\n getHeader: (name: string) => headersList.get(name) ?? null,\n });\n\n if (storedLocale) return storedLocale as Locale;\n\n // Fallback to Accept-Language negotiation\n const negotiatorHeaders: Record<string, string> = {};\n headersList.forEach((value, key) => {\n negotiatorHeaders[key] = value;\n });\n\n const userFallbackLocale = localeDetector(negotiatorHeaders);\n if (userFallbackLocale) return userFallbackLocale as Locale;\n\n // Default locale\n return defaultLocale;\n};\n"],"mappings":";;;;;;AAMA,MAAa,YAAY,YAA6B;CACpD,MAAM,gBACJ,eAAe,sBAAsB,iBAAiB,QAAQ;CAGhE,MAAM,cAAc,MAAM,SAAS;CACnC,MAAM,cAAc,MAAM,SAAS;CAEnC,MAAM,eAAe,qBAAqB;EACxC,YAAY,SAAiB,YAAY,IAAI,KAAK,EAAE,SAAS;EAC7D,YAAY,SAAiB,YAAY,IAAI,KAAK,IAAI;EACvD,CAAC;AAEF,KAAI,aAAc,QAAO;CAGzB,MAAMA,oBAA4C,EAAE;AACpD,aAAY,SAAS,OAAO,QAAQ;AAClC,oBAAkB,OAAO;GACzB;CAEF,MAAM,qBAAqB,eAAe,kBAAkB;AAC5D,KAAI,mBAAoB,QAAO;AAG/B,QAAO"}
@@ -1,4 +1,4 @@
1
- import * as _intlayer_types3 from "@intlayer/types";
1
+ import * as _intlayer_types0 from "@intlayer/types";
2
2
  import { LocalesValues } from "@intlayer/types";
3
3
 
4
4
  //#region src/client/useLocale.d.ts
@@ -9,9 +9,9 @@ declare const useLocale: ({
9
9
  onChange
10
10
  }?: UseLocaleProps) => {
11
11
  pathWithoutLocale: string;
12
- locale: _intlayer_types3.DeclaredLocales;
13
- defaultLocale: _intlayer_types3.DeclaredLocales;
14
- availableLocales: _intlayer_types3.DeclaredLocales[];
12
+ locale: _intlayer_types0.DeclaredLocales;
13
+ defaultLocale: _intlayer_types0.DeclaredLocales;
14
+ availableLocales: _intlayer_types0.DeclaredLocales[];
15
15
  setLocale: (locale: LocalesValues) => void;
16
16
  };
17
17
  //#endregion
@@ -1,12 +1,12 @@
1
- import * as _intlayer_types0 from "@intlayer/types";
1
+ import * as _intlayer_types2 from "@intlayer/types";
2
2
  import { LocalesValues } from "@intlayer/types";
3
3
 
4
4
  //#region src/client/useLocalePageRouter.d.ts
5
5
  declare const useLocalePageRouter: () => {
6
6
  pathWithoutLocale: string;
7
- locale: _intlayer_types0.DeclaredLocales;
8
- defaultLocale: _intlayer_types0.DeclaredLocales;
9
- availableLocales: _intlayer_types0.DeclaredLocales[];
7
+ locale: _intlayer_types2.DeclaredLocales;
8
+ defaultLocale: _intlayer_types2.DeclaredLocales;
9
+ availableLocales: _intlayer_types2.DeclaredLocales[];
10
10
  setLocale: (locale: LocalesValues) => void;
11
11
  };
12
12
  //#endregion
@@ -1,8 +1,8 @@
1
- import * as _intlayer_types0 from "@intlayer/types";
1
+ import * as _intlayer_types5 from "@intlayer/types";
2
2
 
3
3
  //#region src/generateStaticParams.d.ts
4
4
  declare const generateStaticParams: () => {
5
- locale: _intlayer_types0.Locale;
5
+ locale: _intlayer_types5.Locale;
6
6
  }[];
7
7
  //#endregion
8
8
  export { generateStaticParams };
@@ -1 +1 @@
1
- {"version":3,"file":"intlayerProxy.d.ts","names":[],"sources":["../../../src/proxy/intlayerProxy.ts"],"sourcesContent":[],"mappings":";;;;;;AAsHA;;;;;;;;;;;;;;;;;;;;;cAAa,yBACF,sBACA,4BACG,iBACX"}
1
+ {"version":3,"file":"intlayerProxy.d.ts","names":[],"sources":["../../../src/proxy/intlayerProxy.ts"],"sourcesContent":[],"mappings":";;;;;;AAuHA;;;;;;;;;;;;;;;;;;;;;cAAa,yBACF,sBACA,4BACG,iBACX"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-intlayer",
3
- "version": "7.5.6",
3
+ "version": "7.5.12",
4
4
  "private": false,
5
5
  "description": "Simplify internationalization i18n in Next.js with context providers, hooks, locale detection, and multilingual content integration.",
6
6
  "keywords": [
@@ -118,15 +118,15 @@
118
118
  "typecheck": "tsc --noEmit --project tsconfig.types.json"
119
119
  },
120
120
  "dependencies": {
121
- "@intlayer/chokidar": "7.5.6",
122
- "@intlayer/config": "7.5.6",
123
- "@intlayer/core": "7.5.6",
124
- "@intlayer/dictionaries-entry": "7.5.6",
125
- "@intlayer/types": "7.5.6",
126
- "@intlayer/webpack": "7.5.6",
121
+ "@intlayer/chokidar": "7.5.12",
122
+ "@intlayer/config": "7.5.12",
123
+ "@intlayer/core": "7.5.12",
124
+ "@intlayer/dictionaries-entry": "7.5.12",
125
+ "@intlayer/types": "7.5.12",
126
+ "@intlayer/webpack": "7.5.12",
127
127
  "defu": "6.1.4",
128
128
  "node-loader": "2.1.0",
129
- "react-intlayer": "7.5.6"
129
+ "react-intlayer": "7.5.12"
130
130
  },
131
131
  "devDependencies": {
132
132
  "@types/node": "25.0.3",