@@ -4,6 +4,7 @@ import { MiddlewareManifest, NextConfig } from "config/index.js";
4
4
import { InternalEvent , InternalResult } from "types/open-next.js" ;
5
5
import { emptyReadableStream } from "utils/stream.js" ;
6
6
7
+ import { localizePath } from "./i18n/index.js" ;
7
8
//NOTE: we should try to avoid importing stuff from next as much as possible
8
9
// every release of next could break this
9
10
// const { run } = require("next/dist/server/web/sandbox");
@@ -32,33 +33,12 @@ type MiddlewareOutputEvent = InternalEvent & {
32
33
// and res.body prior to processing the next-server.
33
34
// @returns undefined | res.end()
34
35
35
- // NOTE: We need to normalize the locale path before passing it to the middleware
36
- // See https://github.com/vercel/next.js/blob/39589ff35003ba73f92b7f7b349b3fdd3458819f/packages/next/src/shared/lib/i18n/normalize-locale-path.ts#L15
37
- function normalizeLocalePath ( pathname : string ) {
38
- // first item will be empty string from splitting at first char
39
- const pathnameParts = pathname . split ( "/" ) ;
40
- const locales = NextConfig . i18n ?. locales ;
41
-
42
- ( locales || [ ] ) . some ( ( locale ) => {
43
- if (
44
- pathnameParts [ 1 ] &&
45
- pathnameParts [ 1 ] . toLowerCase ( ) === locale . toLowerCase ( )
46
- ) {
47
- pathnameParts . splice ( 1 , 1 ) ;
48
- pathname = pathnameParts . join ( "/" ) || "/" ;
49
- return true ;
50
- }
51
- return false ;
52
- } ) ;
53
-
54
- return locales && ! pathname . endsWith ( "/" ) ? `${ pathname } /` : pathname ;
55
- }
56
36
// if res.end() is return, the parent needs to return and not process next server
57
37
export async function handleMiddleware (
58
38
internalEvent : InternalEvent ,
59
39
) : Promise < MiddlewareOutputEvent | InternalResult > {
60
- const { rawPath , query } = internalEvent ;
61
- const normalizedPath = normalizeLocalePath ( rawPath ) ;
40
+ const { query } = internalEvent ;
41
+ const normalizedPath = localizePath ( internalEvent ) ;
62
42
// We only need the normalizedPath to check if the middleware should run
63
43
const hasMatch = middleMatch . some ( ( r ) => r . test ( normalizedPath ) ) ;
64
44
if ( ! hasMatch ) return internalEvent ;
@@ -68,7 +48,7 @@ export async function handleMiddleware(
68
48
const host = internalEvent . headers . host
69
49
? `https://${ internalEvent . headers . host } `
70
50
: "http://localhost:3000" ;
71
- const initialUrl = new URL ( rawPath , host ) ;
51
+ const initialUrl = new URL ( normalizedPath , host ) ;
72
52
initialUrl . search = convertToQueryString ( query ) ;
73
53
const url = initialUrl . toString ( ) ;
74
54
// console.log("url", url, normalizedPath);
0 commit comments