next-intlayer 5.5.1 → 5.5.3

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.
@@ -56,9 +56,6 @@ const intlayerMiddleware = (request, _event, _response) => {
56
56
  const isPrefetch = isPrefetchRequest(request);
57
57
  const cookieLocale = isPrefetch ? void 0 : getCookieLocale(request);
58
58
  const basePathTrailingSlash = basePath.endsWith("/");
59
- if (isPrefetch) {
60
- console.log("Prefetch request detected - ignoring cookie locale");
61
- }
62
59
  if (noPrefix) {
63
60
  return handleNoPrefix(
64
61
  request,
@@ -68,14 +65,6 @@ const intlayerMiddleware = (request, _event, _response) => {
68
65
  );
69
66
  }
70
67
  const pathLocale = getPathLocale(pathname);
71
- console.log("--------------------------------");
72
- console.log("intlayerMiddleware", {
73
- pathname,
74
- cookieLocale,
75
- pathLocale,
76
- basePathTrailingSlash,
77
- isPrefetch
78
- });
79
68
  return handlePrefix(
80
69
  request,
81
70
  cookieLocale,
@@ -125,18 +114,8 @@ const handlePrefix = (request, cookieLocale, pathLocale, pathname, basePathTrail
125
114
  const handleMissingPathLocale = (request, cookieLocale, pathname, basePathTrailingSlash) => {
126
115
  let locale = cookieLocale ?? (0, import_localeDetector.localeDetector)?.(request) ?? defaultLocale;
127
116
  if (!locales.includes(locale)) {
128
- console.warn(
129
- "The localeDetector callback must return a locale included in your locales array. Reverting to using defaultLocale."
130
- );
131
117
  locale = defaultLocale;
132
118
  }
133
- console.log("handleMissingPathLocale", {
134
- locale,
135
- pathname,
136
- basePath,
137
- basePathTrailingSlash,
138
- search: request.nextUrl.search
139
- });
140
119
  const newPath = constructPath(
141
120
  locale,
142
121
  pathname,
@@ -151,11 +130,6 @@ const handleExistingPathLocale = (request, cookieLocale, pathLocale, pathname, b
151
130
  // If the cookie locale is set and differs from the locale in the URL, and server should not always set cookie
152
131
  cookieLocale && cookieLocale !== pathLocale && serverSetCookie !== "always"
153
132
  ) {
154
- console.log("handleExistingPathLocale", {
155
- pathname,
156
- pathLocale,
157
- cookieLocale
158
- });
159
133
  const newPath = handleCookieLocaleMismatch(
160
134
  request,
161
135
  pathname,
@@ -175,11 +149,6 @@ const handleExistingPathLocale = (request, cookieLocale, pathLocale, pathname, b
175
149
  };
176
150
  const handleCookieLocaleMismatch = (request, pathname, pathLocale, cookieLocale, basePath2, basePathTrailingSlash) => {
177
151
  const newPath = pathname.replace(`/${pathLocale}`, `/${cookieLocale}`);
178
- console.log("handleCookieLocaleMismatch", {
179
- newPath,
180
- pathLocale,
181
- cookieLocale
182
- });
183
152
  return constructPath(
184
153
  cookieLocale,
185
154
  newPath,
@@ -200,11 +169,6 @@ const handleDefaultLocaleRedirect = (request, pathLocale, pathname, basePathTrai
200
169
  if (request.nextUrl.search) {
201
170
  pathWithoutLocale += request.nextUrl.search;
202
171
  }
203
- console.log("handleDefaultLocaleRedirect", {
204
- pathWithoutLocale,
205
- basePath,
206
- pathLocale
207
- });
208
172
  return rewriteUrl(request, `${basePath}${pathWithoutLocale}`, pathLocale);
209
173
  }
210
174
  return rewriteUrl(request, pathname, pathLocale);
@@ -219,12 +183,10 @@ const constructPath = (locale, path, basePath2, basePathTrailingSlash, search) =
219
183
  };
220
184
  const rewriteUrl = (request, newPath, locale) => {
221
185
  const response = import_server.NextResponse.rewrite(new URL(newPath, request.url));
222
- console.log("rewriteUrl", { newPath, url: request.url });
223
186
  response.headers.set(headerName, locale);
224
187
  return response;
225
188
  };
226
189
  const redirectUrl = (request, newPath) => {
227
- console.log("redirectUrl", { newPath, url: request.url });
228
190
  return import_server.NextResponse.redirect(new URL(newPath, request.url));
229
191
  };
230
192
  // Annotate the CommonJS export names for ESM import in node:
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/middleware/intlayerMiddleware.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { type Locales } from '@intlayer/config/client';\n\nimport {\n type NextFetchEvent,\n type NextRequest,\n NextResponse,\n} from 'next/server';\nimport { localeDetector } from './localeDetector';\n\nconst { internationalization, middleware } = configuration;\nconst { locales, defaultLocale } = internationalization;\nconst {\n headerName,\n cookieName,\n prefixDefault,\n basePath,\n serverSetCookie,\n noPrefix,\n} = middleware;\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/**\n * Middleware that handles the internationalization layer\n *\n * Usage:\n *\n * ```ts\n * // ./src/middleware.ts\n *\n * export { intlayerMiddleware as middleware } from '@intlayer/next/middleware';\n *\n * // applies this middleware only to files in the app directory\n * export const config = {\n * matcher: '/((?!api|static|.*\\\\..*|_next).*)',\n * };\n * ```\n *\n * Main middleware 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 intlayerMiddleware = (\n request: NextRequest,\n _event?: NextFetchEvent,\n _response?: NextResponse\n): NextResponse => {\n const pathname = request.nextUrl.pathname;\n const isPrefetch = isPrefetchRequest(request);\n const cookieLocale = isPrefetch ? undefined : getCookieLocale(request);\n const basePathTrailingSlash = basePath.endsWith('/');\n\n if (isPrefetch) {\n console.log('Prefetch request detected - ignoring cookie locale');\n }\n\n if (\n noPrefix // If the application is configured not to use locale prefixes in URLs\n ) {\n return handleNoPrefix(\n request,\n cookieLocale,\n pathname,\n basePathTrailingSlash\n );\n }\n\n const pathLocale = getPathLocale(pathname);\n\n console.log('--------------------------------');\n console.log('intlayerMiddleware', {\n pathname,\n cookieLocale,\n pathLocale,\n basePathTrailingSlash,\n isPrefetch,\n });\n\n return handlePrefix(\n request,\n cookieLocale,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\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 getCookieLocale = (request: NextRequest): Locales | undefined => {\n if (!cookieName) return undefined;\n const cookieValue = request.cookies.get(cookieName)?.value as Locales;\n if (cookieValue && locales.includes(cookieValue)) {\n return cookieValue;\n }\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 cookieLocale - 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 rewritten response with the locale applied.\n */\nconst handleNoPrefix = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n const locale = cookieLocale ?? defaultLocale;\n const newPath = constructPath(\n locale,\n pathname,\n basePath,\n basePathTrailingSlash,\n request.nextUrl.search\n );\n return rewriteUrl(request, newPath, 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): Locales | 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 cookieLocale - 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 cookieLocale: Locales | undefined,\n pathLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (\n !pathLocale // If the URL does not contain a locale prefix\n ) {\n return handleMissingPathLocale(\n request,\n cookieLocale,\n pathname,\n basePathTrailingSlash\n );\n }\n\n // If the URL contains a locale prefix\n return handleExistingPathLocale(\n request,\n cookieLocale,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\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 cookieLocale - 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 cookieLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n let locale = (cookieLocale ??\n localeDetector?.(request) ??\n defaultLocale) as Locales;\n if (!locales.includes(locale)) {\n console.warn(\n 'The localeDetector callback must return a locale included in your locales array. Reverting to using defaultLocale.'\n );\n locale = defaultLocale;\n }\n\n console.log('handleMissingPathLocale', {\n locale,\n pathname,\n basePath,\n basePathTrailingSlash,\n search: request.nextUrl.search,\n });\n const newPath = constructPath(\n locale,\n pathname,\n basePath,\n basePathTrailingSlash,\n request.nextUrl.search\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 cookieLocale - 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 handleExistingPathLocale = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathLocale: Locales,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (\n // If the cookie locale is set and differs from the locale in the URL, and server should not always set cookie\n cookieLocale &&\n cookieLocale !== pathLocale &&\n serverSetCookie !== 'always'\n ) {\n console.log('handleExistingPathLocale', {\n pathname,\n pathLocale,\n cookieLocale,\n });\n\n const newPath = handleCookieLocaleMismatch(\n request,\n pathname,\n pathLocale,\n cookieLocale,\n basePath,\n basePathTrailingSlash\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(\n request,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\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 cookieLocale - The locale from the cookie.\n * @param basePath - The base path of the application.\n * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The new URL path with the correct locale.\n */\nconst handleCookieLocaleMismatch = (\n request: NextRequest,\n pathname: string,\n pathLocale: Locales,\n cookieLocale: Locales,\n basePath: string,\n basePathTrailingSlash: boolean\n): string => {\n // Replace the pathLocale in the pathname with the cookieLocale\n const newPath = pathname.replace(`/${pathLocale}`, `/${cookieLocale}`);\n\n console.log('handleCookieLocaleMismatch', {\n newPath,\n pathLocale,\n cookieLocale,\n });\n\n return constructPath(\n cookieLocale,\n newPath,\n basePath,\n basePathTrailingSlash,\n request.nextUrl.search\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 * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The rewritten response without the locale prefix.\n */\nconst handleDefaultLocaleRedirect = (\n request: NextRequest,\n pathLocale: Locales,\n pathname: string,\n basePathTrailingSlash: boolean\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 if (basePathTrailingSlash) {\n pathWithoutLocale = pathWithoutLocale.slice(1);\n }\n\n if (request.nextUrl.search) {\n pathWithoutLocale += request.nextUrl.search;\n }\n\n console.log('handleDefaultLocaleRedirect', {\n pathWithoutLocale,\n basePath,\n pathLocale,\n });\n\n return rewriteUrl(request, `${basePath}${pathWithoutLocale}`, pathLocale);\n }\n\n // If prefixing default locale is required or pathLocale is not the defaultLocale\n\n return rewriteUrl(request, pathname, 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 basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @param [search] - The query string from the request URL (optional).\n * @returns - The constructed new path.\n */\nconst constructPath = (\n locale: Locales,\n path: string,\n basePath: string,\n basePathTrailingSlash: boolean,\n search?: string\n): string => {\n let newPath = `${locale}${path}`;\n\n newPath = `${basePath}${basePathTrailingSlash ? '' : '/'}${newPath}`;\n if (search) {\n newPath += search;\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: Locales\n): NextResponse => {\n const response = NextResponse.rewrite(new URL(newPath, request.url));\n console.log('rewriteUrl', { newPath, url: request.url });\n response.headers.set(headerName, locale);\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 console.log('redirectUrl', { newPath, url: request.url });\n return NextResponse.redirect(new URL(newPath, request.url));\n};\n"],"mappings":";;;;;;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AAM,GAAG,IAAI;AAE9B,YAAU,GAAGA,SAAQ,GAAG,wBAAwB,KAAK,GAAG,GAAG,OAAO;AAClE,MAAI,QAAQ;AACV,eAAW;AAAA,EACb;AACA,SAAO;AACT;AAUA,MAAM,aAAa,CACjB,SACA,SACA,WACiB;AACjB,QAAM,WAAW,2BAAa,QAAQ,IAAI,IAAI,SAAS,QAAQ,GAAG,CAAC;AACnE,UAAQ,IAAI,cAAc,EAAE,SAAS,KAAK,QAAQ,IAAI,CAAC;AACvD,WAAS,QAAQ,IAAI,YAAY,MAAM;AACvC,SAAO;AACT;AASA,MAAM,cAAc,CAAC,SAAsB,YAAkC;AAC3E,UAAQ,IAAI,eAAe,EAAE,SAAS,KAAK,QAAQ,IAAI,CAAC;AACxD,SAAO,2BAAa,SAAS,IAAI,IAAI,SAAS,QAAQ,GAAG,CAAC;AAC5D;","names":["configuration","basePath"]}
1
+ {"version":3,"sources":["../../../src/middleware/intlayerMiddleware.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { type Locales } from '@intlayer/config/client';\n\nimport {\n type NextFetchEvent,\n type NextRequest,\n NextResponse,\n} from 'next/server';\nimport { localeDetector } from './localeDetector';\n\nconst { internationalization, middleware } = configuration;\nconst { locales, defaultLocale } = internationalization;\nconst {\n headerName,\n cookieName,\n prefixDefault,\n basePath,\n serverSetCookie,\n noPrefix,\n} = middleware;\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/**\n * Middleware that handles the internationalization layer\n *\n * Usage:\n *\n * ```ts\n * // ./src/middleware.ts\n *\n * export { intlayerMiddleware as middleware } from '@intlayer/next/middleware';\n *\n * // applies this middleware only to files in the app directory\n * export const config = {\n * matcher: '/((?!api|static|.*\\\\..*|_next).*)',\n * };\n * ```\n *\n * Main middleware 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 intlayerMiddleware = (\n request: NextRequest,\n _event?: NextFetchEvent,\n _response?: NextResponse\n): NextResponse => {\n const pathname = request.nextUrl.pathname;\n const isPrefetch = isPrefetchRequest(request);\n const cookieLocale = isPrefetch ? undefined : getCookieLocale(request);\n const basePathTrailingSlash = basePath.endsWith('/');\n\n if (\n noPrefix // If the application is configured not to use locale prefixes in URLs\n ) {\n return handleNoPrefix(\n request,\n cookieLocale,\n pathname,\n basePathTrailingSlash\n );\n }\n\n const pathLocale = getPathLocale(pathname);\n\n return handlePrefix(\n request,\n cookieLocale,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\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 getCookieLocale = (request: NextRequest): Locales | undefined => {\n if (!cookieName) return undefined;\n const cookieValue = request.cookies.get(cookieName)?.value as Locales;\n if (cookieValue && locales.includes(cookieValue)) {\n return cookieValue;\n }\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 cookieLocale - 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 rewritten response with the locale applied.\n */\nconst handleNoPrefix = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n const locale = cookieLocale ?? defaultLocale;\n const newPath = constructPath(\n locale,\n pathname,\n basePath,\n basePathTrailingSlash,\n request.nextUrl.search\n );\n return rewriteUrl(request, newPath, 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): Locales | 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 cookieLocale - 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 cookieLocale: Locales | undefined,\n pathLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (\n !pathLocale // If the URL does not contain a locale prefix\n ) {\n return handleMissingPathLocale(\n request,\n cookieLocale,\n pathname,\n basePathTrailingSlash\n );\n }\n\n // If the URL contains a locale prefix\n return handleExistingPathLocale(\n request,\n cookieLocale,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\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 cookieLocale - 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 cookieLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n let locale = (cookieLocale ??\n localeDetector?.(request) ??\n defaultLocale) as Locales;\n if (!locales.includes(locale)) {\n locale = defaultLocale;\n }\n\n const newPath = constructPath(\n locale,\n pathname,\n basePath,\n basePathTrailingSlash,\n request.nextUrl.search\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 cookieLocale - 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 handleExistingPathLocale = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathLocale: Locales,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (\n // If the cookie locale is set and differs from the locale in the URL, and server should not always set cookie\n cookieLocale &&\n cookieLocale !== pathLocale &&\n serverSetCookie !== 'always'\n ) {\n const newPath = handleCookieLocaleMismatch(\n request,\n pathname,\n pathLocale,\n cookieLocale,\n basePath,\n basePathTrailingSlash\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(\n request,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\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 cookieLocale - The locale from the cookie.\n * @param basePath - The base path of the application.\n * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The new URL path with the correct locale.\n */\nconst handleCookieLocaleMismatch = (\n request: NextRequest,\n pathname: string,\n pathLocale: Locales,\n cookieLocale: Locales,\n basePath: string,\n basePathTrailingSlash: boolean\n): string => {\n // Replace the pathLocale in the pathname with the cookieLocale\n const newPath = pathname.replace(`/${pathLocale}`, `/${cookieLocale}`);\n\n return constructPath(\n cookieLocale,\n newPath,\n basePath,\n basePathTrailingSlash,\n request.nextUrl.search\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 * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The rewritten response without the locale prefix.\n */\nconst handleDefaultLocaleRedirect = (\n request: NextRequest,\n pathLocale: Locales,\n pathname: string,\n basePathTrailingSlash: boolean\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 if (basePathTrailingSlash) {\n pathWithoutLocale = pathWithoutLocale.slice(1);\n }\n\n if (request.nextUrl.search) {\n pathWithoutLocale += request.nextUrl.search;\n }\n\n return rewriteUrl(request, `${basePath}${pathWithoutLocale}`, pathLocale);\n }\n\n // If prefixing default locale is required or pathLocale is not the defaultLocale\n\n return rewriteUrl(request, pathname, 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 basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @param [search] - The query string from the request URL (optional).\n * @returns - The constructed new path.\n */\nconst constructPath = (\n locale: Locales,\n path: string,\n basePath: string,\n basePathTrailingSlash: boolean,\n search?: string\n): string => {\n let newPath = `${locale}${path}`;\n\n newPath = `${basePath}${basePathTrailingSlash ? '' : '/'}${newPath}`;\n if (search) {\n newPath += search;\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: Locales\n): NextResponse => {\n const response = NextResponse.rewrite(new URL(newPath, request.url));\n response.headers.set(headerName, locale);\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 return NextResponse.redirect(new URL(newPath, request.url));\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA0B;AAG1B,oBAIO;AACP,4BAA+B;AAE/B,MAAM,EAAE,sBAAsB,WAAW,IAAI,aAAAA;AAC7C,MAAM,EAAE,SAAS,cAAc,IAAI;AACnC,MAAM;AAAA,EACJ;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,IAAI;AAgBJ,MAAM,oBAAoB,CAAC,YAAkC;AAC3D,QAAM,UAAU,QAAQ,QAAQ,IAAI,SAAS;AAC7C,QAAM,qBAAqB,QAAQ,QAAQ,IAAI,sBAAsB;AACrE,QAAM,UAAU,QAAQ,QAAQ,IAAI,UAAU;AAC9C,QAAM,cAAc,QAAQ,QAAQ,IAAI,eAAe;AAEvD,SACE,YAAY,cACZ,uBAAuB,OACvB,CAAC,CAAC,WACF,CAAC,CAAC;AAEN;AAyBO,MAAM,qBAAqB,CAChC,SACA,QACA,cACiB;AACjB,QAAM,WAAW,QAAQ,QAAQ;AACjC,QAAM,aAAa,kBAAkB,OAAO;AAC5C,QAAM,eAAe,aAAa,SAAY,gBAAgB,OAAO;AACrE,QAAM,wBAAwB,SAAS,SAAS,GAAG;AAEnD,MACE,UACA;AACA,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,QAAM,aAAa,cAAc,QAAQ;AAEzC,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAQA,MAAM,kBAAkB,CAAC,YAA8C;AACrE,MAA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AO,2BAAa,SAAS,IAAI,IAAI,SAAS,QAAQ,GAAG,CAAC;AAC5D;","names":["configuration","basePath"]}
@@ -31,17 +31,50 @@ __export(withIntlayer_exports, {
31
31
  withIntlayer: () => withIntlayer
32
32
  });
33
33
  module.exports = __toCommonJS(withIntlayer_exports);
34
+ var import_chokidar = require("@intlayer/chokidar");
34
35
  var import_config = require("@intlayer/config");
35
36
  var import_webpack = require("@intlayer/webpack");
36
37
  var import_deepmerge = __toESM(require("deepmerge"));
37
38
  var import_path = require("path");
38
39
  var import_compareVersion = require('./compareVersion.cjs');
39
40
  var import_getNextVertion = require('./getNextVertion.cjs');
41
+ (0, import_chokidar.cleanOutputDir)();
40
42
  const isTurbopackEnabled = process.env.npm_lifecycle_script?.includes("--turbo");
41
43
  const nextVersion = (0, import_getNextVertion.getNextVersion)();
42
44
  const isGteNext13 = (0, import_compareVersion.compareVersions)(nextVersion, "\u2265", "13.0.0");
43
45
  const isGteNext15 = (0, import_compareVersion.compareVersions)(nextVersion, "\u2265", "15.0.0");
44
46
  const isTurbopackStable = (0, import_compareVersion.compareVersions)(nextVersion, "\u2265", "15.3.0");
47
+ const getIsSwcPluginAvailable = () => {
48
+ try {
49
+ import_config.ESMxCJSRequire.resolve("@intlayer/swc");
50
+ return true;
51
+ } catch (e) {
52
+ return false;
53
+ }
54
+ };
55
+ const getPruneConfig = (intlayerConfig) => {
56
+ const { contentDir, dictionariesDir, mainDir } = intlayerConfig.content;
57
+ const isProduction = process.env.NODE_ENV === "production";
58
+ if (!isProduction) return {};
59
+ if (!isGteNext13) return {};
60
+ const isSwcPluginAvailable = getIsSwcPluginAvailable();
61
+ if (!isSwcPluginAvailable) return {};
62
+ const dictionariesPath = (0, import_path.join)(mainDir, "dictionaries.mjs");
63
+ return {
64
+ experimental: {
65
+ swcPlugins: [
66
+ [
67
+ import_config.ESMxCJSRequire.resolve("@intlayer/swc"),
68
+ {
69
+ dictionaries_dir: dictionariesDir,
70
+ dictionaries_entry_path: dictionariesPath,
71
+ content_dir: contentDir
72
+ }
73
+ ]
74
+ ]
75
+ }
76
+ };
77
+ };
45
78
  const withIntlayer = (nextConfig = {}) => {
46
79
  if (typeof nextConfig !== "object") {
47
80
  nextConfig = {};
@@ -130,7 +163,9 @@ const withIntlayer = (nextConfig = {}) => {
130
163
  return config;
131
164
  }
132
165
  };
133
- return (0, import_deepmerge.default)(nextConfig, newConfig);
166
+ const pruneConfig = getPruneConfig(intlayerConfig);
167
+ const intlayerNextConfig = (0, import_deepmerge.default)(pruneConfig, newConfig);
168
+ return (0, import_deepmerge.default)(nextConfig, intlayerNextConfig);
134
169
  };
135
170
  // Annotate the CommonJS export names for ESM import in node:
136
171
  0 && (module.exports = {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/server/withIntlayer.ts"],"sourcesContent":["import { getConfiguration } from '@intlayer/config';\nimport { IntlayerPlugin } from '@intlayer/webpack';\nimport merge from 'deepmerge';\nimport type { NextConfig } from 'next';\nimport type { NextJsWebpackConfig } from 'next/dist/server/config-shared';\nimport { join, relative, resolve } from 'path';\nimport { compareVersions } from './compareVersion';\nimport { getNextVersion } from './getNextVertion';\n\n// Extract from the start script if --turbo or --turbopack flag is used\nconst isTurbopackEnabled =\n process.env.npm_lifecycle_script?.includes('--turbo');\nconst nextVersion = getNextVersion();\nconst isGteNext13 = compareVersions(nextVersion, '≥', '13.0.0');\nconst isGteNext15 = compareVersions(nextVersion, '≥', '15.0.0');\nconst isTurbopackStable = compareVersions(nextVersion, '≥', '15.3.0');\n\ntype WebpackParams = Parameters<NextJsWebpackConfig>;\n\n/**\n * A Next.js plugin that adds the intlayer configuration to the webpack configuration\n * and sets the environment variablesi\n *\n * Usage:\n *\n * ```ts\n * // next.config.js\n * export default withIntlayer(nextConfig)\n * ```\n */\nexport const withIntlayer = <T extends Partial<NextConfig>>(\n nextConfig: T = {} as T\n): NextConfig & T => {\n if (typeof nextConfig !== 'object') {\n nextConfig = {} as T;\n }\n\n const intlayerConfig = getConfiguration();\n\n // Format all configuration values as environment variables\n const { mainDir, configDir, baseDir } = intlayerConfig.content;\n\n const dictionariesPath = join(mainDir, 'dictionaries.mjs');\n const relativeDictionariesPath = relative(baseDir, dictionariesPath);\n\n const unmergedDictionariesPath = join(mainDir, 'unmerged_dictionaries.mjs');\n const relativeUnmergedDictionariesPath = relative(\n baseDir,\n unmergedDictionariesPath\n );\n\n const configurationPath = join(configDir, 'configuration.json');\n const relativeConfigurationPath = relative(baseDir, configurationPath);\n\n // Only provide turbo-specific config if user explicitly sets it\n const turboConfig = {\n resolveAlias: {\n // \"prefix by './' to consider the path as relative to the project root. This is necessary for turbo to work correctly.\"\n '@intlayer/dictionaries-entry': `./${relativeDictionariesPath}`,\n '@intlayer/unmerged-dictionaries-entry': `./${relativeUnmergedDictionariesPath}`,\n '@intlayer/config/built': `./${relativeConfigurationPath}`,\n },\n rules: {\n '*.node': {\n as: '*.node',\n loaders: ['node-loader'],\n },\n },\n };\n\n const serverExternalPackages = [\n 'esbuild',\n 'module',\n 'fs',\n 'chokidar',\n 'fsevents',\n ];\n\n const newConfig: Partial<NextConfig> = {\n // Only add `serverExternalPackages` if Next.js is v15+\n ...(isGteNext15\n ? {\n // only for Next ≥15\n serverExternalPackages,\n }\n : {\n // only for Next ≥13 and <15.3\n ...(isGteNext13 && {\n serverComponentsExternalPackages: serverExternalPackages,\n }),\n }),\n\n ...(isTurbopackEnabled && {\n ...(isGteNext15 && isTurbopackStable\n ? {\n // only for Next ≥15.3\n turbopack: turboConfig,\n }\n : {\n experimental: {\n // only for Next ≥13 and <15.3\n turbo: turboConfig,\n },\n }),\n }),\n\n webpack: (config: WebpackParams['0'], options: WebpackParams[1]) => {\n // If the user has defined their own webpack config, call it\n if (typeof nextConfig.webpack === 'function') {\n config = nextConfig.webpack(config, options);\n }\n\n // Alias the dictionary entry for all builds\n config.resolve.alias = {\n ...config.resolve.alias,\n '@intlayer/dictionaries-entry': resolve(relativeDictionariesPath),\n '@intlayer/unmerged-dictionaries-entry': resolve(\n relativeUnmergedDictionariesPath\n ),\n '@intlayer/config/built': resolve(relativeConfigurationPath),\n };\n\n // Mark these modules as externals\n config.externals.push({\n esbuild: 'esbuild',\n module: 'module',\n fs: 'fs',\n chokidar: 'chokidar',\n fsevents: 'fsevents',\n });\n\n // Use `node-loader` for any `.node` files\n config.module.rules.push({\n test: /\\.node$/,\n loader: 'node-loader',\n });\n\n // Only add Intlayer plugin on server side (node runtime)\n const { isServer, nextRuntime } = options;\n\n if (isServer && nextRuntime === 'nodejs') {\n config.plugins.push(new IntlayerPlugin());\n }\n\n return config;\n },\n };\n\n // Merge the new config with the user's config\n return merge(nextConfig, newConfig) as NextConfig & T;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAiC;AACjC,qBAA+B;AAC/B,uBAAkB;AAGlB,kBAAwC;AACxC,4BAAgC;AAChC,4BAA+B;AAG/B,MAAM,qBACJ,QAAQ,IAAI,sBAAsB,SAAS,SAAS;AACtD,MAAM,kBAAc,sCAAe;AACnC,MAAM,kBAAc,uCAAgB,aAAa,UAAK,QAAQ;AAC9D,MAAM,kBAAc,uCAAgB,aAAa,UAAK,QAAQ;AAC9D,MAAM,wBAAoB,uCAAgB,aAAa,UAAK,QAAQ;AAe7D,MAAM,eAAe,CAC1B,aAAgB,CAAC,MACE;AACnB,MAAI,OAAO,eAAe,UAAU;AAClC,iBAAa,CAAC;AAAA,EAChB;AAEA,QAAM,qBAAiB,gCAAiB;AAGxC,QAAM,EAAE,SAAS,WAAW,QAAQ,IAAI,eAAe;AAEvD,QAAM,uBAAmB,kBAAK,SAAS,kBAAkB;AACzD,QAAM,+BAA2B,sBAAS,SAAS,gBAAgB;AAEnE,QAAM,+BAA2B,kBAAK,SAAS,2BAA2B;AAC1E,QAAM,uCAAmC;AAAA,IACvC;AAAA,IACA;AAAA,EACF;AAEA,QAAM,wBAAoB,kBAAK,WAAW,oBAAoB;AAC9D,QAAM,g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Q;AAAA,MACV,CAAC;AAGD,YAAM,EAAE,UAAU,YAAY,IAAI;AAElC,UAAI,YAAY,gBAAgB,UAAU;AACxC,eAAO,QAAQ,KAAK,IAAI,8BAAe,CAAC;AAAA,MAC1C;AAEA,aAAO;AAAA,IACT;AAAA,EACF;AAGA,aAAO,iBAAAA,SAAM,YAAY,SAAS;AACpC;","names":["merge"]}
1
+ {"version":3,"sources":["../../../src/server/withIntlayer.ts"],"sourcesContent":["import { cleanOutputDir } from '@intlayer/chokidar';\nimport {\n ESMxCJSRequire,\n getConfiguration,\n IntlayerConfig,\n} from '@intlayer/config';\nimport { IntlayerPlugin } from '@intlayer/webpack';\nimport merge from 'deepmerge';\nimport type { NextConfig } from 'next';\nimport type { NextJsWebpackConfig } from 'next/dist/server/config-shared';\nimport { join, relative, resolve } from 'path';\nimport { compareVersions } from './compareVersion';\nimport { getNextVersion } from './getNextVertion';\n\ncleanOutputDir();\n\n// Extract from the start script if --turbo or --turbopack flag is used\nconst isTurbopackEnabled =\n process.env.npm_lifecycle_script?.includes('--turbo');\nconst nextVersion = getNextVersion();\nconst isGteNext13 = compareVersions(nextVersion, '≥', '13.0.0');\nconst isGteNext15 = compareVersions(nextVersion, '≥', '15.0.0');\nconst isTurbopackStable = compareVersions(nextVersion, '≥', '15.3.0');\n\n// Check if SWC plugin is available\nconst getIsSwcPluginAvailable = () => {\n try {\n ESMxCJSRequire.resolve('@intlayer/swc');\n return true;\n } catch (e) {\n return false;\n }\n};\n\nconst getPruneConfig = (\n intlayerConfig: IntlayerConfig\n): Partial<NextConfig> => {\n const { contentDir, dictionariesDir, mainDir } = intlayerConfig.content;\n\n const isProduction = process.env.NODE_ENV === 'production';\n\n if (!isProduction) return {};\n\n if (!isGteNext13) return {};\n\n const isSwcPluginAvailable = getIsSwcPluginAvailable();\n if (!isSwcPluginAvailable) return {};\n\n const dictionariesPath = join(mainDir, 'dictionaries.mjs');\n\n return {\n experimental: {\n swcPlugins: [\n [\n ESMxCJSRequire.resolve('@intlayer/swc'),\n {\n dictionaries_dir: dictionariesDir,\n dictionaries_entry_path: dictionariesPath,\n content_dir: contentDir,\n } as any,\n ],\n ],\n },\n };\n};\n\ntype WebpackParams = Parameters<NextJsWebpackConfig>;\n\n/**\n * A Next.js plugin that adds the intlayer configuration to the webpack configuration\n * and sets the environment variablesi\n *\n * Usage:\n *\n * ```ts\n * // next.config.js\n * export default withIntlayer(nextConfig)\n * ```\n */\nexport const withIntlayer = <T extends Partial<NextConfig>>(\n nextConfig: T = {} as T\n): NextConfig & T => {\n if (typeof nextConfig !== 'object') {\n nextConfig = {} as T;\n }\n\n const intlayerConfig = getConfiguration();\n\n // Format all configuration values as environment variables\n const { mainDir, configDir, baseDir } = intlayerConfig.content;\n\n const dictionariesPath = join(mainDir, 'dictionaries.mjs');\n const relativeDictionariesPath = relative(baseDir, dictionariesPath);\n\n const unmergedDictionariesPath = join(mainDir, 'unmerged_dictionaries.mjs');\n const relativeUnmergedDictionariesPath = relative(\n baseDir,\n unmergedDictionariesPath\n );\n\n const configurationPath = join(configDir, 'configuration.json');\n const relativeConfigurationPath = relative(baseDir, configurationPath);\n\n // Only provide turbo-specific config if user explicitly sets it\n const turboConfig = {\n resolveAlias: {\n // \"prefix by './' to consider the path as relative to the project root. This is necessary for turbo to work correctly.\"\n '@intlayer/dictionaries-entry': `./${relativeDictionariesPath}`,\n '@intlayer/unmerged-dictionaries-entry': `./${relativeUnmergedDictionariesPath}`,\n '@intlayer/config/built': `./${relativeConfigurationPath}`,\n },\n rules: {\n '*.node': {\n as: '*.node',\n loaders: ['node-loader'],\n },\n },\n };\n\n const serverExternalPackages = [\n 'esbuild',\n 'module',\n 'fs',\n 'chokidar',\n 'fsevents',\n ];\n\n const newConfig: Partial<NextConfig> = {\n // Only add `serverExternalPackages` if Next.js is v15+\n ...(isGteNext15\n ? {\n // only for Next ≥15\n serverExternalPackages,\n }\n : {\n // only for Next ≥13 and <15.3\n ...(isGteNext13 && {\n serverComponentsExternalPackages: serverExternalPackages,\n }),\n }),\n\n ...(isTurbopackEnabled && {\n ...(isGteNext15 && isTurbopackStable\n ? {\n // only for Next ≥15.3\n turbopack: turboConfig,\n }\n : {\n experimental: {\n // only for Next ≥13 and <15.3\n turbo: turboConfig,\n },\n }),\n }),\n\n webpack: (config: WebpackParams['0'], options: WebpackParams[1]) => {\n // If the user has defined their own webpack config, call it\n if (typeof nextConfig.webpack === 'function') {\n config = nextConfig.webpack(config, options);\n }\n\n // Alias the dictionary entry for all builds\n config.resolve.alias = {\n ...config.resolve.alias,\n '@intlayer/dictionaries-entry': resolve(relativeDictionariesPath),\n '@intlayer/unmerged-dictionaries-entry': resolve(\n relativeUnmergedDictionariesPath\n ),\n '@intlayer/config/built': resolve(relativeConfigurationPath),\n };\n\n // Mark these modules as externals\n config.externals.push({\n esbuild: 'esbuild',\n module: 'module',\n fs: 'fs',\n chokidar: 'chokidar',\n fsevents: 'fsevents',\n });\n\n // Use `node-loader` for any `.node` files\n config.module.rules.push({\n test: /\\.node$/,\n loader: 'node-loader',\n });\n\n // Only add Intlayer plugin on server side (node runtime)\n const { isServer, nextRuntime } = options;\n\n if (isServer && nextRuntime === 'nodejs') {\n config.plugins.push(new IntlayerPlugin());\n }\n\n return config;\n },\n };\n\n const pruneConfig: Partial<NextConfig> = getPruneConfig(intlayerConfig);\n\n const intlayerNextConfig: Partial<NextConfig> = merge(pruneConfig, newConfig);\n\n // Merge the new config with the user's config\n return merge(nextConfig, intlayerNextConfig) as NextConfig & T;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAA+B;AAC/B,oBAIO;AACP,qBAA+B;AAC/B,uBAAkB;AAGlB,kBAAwC;AACxC,4BAAgC;AAChC,4BAA+B;AAAA,IAE/B,gCAAe;AAGf,MAAM,qBACJ,QAAQ,IAAI,sBAAsB,SAAS,SAAS;AACtD,MAAM,kBAAc,sCAAe;AACnC,MAAM,kBAAc,uCAAgB,aA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,UAAU,YAAY,IAAI;AAElC,UAAI,YAAY,gBAAgB,UAAU;AACxC,eAAO,QAAQ,KAAK,IAAI,8BAAe,CAAC;AAAA,MAC1C;AAEA,aAAO;AAAA,IACT;AAAA,EACF;AAEA,QAAM,cAAmC,eAAe,cAAc;AAEtE,QAAM,yBAA0C,iBAAAA,SAAM,aAAa,SAAS;AAG5E,aAAO,iBAAAA,SAAM,YAAY,kBAAkB;AAC7C;","names":["merge"]}
@@ -25,9 +25,6 @@ const intlayerMiddleware = (request, _event, _response) => {
25
25
  const isPrefetch = isPrefetchRequest(request);
26
26
  const cookieLocale = isPrefetch ? void 0 : getCookieLocale(request);
27
27
  const basePathTrailingSlash = basePath.endsWith("/");
28
- if (isPrefetch) {
29
- console.log("Prefetch request detected - ignoring cookie locale");
30
- }
31
28
  if (noPrefix) {
32
29
  return handleNoPrefix(
33
30
  request,
@@ -37,14 +34,6 @@ const intlayerMiddleware = (request, _event, _response) => {
37
34
  );
38
35
  }
39
36
  const pathLocale = getPathLocale(pathname);
40
- console.log("--------------------------------");
41
- console.log("intlayerMiddleware", {
42
- pathname,
43
- cookieLocale,
44
- pathLocale,
45
- basePathTrailingSlash,
46
- isPrefetch
47
- });
48
37
  return handlePrefix(
49
38
  request,
50
39
  cookieLocale,
@@ -94,18 +83,8 @@ const handlePrefix = (request, cookieLocale, pathLocale, pathname, basePathTrail
94
83
  const handleMissingPathLocale = (request, cookieLocale, pathname, basePathTrailingSlash) => {
95
84
  let locale = cookieLocale ?? localeDetector?.(request) ?? defaultLocale;
96
85
  if (!locales.includes(locale)) {
97
- console.warn(
98
- "The localeDetector callback must return a locale included in your locales array. Reverting to using defaultLocale."
99
- );
100
86
  locale = defaultLocale;
101
87
  }
102
- console.log("handleMissingPathLocale", {
103
- locale,
104
- pathname,
105
- basePath,
106
- basePathTrailingSlash,
107
- search: request.nextUrl.search
108
- });
109
88
  const newPath = constructPath(
110
89
  locale,
111
90
  pathname,
@@ -120,11 +99,6 @@ const handleExistingPathLocale = (request, cookieLocale, pathLocale, pathname, b
120
99
  // If the cookie locale is set and differs from the locale in the URL, and server should not always set cookie
121
100
  cookieLocale && cookieLocale !== pathLocale && serverSetCookie !== "always"
122
101
  ) {
123
- console.log("handleExistingPathLocale", {
124
- pathname,
125
- pathLocale,
126
- cookieLocale
127
- });
128
102
  const newPath = handleCookieLocaleMismatch(
129
103
  request,
130
104
  pathname,
@@ -144,11 +118,6 @@ const handleExistingPathLocale = (request, cookieLocale, pathLocale, pathname, b
144
118
  };
145
119
  const handleCookieLocaleMismatch = (request, pathname, pathLocale, cookieLocale, basePath2, basePathTrailingSlash) => {
146
120
  const newPath = pathname.replace(`/${pathLocale}`, `/${cookieLocale}`);
147
- console.log("handleCookieLocaleMismatch", {
148
- newPath,
149
- pathLocale,
150
- cookieLocale
151
- });
152
121
  return constructPath(
153
122
  cookieLocale,
154
123
  newPath,
@@ -169,11 +138,6 @@ const handleDefaultLocaleRedirect = (request, pathLocale, pathname, basePathTrai
169
138
  if (request.nextUrl.search) {
170
139
  pathWithoutLocale += request.nextUrl.search;
171
140
  }
172
- console.log("handleDefaultLocaleRedirect", {
173
- pathWithoutLocale,
174
- basePath,
175
- pathLocale
176
- });
177
141
  return rewriteUrl(request, `${basePath}${pathWithoutLocale}`, pathLocale);
178
142
  }
179
143
  return rewriteUrl(request, pathname, pathLocale);
@@ -188,12 +152,10 @@ const constructPath = (locale, path, basePath2, basePathTrailingSlash, search) =
188
152
  };
189
153
  const rewriteUrl = (request, newPath, locale) => {
190
154
  const response = NextResponse.rewrite(new URL(newPath, request.url));
191
- console.log("rewriteUrl", { newPath, url: request.url });
192
155
  response.headers.set(headerName, locale);
193
156
  return response;
194
157
  };
195
158
  const redirectUrl = (request, newPath) => {
196
- console.log("redirectUrl", { newPath, url: request.url });
197
159
  return NextResponse.redirect(new URL(newPath, request.url));
198
160
  };
199
161
  export {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/middleware/intlayerMiddleware.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { type Locales } from '@intlayer/config/client';\n\nimport {\n type NextFetchEvent,\n type NextRequest,\n NextResponse,\n} from 'next/server';\nimport { localeDetector } from './localeDetector';\n\nconst { internationalization, middleware } = configuration;\nconst { locales, defaultLocale } = internationalization;\nconst {\n headerName,\n cookieName,\n prefixDefault,\n basePath,\n serverSetCookie,\n noPrefix,\n} = middleware;\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/**\n * Middleware that handles the internationalization layer\n *\n * Usage:\n *\n * ```ts\n * // ./src/middleware.ts\n *\n * export { intlayerMiddleware as middleware } from '@intlayer/next/middleware';\n *\n * // applies this middleware only to files in the app directory\n * export const config = {\n * matcher: '/((?!api|static|.*\\\\..*|_next).*)',\n * };\n * ```\n *\n * Main middleware 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 intlayerMiddleware = (\n request: NextRequest,\n _event?: NextFetchEvent,\n _response?: NextResponse\n): NextResponse => {\n const pathname = request.nextUrl.pathname;\n const isPrefetch = isPrefetchRequest(request);\n const cookieLocale = isPrefetch ? undefined : getCookieLocale(request);\n const basePathTrailingSlash = basePath.endsWith('/');\n\n if (isPrefetch) {\n console.log('Prefetch request detected - ignoring cookie locale');\n }\n\n if (\n noPrefix // If the application is configured not to use locale prefixes in URLs\n ) {\n return handleNoPrefix(\n request,\n cookieLocale,\n pathname,\n basePathTrailingSlash\n );\n }\n\n const pathLocale = getPathLocale(pathname);\n\n console.log('--------------------------------');\n console.log('intlayerMiddleware', {\n pathname,\n cookieLocale,\n pathLocale,\n basePathTrailingSlash,\n isPrefetch,\n });\n\n return handlePrefix(\n request,\n cookieLocale,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\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 getCookieLocale = (request: NextRequest): Locales | undefined => {\n if (!cookieName) return undefined;\n const cookieValue = request.cookies.get(cookieName)?.value as Locales;\n if (cookieValue && locales.includes(cookieValue)) {\n return cookieValue;\n }\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 cookieLocale - 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 rewritten response with the locale applied.\n */\nconst handleNoPrefix = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n const locale = cookieLocale ?? defaultLocale;\n const newPath = constructPath(\n locale,\n pathname,\n basePath,\n basePathTrailingSlash,\n request.nextUrl.search\n );\n return rewriteUrl(request, newPath, 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): Locales | 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 cookieLocale - 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 cookieLocale: Locales | undefined,\n pathLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (\n !pathLocale // If the URL does not contain a locale prefix\n ) {\n return handleMissingPathLocale(\n request,\n cookieLocale,\n pathname,\n basePathTrailingSlash\n );\n }\n\n // If the URL contains a locale prefix\n return handleExistingPathLocale(\n request,\n cookieLocale,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\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 cookieLocale - 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 cookieLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n let locale = (cookieLocale ??\n localeDetector?.(request) ??\n defaultLocale) as Locales;\n if (!locales.includes(locale)) {\n console.warn(\n 'The localeDetector callback must return a locale included in your locales array. Reverting to using defaultLocale.'\n );\n locale = defaultLocale;\n }\n\n console.log('handleMissingPathLocale', {\n locale,\n pathname,\n basePath,\n basePathTrailingSlash,\n search: request.nextUrl.search,\n });\n const newPath = constructPath(\n locale,\n pathname,\n basePath,\n basePathTrailingSlash,\n request.nextUrl.search\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 cookieLocale - 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 handleExistingPathLocale = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathLocale: Locales,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (\n // If the cookie locale is set and differs from the locale in the URL, and server should not always set cookie\n cookieLocale &&\n cookieLocale !== pathLocale &&\n serverSetCookie !== 'always'\n ) {\n console.log('handleExistingPathLocale', {\n pathname,\n pathLocale,\n cookieLocale,\n });\n\n const newPath = handleCookieLocaleMismatch(\n request,\n pathname,\n pathLocale,\n cookieLocale,\n basePath,\n basePathTrailingSlash\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(\n request,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\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 cookieLocale - The locale from the cookie.\n * @param basePath - The base path of the application.\n * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The new URL path with the correct locale.\n */\nconst handleCookieLocaleMismatch = (\n request: NextRequest,\n pathname: string,\n pathLocale: Locales,\n cookieLocale: Locales,\n basePath: string,\n basePathTrailingSlash: boolean\n): string => {\n // Replace the pathLocale in the pathname with the cookieLocale\n const newPath = pathname.replace(`/${pathLocale}`, `/${cookieLocale}`);\n\n console.log('handleCookieLocaleMismatch', {\n newPath,\n pathLocale,\n cookieLocale,\n });\n\n return constructPath(\n cookieLocale,\n newPath,\n basePath,\n basePathTrailingSlash,\n request.nextUrl.search\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 * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The rewritten response without the locale prefix.\n */\nconst handleDefaultLocaleRedirect = (\n request: NextRequest,\n pathLocale: Locales,\n pathname: string,\n basePathTrailingSlash: boolean\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 if (basePathTrailingSlash) {\n pathWithoutLocale = pathWithoutLocale.slice(1);\n }\n\n if (request.nextUrl.search) {\n pathWithoutLocale += request.nextUrl.search;\n }\n\n console.log('handleDefaultLocaleRedirect', {\n pathWithoutLocale,\n basePath,\n pathLocale,\n });\n\n return rewriteUrl(request, `${basePath}${pathWithoutLocale}`, pathLocale);\n }\n\n // If prefixing default locale is required or pathLocale is not the defaultLocale\n\n return rewriteUrl(request, pathname, 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 basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @param [search] - The query string from the request URL (optional).\n * @returns - The constructed new path.\n */\nconst constructPath = (\n locale: Locales,\n path: string,\n basePath: string,\n basePathTrailingSlash: boolean,\n search?: string\n): string => {\n let newPath = `${locale}${path}`;\n\n newPath = `${basePath}${basePathTrailingSlash ? '' : '/'}${newPath}`;\n if (search) {\n newPath += search;\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: Locales\n): NextResponse => {\n const response = NextResponse.rewrite(new URL(newPath, request.url));\n console.log('rewriteUrl', { newPath, url: request.url });\n response.headers.set(headerName, locale);\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 console.log('redirectUrl', { newPath, url: request.url });\n return NextResponse.redirect(new URL(newPath, request.url));\n};\n"],"mappings":"AAAA,OAAO,mBAAmB;AAG1B;AAAA,EAGE;AAAA,OACK;AACP,SAAS,sBAAsB;AAE/B,MAAM,EAAE,sBAAsB,WAAW,IAAI;AAC7C,MAAM,EAAE,SAAS,cAAc,IAAI;AACnC,MAAM;AAAA,EACJ;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,IAAI;AAgBJ,MAAM,oBAAoB,CAAC,YAAkC;AAC3D,QAAM,UAAU,QAAQ,QAAQ,IAAI,SAAS;AAC7C,QAAM,qBAAqB,QAAQ,QAAQ,IAAI,sBAAsB;AACrE,QAAM,UAAU,QAAQ,QAAQ,IAAI,UAAU;AAC9C,QAAM,cAAc,QAAQ,QAAQ,IAAI,eAAe;AAEvD,SACE,YAAY,cACZ,uBAAuB,OACvB,CAAC,CAAC,WACF,CAAC,CAAC;AAEN;AAyBO,MAAM,qBAAqB,CAChC,SACA,QACA,cACiB;AACjB,QAAM,WAAW,QAAQ,QAAQ;AACjC,QAAM,aAAa,kBAAkB,OAAO;AAC5C,QAAM,eAAe,aAAa,SAAY,gBAAgB,OAAO;AACrE,QAAM,wBAAwB,SAAS,SAAS,GAAG;AAEnD,MAAI,YAAY;AACd,YAAQ,IAAI,oDAAoD;AAAA,EAClE;AAEA,MACE,UACA;AACA,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,QAAM,aAAa,cAAc,QAAQ;AAEzC,UAAQ,IAAI,kCAAkC;AAC9C,UAAQ,IAAI,sBAAsB;AAAA,IAChC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AAED,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF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AU;AACjD;AAYA,MAAM,gBAAgB,CACpB,QACA,MACAA,WACA,uBACA,WACW;AACX,MAAI,UAAU,GAAG,MAAM,GAAG,IAAI;AAE9B,YAAU,GAAGA,SAAQ,GAAG,wBAAwB,KAAK,GAAG,GAAG,OAAO;AAClE,MAAI,QAAQ;AACV,eAAW;AAAA,EACb;AACA,SAAO;AACT;AAUA,MAAM,aAAa,CACjB,SACA,SACA,WACiB;AACjB,QAAM,WAAW,aAAa,QAAQ,IAAI,IAAI,SAAS,QAAQ,GAAG,CAAC;AACnE,UAAQ,IAAI,cAAc,EAAE,SAAS,KAAK,QAAQ,IAAI,CAAC;AACvD,WAAS,QAAQ,IAAI,YAAY,MAAM;AACvC,SAAO;AACT;AASA,MAAM,cAAc,CAAC,SAAsB,YAAkC;AAC3E,UAAQ,IAAI,eAAe,EAAE,SAAS,KAAK,QAAQ,IAAI,CAAC;AACxD,SAAO,aAAa,SAAS,IAAI,IAAI,SAAS,QAAQ,GAAG,CAAC;AAC5D;","names":["basePath"]}
1
+ {"version":3,"sources":["../../../src/middleware/intlayerMiddleware.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { type Locales } from '@intlayer/config/client';\n\nimport {\n type NextFetchEvent,\n type NextRequest,\n NextResponse,\n} from 'next/server';\nimport { localeDetector } from './localeDetector';\n\nconst { internationalization, middleware } = configuration;\nconst { locales, defaultLocale } = internationalization;\nconst {\n headerName,\n cookieName,\n prefixDefault,\n basePath,\n serverSetCookie,\n noPrefix,\n} = middleware;\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/**\n * Middleware that handles the internationalization layer\n *\n * Usage:\n *\n * ```ts\n * // ./src/middleware.ts\n *\n * export { intlayerMiddleware as middleware } from '@intlayer/next/middleware';\n *\n * // applies this middleware only to files in the app directory\n * export const config = {\n * matcher: '/((?!api|static|.*\\\\..*|_next).*)',\n * };\n * ```\n *\n * Main middleware 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 intlayerMiddleware = (\n request: NextRequest,\n _event?: NextFetchEvent,\n _response?: NextResponse\n): NextResponse => {\n const pathname = request.nextUrl.pathname;\n const isPrefetch = isPrefetchRequest(request);\n const cookieLocale = isPrefetch ? undefined : getCookieLocale(request);\n const basePathTrailingSlash = basePath.endsWith('/');\n\n if (\n noPrefix // If the application is configured not to use locale prefixes in URLs\n ) {\n return handleNoPrefix(\n request,\n cookieLocale,\n pathname,\n basePathTrailingSlash\n );\n }\n\n const pathLocale = getPathLocale(pathname);\n\n return handlePrefix(\n request,\n cookieLocale,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\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 getCookieLocale = (request: NextRequest): Locales | undefined => {\n if (!cookieName) return undefined;\n const cookieValue = request.cookies.get(cookieName)?.value as Locales;\n if (cookieValue && locales.includes(cookieValue)) {\n return cookieValue;\n }\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 cookieLocale - 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 rewritten response with the locale applied.\n */\nconst handleNoPrefix = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n const locale = cookieLocale ?? defaultLocale;\n const newPath = constructPath(\n locale,\n pathname,\n basePath,\n basePathTrailingSlash,\n request.nextUrl.search\n );\n return rewriteUrl(request, newPath, 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): Locales | 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 cookieLocale - 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 cookieLocale: Locales | undefined,\n pathLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (\n !pathLocale // If the URL does not contain a locale prefix\n ) {\n return handleMissingPathLocale(\n request,\n cookieLocale,\n pathname,\n basePathTrailingSlash\n );\n }\n\n // If the URL contains a locale prefix\n return handleExistingPathLocale(\n request,\n cookieLocale,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\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 cookieLocale - 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 cookieLocale: Locales | undefined,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n let locale = (cookieLocale ??\n localeDetector?.(request) ??\n defaultLocale) as Locales;\n if (!locales.includes(locale)) {\n locale = defaultLocale;\n }\n\n const newPath = constructPath(\n locale,\n pathname,\n basePath,\n basePathTrailingSlash,\n request.nextUrl.search\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 cookieLocale - 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 handleExistingPathLocale = (\n request: NextRequest,\n cookieLocale: Locales | undefined,\n pathLocale: Locales,\n pathname: string,\n basePathTrailingSlash: boolean\n): NextResponse => {\n if (\n // If the cookie locale is set and differs from the locale in the URL, and server should not always set cookie\n cookieLocale &&\n cookieLocale !== pathLocale &&\n serverSetCookie !== 'always'\n ) {\n const newPath = handleCookieLocaleMismatch(\n request,\n pathname,\n pathLocale,\n cookieLocale,\n basePath,\n basePathTrailingSlash\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(\n request,\n pathLocale,\n pathname,\n basePathTrailingSlash\n );\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 cookieLocale - The locale from the cookie.\n * @param basePath - The base path of the application.\n * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The new URL path with the correct locale.\n */\nconst handleCookieLocaleMismatch = (\n request: NextRequest,\n pathname: string,\n pathLocale: Locales,\n cookieLocale: Locales,\n basePath: string,\n basePathTrailingSlash: boolean\n): string => {\n // Replace the pathLocale in the pathname with the cookieLocale\n const newPath = pathname.replace(`/${pathLocale}`, `/${cookieLocale}`);\n\n return constructPath(\n cookieLocale,\n newPath,\n basePath,\n basePathTrailingSlash,\n request.nextUrl.search\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 * @param basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @returns - The rewritten response without the locale prefix.\n */\nconst handleDefaultLocaleRedirect = (\n request: NextRequest,\n pathLocale: Locales,\n pathname: string,\n basePathTrailingSlash: boolean\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 if (basePathTrailingSlash) {\n pathWithoutLocale = pathWithoutLocale.slice(1);\n }\n\n if (request.nextUrl.search) {\n pathWithoutLocale += request.nextUrl.search;\n }\n\n return rewriteUrl(request, `${basePath}${pathWithoutLocale}`, pathLocale);\n }\n\n // If prefixing default locale is required or pathLocale is not the defaultLocale\n\n return rewriteUrl(request, pathname, 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 basePathTrailingSlash - Indicates if the basePath ends with a slash.\n * @param [search] - The query string from the request URL (optional).\n * @returns - The constructed new path.\n */\nconst constructPath = (\n locale: Locales,\n path: string,\n basePath: string,\n basePathTrailingSlash: boolean,\n search?: string\n): string => {\n let newPath = `${locale}${path}`;\n\n newPath = `${basePath}${basePathTrailingSlash ? '' : '/'}${newPath}`;\n if (search) {\n newPath += search;\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: Locales\n): NextResponse => {\n const response = NextResponse.rewrite(new URL(newPath, request.url));\n response.headers.set(headerName, locale);\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 return NextResponse.redirect(new URL(newPath, request.url));\n};\n"],"mappings":"AAAA,OAAO,mBAAmB;AAG1B;AAAA,EAGE;AAAA,OACK;AACP,SAAS,sBAAsB;AAE/B,MAAM,EAAE,sBAAsB,WAAW,IAAI;AAC7C,MAAM,EAAE,SAAS,cAAc,IAAI;AACnC,MAAM;AAAA,EACJ;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,IAAI;AAgBJ,MAAM,oBAAoB,CAAC,YAAkC;AAC3D,QAAM,UAAU,QAAQ,QAAQ,IAAI,SAAS;AAC7C,QAAM,qBAAqB,QAAQ,QAAQ,IAAI,sBAAsB;AACrE,QAAM,UAAU,QAAQ,QAAQ,IAAI,UAAU;AAC9C,QAAM,cAAc,QAAQ,QAAQ,IAAI,eAAe;AAEvD,SACE,YAAY,cACZ,uBAAuB,OACvB,CAAC,CAAC,WACF,CAAC,CAAC;AAEN;AAyBO,MAAM,qBAAqB,CAChC,SACA,QACA,cACiB;AACjB,QAAM,WAAW,QAAQ,QAAQ;AACjC,QAAM,aAAa,kBAAkB,OAAO;AAC5C,QAAM,eAAe,aAAa,SAAY,gBAAgB,OAAO;AACrE,QAAM,wBAAwB,SAAS,SAAS,GAAG;AAEnD,MACE,UACA;AACA,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,QAAM,aAAa,cAAc,QAAQ;AAEzC,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAQA,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cAAc,CAAC,SAAsB,YAAkC;AAC3E,SAAO,aAAa,SAAS,IAAI,IAAI,SAAS,QAAQ,GAAG,CAAC;AAC5D;","names":["basePath"]}
@@ -1,14 +1,50 @@
1
- import { getConfiguration } from "@intlayer/config";
1
+ import { cleanOutputDir } from "@intlayer/chokidar";
2
+ import {
3
+ ESMxCJSRequire,
4
+ getConfiguration
5
+ } from "@intlayer/config";
2
6
  import { IntlayerPlugin } from "@intlayer/webpack";
3
7
  import merge from "deepmerge";
4
8
  import { join, relative, resolve } from "path";
5
9
  import { compareVersions } from "./compareVersion.mjs";
6
10
  import { getNextVersion } from "./getNextVertion.mjs";
11
+ cleanOutputDir();
7
12
  const isTurbopackEnabled = process.env.npm_lifecycle_script?.includes("--turbo");
8
13
  const nextVersion = getNextVersion();
9
14
  const isGteNext13 = compareVersions(nextVersion, "\u2265", "13.0.0");
10
15
  const isGteNext15 = compareVersions(nextVersion, "\u2265", "15.0.0");
11
16
  const isTurbopackStable = compareVersions(nextVersion, "\u2265", "15.3.0");
17
+ const getIsSwcPluginAvailable = () => {
18
+ try {
19
+ ESMxCJSRequire.resolve("@intlayer/swc");
20
+ return true;
21
+ } catch (e) {
22
+ return false;
23
+ }
24
+ };
25
+ const getPruneConfig = (intlayerConfig) => {
26
+ const { contentDir, dictionariesDir, mainDir } = intlayerConfig.content;
27
+ const isProduction = process.env.NODE_ENV === "production";
28
+ if (!isProduction) return {};
29
+ if (!isGteNext13) return {};
30
+ const isSwcPluginAvailable = getIsSwcPluginAvailable();
31
+ if (!isSwcPluginAvailable) return {};
32
+ const dictionariesPath = join(mainDir, "dictionaries.mjs");
33
+ return {
34
+ experimental: {
35
+ swcPlugins: [
36
+ [
37
+ ESMxCJSRequire.resolve("@intlayer/swc"),
38
+ {
39
+ dictionaries_dir: dictionariesDir,
40
+ dictionaries_entry_path: dictionariesPath,
41
+ content_dir: contentDir
42
+ }
43
+ ]
44
+ ]
45
+ }
46
+ };
47
+ };
12
48
  const withIntlayer = (nextConfig = {}) => {
13
49
  if (typeof nextConfig !== "object") {
14
50
  nextConfig = {};
@@ -97,7 +133,9 @@ const withIntlayer = (nextConfig = {}) => {
97
133
  return config;
98
134
  }
99
135
  };
100
- return merge(nextConfig, newConfig);
136
+ const pruneConfig = getPruneConfig(intlayerConfig);
137
+ const intlayerNextConfig = merge(pruneConfig, newConfig);
138
+ return merge(nextConfig, intlayerNextConfig);
101
139
  };
102
140
  export {
103
141
  withIntlayer
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/server/withIntlayer.ts"],"sourcesContent":["import { getConfiguration } from '@intlayer/config';\nimport { IntlayerPlugin } from '@intlayer/webpack';\nimport merge from 'deepmerge';\nimport type { NextConfig } from 'next';\nimport type { NextJsWebpackConfig } from 'next/dist/server/config-shared';\nimport { join, relative, resolve } from 'path';\nimport { compareVersions } from './compareVersion';\nimport { getNextVersion } from './getNextVertion';\n\n// Extract from the start script if --turbo or --turbopack flag is used\nconst isTurbopackEnabled =\n process.env.npm_lifecycle_script?.includes('--turbo');\nconst nextVersion = getNextVersion();\nconst isGteNext13 = compareVersions(nextVersion, '≥', '13.0.0');\nconst isGteNext15 = compareVersions(nextVersion, '≥', '15.0.0');\nconst isTurbopackStable = compareVersions(nextVersion, '≥', '15.3.0');\n\ntype WebpackParams = Parameters<NextJsWebpackConfig>;\n\n/**\n * A Next.js plugin that adds the intlayer configuration to the webpack configuration\n * and sets the environment variablesi\n *\n * Usage:\n *\n * ```ts\n * // next.config.js\n * export default withIntlayer(nextConfig)\n * ```\n */\nexport const withIntlayer = <T extends Partial<NextConfig>>(\n nextConfig: T = {} as T\n): NextConfig & T => {\n if (typeof nextConfig !== 'object') {\n nextConfig = {} as T;\n }\n\n const intlayerConfig = getConfiguration();\n\n // Format all configuration values as environment variables\n const { mainDir, configDir, baseDir } = intlayerConfig.content;\n\n const dictionariesPath = join(mainDir, 'dictionaries.mjs');\n const relativeDictionariesPath = relative(baseDir, dictionariesPath);\n\n const unmergedDictionariesPath = join(mainDir, 'unmerged_dictionaries.mjs');\n const relativeUnmergedDictionariesPath = relative(\n baseDir,\n unmergedDictionariesPath\n );\n\n const configurationPath = join(configDir, 'configuration.json');\n const relativeConfigurationPath = relative(baseDir, configurationPath);\n\n // Only provide turbo-specific config if user explicitly sets it\n const turboConfig = {\n resolveAlias: {\n // \"prefix by './' to consider the path as relative to the project root. This is necessary for turbo to work correctly.\"\n '@intlayer/dictionaries-entry': `./${relativeDictionariesPath}`,\n '@intlayer/unmerged-dictionaries-entry': `./${relativeUnmergedDictionariesPath}`,\n '@intlayer/config/built': `./${relativeConfigurationPath}`,\n },\n rules: {\n '*.node': {\n as: '*.node',\n loaders: ['node-loader'],\n },\n },\n };\n\n const serverExternalPackages = [\n 'esbuild',\n 'module',\n 'fs',\n 'chokidar',\n 'fsevents',\n ];\n\n const newConfig: Partial<NextConfig> = {\n // Only add `serverExternalPackages` if Next.js is v15+\n ...(isGteNext15\n ? {\n // only for Next ≥15\n serverExternalPackages,\n }\n : {\n // only for Next ≥13 and <15.3\n ...(isGteNext13 && {\n serverComponentsExternalPackages: serverExternalPackages,\n }),\n }),\n\n ...(isTurbopackEnabled && {\n ...(isGteNext15 && isTurbopackStable\n ? {\n // only for Next ≥15.3\n turbopack: turboConfig,\n }\n : {\n experimental: {\n // only for Next ≥13 and <15.3\n turbo: turboConfig,\n },\n }),\n }),\n\n webpack: (config: WebpackParams['0'], options: WebpackParams[1]) => {\n // If the user has defined their own webpack config, call it\n if (typeof nextConfig.webpack === 'function') {\n config = nextConfig.webpack(config, options);\n }\n\n // Alias the dictionary entry for all builds\n config.resolve.alias = {\n ...config.resolve.alias,\n '@intlayer/dictionaries-entry': resolve(relativeDictionariesPath),\n '@intlayer/unmerged-dictionaries-entry': resolve(\n relativeUnmergedDictionariesPath\n ),\n '@intlayer/config/built': resolve(relativeConfigurationPath),\n };\n\n // Mark these modules as externals\n config.externals.push({\n esbuild: 'esbuild',\n module: 'module',\n fs: 'fs',\n chokidar: 'chokidar',\n fsevents: 'fsevents',\n });\n\n // Use `node-loader` for any `.node` files\n config.module.rules.push({\n test: /\\.node$/,\n loader: 'node-loader',\n });\n\n // Only add Intlayer plugin on server side (node runtime)\n const { isServer, nextRuntime } = options;\n\n if (isServer && nextRuntime === 'nodejs') {\n config.plugins.push(new IntlayerPlugin());\n }\n\n return config;\n },\n };\n\n // Merge the new config with the user's config\n return merge(nextConfig, newConfig) as NextConfig & T;\n};\n"],"mappings":"AAAA,SAAS,wBAAwB;AACjC,SAAS,sBAAsB;AAC/B,OAAO,WAAW;AAGlB,SAAS,MAAM,UAAU,eAAe;AACxC,SAAS,uBAAuB;AAChC,SAAS,sBAAsB;AAG/B,MAAM,qBACJ,QAAQ,IAAI,sBAAsB,SAAS,SAAS;AACtD,MAAM,cAAc,eAAe;AACnC,MAAM,cAAc,gBAAgB,aAAa,UAAK,QAAQ;AAC9D,MAAM,cAAc,gBAAgB,aAAa,UAAK,QAAQ;AAC9D,MAAM,oBAAoB,gBAAgB,aAAa,UAAK,QAAQ;AAe7D,MAAM,eAAe,CAC1B,aAAgB,CAAC,MACE;AACnB,MAAI,OAAO,eAAe,UAAU;AAClC,iBAAa,CAAC;AAAA,EAChB;AAEA,QAAM,iBAAiB,iBAAiB;AAGxC,QAAM,EAAE,SAAS,WAAW,QAAQ,IAAI,eAAe;AAEvD,QAAM,mBAAmB,KAAK,SAAS,kBAAkB;AACzD,QAAM,2BAA2B,SAAS,SAAS,gBAAgB;AAEnE,QAAM,2BAA2B,KAAK,SAAS,2BAA2B;AAC1E,QAAM,mCAAmC;AAAA,IACvC;AAAA,IACA;AAAA,EACF;AAEA,QAAM,oBAAoB,KAAK,WAAW,oBAAoB;AAC9D,QAAM,4BAA4B,SAAS,SAAS,iBAAiB;AAGrE,QAAM,cAAc;AAAA,IAClB,cAAc;AAAA;AAAA,MAEZ,gCAAgC,KAAK,wBAAwB;AAAA,MAC7D,yCAAyC,KAAK,gCAAgC;AAAA,MAC9E,0BAA0B,KAAK,yBAAyB;AA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SAAO,MAAM,YAAY,SAAS;AACpC;","names":[]}
1
+ {"version":3,"sources":["../../../src/server/withIntlayer.ts"],"sourcesContent":["import { cleanOutputDir } from '@intlayer/chokidar';\nimport {\n ESMxCJSRequire,\n getConfiguration,\n IntlayerConfig,\n} from '@intlayer/config';\nimport { IntlayerPlugin } from '@intlayer/webpack';\nimport merge from 'deepmerge';\nimport type { NextConfig } from 'next';\nimport type { NextJsWebpackConfig } from 'next/dist/server/config-shared';\nimport { join, relative, resolve } from 'path';\nimport { compareVersions } from './compareVersion';\nimport { getNextVersion } from './getNextVertion';\n\ncleanOutputDir();\n\n// Extract from the start script if --turbo or --turbopack flag is used\nconst isTurbopackEnabled =\n process.env.npm_lifecycle_script?.includes('--turbo');\nconst nextVersion = getNextVersion();\nconst isGteNext13 = compareVersions(nextVersion, '≥', '13.0.0');\nconst isGteNext15 = compareVersions(nextVersion, '≥', '15.0.0');\nconst isTurbopackStable = compareVersions(nextVersion, '≥', '15.3.0');\n\n// Check if SWC plugin is available\nconst getIsSwcPluginAvailable = () => {\n try {\n ESMxCJSRequire.resolve('@intlayer/swc');\n return true;\n } catch (e) {\n return false;\n }\n};\n\nconst getPruneConfig = (\n intlayerConfig: IntlayerConfig\n): Partial<NextConfig> => {\n const { contentDir, dictionariesDir, mainDir } = intlayerConfig.content;\n\n const isProduction = process.env.NODE_ENV === 'production';\n\n if (!isProduction) return {};\n\n if (!isGteNext13) return {};\n\n const isSwcPluginAvailable = getIsSwcPluginAvailable();\n if (!isSwcPluginAvailable) return {};\n\n const dictionariesPath = join(mainDir, 'dictionaries.mjs');\n\n return {\n experimental: {\n swcPlugins: [\n [\n ESMxCJSRequire.resolve('@intlayer/swc'),\n {\n dictionaries_dir: dictionariesDir,\n dictionaries_entry_path: dictionariesPath,\n content_dir: contentDir,\n } as any,\n ],\n ],\n },\n };\n};\n\ntype WebpackParams = Parameters<NextJsWebpackConfig>;\n\n/**\n * A Next.js plugin that adds the intlayer configuration to the webpack configuration\n * and sets the environment variablesi\n *\n * Usage:\n *\n * ```ts\n * // next.config.js\n * export default withIntlayer(nextConfig)\n * ```\n */\nexport const withIntlayer = <T extends Partial<NextConfig>>(\n nextConfig: T = {} as T\n): NextConfig & T => {\n if (typeof nextConfig !== 'object') {\n nextConfig = {} as T;\n }\n\n const intlayerConfig = getConfiguration();\n\n // Format all configuration values as environment variables\n const { mainDir, configDir, baseDir } = intlayerConfig.content;\n\n const dictionariesPath = join(mainDir, 'dictionaries.mjs');\n const relativeDictionariesPath = relative(baseDir, dictionariesPath);\n\n const unmergedDictionariesPath = join(mainDir, 'unmerged_dictionaries.mjs');\n const relativeUnmergedDictionariesPath = relative(\n baseDir,\n unmergedDictionariesPath\n );\n\n const configurationPath = join(configDir, 'configuration.json');\n const relativeConfigurationPath = relative(baseDir, configurationPath);\n\n // Only provide turbo-specific config if user explicitly sets it\n const turboConfig = {\n resolveAlias: {\n // \"prefix by './' to consider the path as relative to the project root. This is necessary for turbo to work correctly.\"\n '@intlayer/dictionaries-entry': `./${relativeDictionariesPath}`,\n '@intlayer/unmerged-dictionaries-entry': `./${relativeUnmergedDictionariesPath}`,\n '@intlayer/config/built': `./${relativeConfigurationPath}`,\n },\n rules: {\n '*.node': {\n as: '*.node',\n loaders: ['node-loader'],\n },\n },\n };\n\n const serverExternalPackages = [\n 'esbuild',\n 'module',\n 'fs',\n 'chokidar',\n 'fsevents',\n ];\n\n const newConfig: Partial<NextConfig> = {\n // Only add `serverExternalPackages` if Next.js is v15+\n ...(isGteNext15\n ? {\n // only for Next ≥15\n serverExternalPackages,\n }\n : {\n // only for Next ≥13 and <15.3\n ...(isGteNext13 && {\n serverComponentsExternalPackages: serverExternalPackages,\n }),\n }),\n\n ...(isTurbopackEnabled && {\n ...(isGteNext15 && isTurbopackStable\n ? {\n // only for Next ≥15.3\n turbopack: turboConfig,\n }\n : {\n experimental: {\n // only for Next ≥13 and <15.3\n turbo: turboConfig,\n },\n }),\n }),\n\n webpack: (config: WebpackParams['0'], options: WebpackParams[1]) => {\n // If the user has defined their own webpack config, call it\n if (typeof nextConfig.webpack === 'function') {\n config = nextConfig.webpack(config, options);\n }\n\n // Alias the dictionary entry for all builds\n config.resolve.alias = {\n ...config.resolve.alias,\n '@intlayer/dictionaries-entry': resolve(relativeDictionariesPath),\n '@intlayer/unmerged-dictionaries-entry': resolve(\n relativeUnmergedDictionariesPath\n ),\n '@intlayer/config/built': resolve(relativeConfigurationPath),\n };\n\n // Mark these modules as externals\n config.externals.push({\n esbuild: 'esbuild',\n module: 'module',\n fs: 'fs',\n chokidar: 'chokidar',\n fsevents: 'fsevents',\n });\n\n // Use `node-loader` for any `.node` files\n config.module.rules.push({\n test: /\\.node$/,\n loader: 'node-loader',\n });\n\n // Only add Intlayer plugin on server side (node runtime)\n const { isServer, nextRuntime } = options;\n\n if (isServer && nextRuntime === 'nodejs') {\n config.plugins.push(new IntlayerPlugin());\n }\n\n return config;\n },\n };\n\n const pruneConfig: Partial<NextConfig> = getPruneConfig(intlayerConfig);\n\n const intlayerNextConfig: Partial<NextConfig> = merge(pruneConfig, newConfig);\n\n // Merge the new config with the user's config\n return merge(nextConfig, intlayerNextConfig) as NextConfig & T;\n};\n"],"mappings":"AAAA,SAAS,sBAAsB;AAC/B;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP,SAAS,sBAAsB;AAC/B,OAAO,WAAW;AAGlB,SAAS,MAAM,UAAU,eAAe;AACxC,SAAS,uBAAuB;AAChC,SAAS,sBAAsB;AAE/B,eAAe;AAGf,MAAM,qBACJ,QAAQ,IAAI,sBAAsB,SAAS,SAAS;AACtD,MAAM,cAAc,eAAe;AACnC,MAAM,cAAc,gBAAgB,aAAa,UAAK,QAAQ;AAC9D,MAAM,cAAc,gBAAgB,aAAa,UAAK,QAAQ;AAC9D,MAAM,oBAAoB,gBAAgB,aAAa,UAAK,QAAQ;AAGpE,MAAM,0BAA0B,MAAM;AACpC,MAAI;AACF,mBAAe,QAAQ,eAAe;AACtC,WAAO;AAAA,EACT,S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AG5E,SAAO,MAAM,YAAY,kBAAkB;AAC7C;","names":[]}
@@ -1 +1 @@
1
- {"version":3,"file":"intlayerMiddleware.d.ts","sourceRoot":"","sources":["../../../src/middleware/intlayerMiddleware.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,WAAW,EAChB,YAAY,EACb,MAAM,aAAa,CAAC;AA0CrB;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,kBAAkB,GAC7B,SAAS,WAAW,EACpB,SAAS,cAAc,EACvB,YAAY,YAAY,KACvB,YAuCF,CAAC"}
1
+ {"version":3,"file":"intlayerMiddleware.d.ts","sourceRoot":"","sources":["../../../src/middleware/intlayerMiddleware.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,WAAW,EAChB,YAAY,EACb,MAAM,aAAa,CAAC;AA0CrB;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,kBAAkB,GAC7B,SAAS,WAAW,EACpB,SAAS,cAAc,EACvB,YAAY,YAAY,KACvB,YA0BF,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"withIntlayer.d.ts","sourceRoot":"","sources":["../../../src/server/withIntlayer.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAgBvC;;;;;;;;;;GAUG;AACH,eAAO,MAAM,YAAY,GAAI,CAAC,SAAS,OAAO,CAAC,UAAU,CAAC,EACxD,aAAY,CAAW,KACtB,UAAU,GAAG,CAsHf,CAAC"}
1
+ {"version":3,"file":"withIntlayer.d.ts","sourceRoot":"","sources":["../../../src/server/withIntlayer.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AA4DvC;;;;;;;;;;GAUG;AACH,eAAO,MAAM,YAAY,GAAI,CAAC,SAAS,OAAO,CAAC,UAAU,CAAC,EACxD,aAAY,CAAW,KACtB,UAAU,GAAG,CA0Hf,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-intlayer",
3
- "version": "5.5.1",
3
+ "version": "5.5.3",
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": [
@@ -69,12 +69,12 @@
69
69
  "dependencies": {
70
70
  "deepmerge": "^4.3.1",
71
71
  "node-loader": "^2.1.0",
72
- "@intlayer/chokidar": "5.5.1",
73
- "@intlayer/config": "5.5.1",
74
- "@intlayer/dictionaries-entry": "5.5.1",
75
- "@intlayer/core": "5.5.1",
76
- "@intlayer/webpack": "5.5.1",
77
- "react-intlayer": "5.5.1"
72
+ "@intlayer/chokidar": "5.5.3",
73
+ "@intlayer/config": "5.5.3",
74
+ "@intlayer/core": "5.5.3",
75
+ "react-intlayer": "5.5.3",
76
+ "@intlayer/dictionaries-entry": "5.5.3",
77
+ "@intlayer/webpack": "5.5.3"
78
78
  },
79
79
  "devDependencies": {
80
80
  "@types/node": "^22.13.10",
@@ -88,22 +88,23 @@
88
88
  "tsc-alias": "^1.8.11",
89
89
  "tsup": "^8.4.0",
90
90
  "typescript": "^5.8.2",
91
- "@utils/ts-config": "1.0.4",
92
91
  "@utils/eslint-config": "1.0.4",
92
+ "@utils/ts-config": "1.0.4",
93
+ "@utils/ts-config-types": "1.0.4",
93
94
  "@utils/tsup-config": "1.0.4",
94
- "intlayer": "5.5.1",
95
- "@utils/ts-config-types": "1.0.4"
95
+ "intlayer": "5.5.3"
96
96
  },
97
97
  "peerDependencies": {
98
98
  "next": ">=14.0.0",
99
99
  "react": ">=16.0.0",
100
100
  "react-dom": ">=16.0.0",
101
- "@intlayer/config": "5.5.1",
102
- "@intlayer/core": "5.5.1",
103
- "@intlayer/dictionaries-entry": "5.5.1",
104
- "intlayer": "5.5.1",
105
- "@intlayer/webpack": "5.5.1",
106
- "react-intlayer": "5.5.1"
101
+ "@intlayer/config": "5.5.3",
102
+ "@intlayer/core": "5.5.3",
103
+ "@intlayer/dictionaries-entry": "5.5.3",
104
+ "intlayer": "5.5.3",
105
+ "@intlayer/webpack": "5.5.3",
106
+ "react-intlayer": "5.5.3",
107
+ "@intlayer/swc": "5.5.3"
107
108
  },
108
109
  "engines": {
109
110
  "node": ">=14.18"