@@ -10,24 +10,12 @@ import {
10
10
} from './util.ts'
11
11
12
12
import type { NextConfig } from 'next/dist/server/config-shared'
13
- import type { NextRequest } from 'next/server'
14
-
15
- export type NetlifyNextRequest = Pick <
16
- NextRequest ,
17
- 'url' | 'headers' | 'geo' | 'ip' | 'method' | 'body'
18
- >
19
-
20
- export type NetlifyNextContext = {
21
- localizedUrl : string
22
- detectedLocale ?: string
23
- i18n ?: NextConfig [ 'i18n' ]
24
- basePath ?: NextConfig [ 'basePath' ]
25
- trailingSlash ?: NextConfig [ 'trailingSlash' ]
26
- }
13
+ import type { NextRequest , RequestInit } from 'next/dist/server/web/spec-extension/request.js'
27
14
28
- const normalizeRequestURL = ( originalURL : string , nextConfig ?: NextConfig ) : string => {
29
- const url = new URL ( originalURL )
15
+ export type NetlifyNextRequest = RequestInit &
16
+ Pick < NextRequest , ' url' | 'headers' | 'geo' | 'ip' | 'method' | 'body' >
30
17
18
+ const normalizeRequest = ( url : URL , nextConfig ?: NextConfig ) : URL => {
31
19
url . pathname = removeBasePath ( url . pathname , nextConfig ?. basePath )
32
20
33
21
// We want to run middleware for data requests and expose the URL of the
@@ -41,15 +29,13 @@ const normalizeRequestURL = (originalURL: string, nextConfig?: NextConfig): stri
41
29
42
30
url . pathname = addBasePath ( url . pathname , nextConfig ?. basePath )
43
31
44
- return url . toString ( )
32
+ return url
45
33
}
46
34
47
- const localizeRequestURL = (
48
- originalURL : string ,
35
+ export const localizeRequest = (
36
+ url : URL ,
49
37
nextConfig ?: NextConfig ,
50
- ) : { localizedUrl : string ; detectedLocale ?: string } => {
51
- const url = new URL ( originalURL )
52
-
38
+ ) : { localizedUrl : URL ; locale ?: string } => {
53
39
url . pathname = removeBasePath ( url . pathname , nextConfig ?. basePath )
54
40
55
41
// Detect the locale from the URL
@@ -61,28 +47,27 @@ const localizeRequestURL = (
61
47
url . pathname = addBasePath ( url . pathname , nextConfig ?. basePath )
62
48
63
49
return {
64
- localizedUrl : url . toString ( ) ,
65
- detectedLocale,
50
+ localizedUrl : url ,
51
+ locale : detectedLocale ,
66
52
}
67
53
}
68
54
69
55
export const buildNextRequest = (
70
56
request : Request ,
71
57
context : Context ,
72
58
nextConfig ?: NextConfig ,
73
- ) : { nextRequest : NetlifyNextRequest ; nextContext : NetlifyNextContext } => {
74
- const { url , method, body, headers } = request
59
+ ) : NetlifyNextRequest => {
60
+ const { method, body, headers } = request
75
61
const { country, subdivision, city, latitude, longitude } = context . geo
76
62
const { i18n, basePath, trailingSlash } = nextConfig ?? { }
77
63
64
+ const url = new URL ( request . url )
78
65
const normalizedUrl = nextConfig ?. skipMiddlewareUrlNormalize
79
66
? url
80
- : normalizeRequestURL ( url , nextConfig )
81
-
82
- const { localizedUrl, detectedLocale } = localizeRequestURL ( normalizedUrl , nextConfig )
67
+ : normalizeRequest ( url , nextConfig )
83
68
84
- const nextRequest : NetlifyNextRequest = {
85
- url : normalizedUrl ,
69
+ return {
70
+ url : normalizedUrl . toString ( ) ,
86
71
headers,
87
72
geo : {
88
73
city,
@@ -94,18 +79,10 @@ export const buildNextRequest = (
94
79
ip : context . ip ,
95
80
method,
96
81
body,
97
- }
98
-
99
- const nextContext = {
100
- localizedUrl,
101
- detectedLocale,
102
- i18n,
103
- trailingSlash,
104
- basePath,
105
- }
106
-
107
- return {
108
- nextRequest,
109
- nextContext,
82
+ nextConfig : {
83
+ i18n,
84
+ basePath,
85
+ trailingSlash,
86
+ } ,
110
87
}
111
88
}
0 commit comments