cloudflare-next-intl 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,8 @@
1
1
  import type { NextRequest } from 'next/server';
2
2
  import { NextResponse } from 'next/server';
3
+ import type { MiddlewareCustomHandler } from '../types/types';
3
4
  export declare const localesSet: Set<string>;
4
- export default function intlMiddleware(request: NextRequest): Promise<NextResponse<unknown>>;
5
+ export default function intlMiddleware(request: NextRequest, options?: {
6
+ middlewareHandler?: MiddlewareCustomHandler;
7
+ runHandlerOnRedirect?: boolean;
8
+ }): Promise<NextResponse<unknown>>;
@@ -20,7 +20,7 @@ async function getIsBotValue(userAgent) {
20
20
  const getIsBotValueCache = cache(getIsBotValue);
21
21
  export const localesSet = new Set(config.locales);
22
22
  // This middleware function runs for every incoming request
23
- export default async function intlMiddleware(request) {
23
+ export default async function intlMiddleware(request, options) {
24
24
  try {
25
25
  let initialChosenLocale;
26
26
  const existingLocaleCookie = request.cookies.get(localeCookieName)?.value;
@@ -54,13 +54,16 @@ export default async function intlMiddleware(request) {
54
54
  }
55
55
  const effectiveLocaleForRequest = urlLocale ?? initialChosenLocale;
56
56
  let response;
57
+ let isRedirect = false;
58
+ let targetUrl;
57
59
  if (!urlLocale) {
58
60
  const targetPath = `/${effectiveLocaleForRequest}${pathWithoutLocale === '/' ? '' : pathWithoutLocale}`;
59
- const targetUrl = new URL(`${targetPath}${search}${hash}`, request.url);
61
+ targetUrl = new URL(`${targetPath}${search}${hash}`, request.url);
60
62
  if (initialChosenLocale === config.defaultLocale) {
61
63
  response = NextResponse.rewrite(targetUrl, { request });
62
64
  }
63
65
  else {
66
+ isRedirect = true;
64
67
  response = NextResponse.redirect(targetUrl, request);
65
68
  }
66
69
  }
@@ -69,6 +72,12 @@ export default async function intlMiddleware(request) {
69
72
  request,
70
73
  });
71
74
  }
75
+ if (options?.middlewareHandler && (!isRedirect || options.runHandlerOnRedirect)) {
76
+ const customResponse = await options.middlewareHandler(request, effectiveLocaleForRequest, targetUrl);
77
+ if (customResponse) {
78
+ response = customResponse;
79
+ }
80
+ }
72
81
  if (!existingLocaleCookie ||
73
82
  existingLocaleCookie !== effectiveLocaleForRequest) {
74
83
  response.cookies.set(localeCookieName, effectiveLocaleForRequest, defaultCookieOption);
@@ -1,5 +1,7 @@
1
+ import type { NextRequest, NextResponse } from 'next/server';
1
2
  import type { Languages } from 'next/dist/lib/metadata/types/alternative-urls-types';
2
3
  import type { Videos } from 'next/dist/lib/metadata/types/metadata-types';
4
+ export type MiddlewareCustomHandler = (request: NextRequest, locale: string, targetUrl: URL | undefined) => NextResponse<unknown> | null | Promise<NextResponse<unknown> | null>;
3
5
  export type Locales = readonly string[];
4
6
  export type LocalePrefixMode = 'always' | 'as-needed' | 'never';
5
7
  export interface RoutingConfig<AppLocales extends Locales, AppLocalePrefixMode extends LocalePrefixMode> {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cloudflare-next-intl",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Optimized Next Intl Package Special for App Router and Cloudflare",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",