|
1 | 1 | import type { Context } from '@netlify/edge-functions' |
2 | 2 |
|
3 | | -import { |
4 | | - addBasePath, |
5 | | - addLocale, |
6 | | - addTrailingSlash, |
7 | | - normalizeDataUrl, |
8 | | - normalizeLocalePath, |
9 | | - removeBasePath, |
10 | | -} from './util.ts' |
| 3 | +import type { RequestData } from './types' |
11 | 4 |
|
12 | | -interface I18NConfig { |
13 | | - defaultLocale: string |
14 | | - localeDetection?: false |
15 | | - locales: string[] |
16 | | -} |
17 | | - |
18 | | -export interface RequestData { |
19 | | - geo?: { |
20 | | - city?: string |
21 | | - country?: string |
22 | | - region?: string |
23 | | - latitude?: string |
24 | | - longitude?: string |
25 | | - timezone?: string |
26 | | - } |
27 | | - headers: Record<string, string> |
28 | | - ip?: string |
29 | | - method: string |
30 | | - nextConfig?: { |
31 | | - basePath?: string |
32 | | - i18n?: I18NConfig | null |
33 | | - trailingSlash?: boolean |
34 | | - skipMiddlewareUrlNormalize?: boolean |
35 | | - } |
36 | | - page?: { |
37 | | - name?: string |
38 | | - params?: { [key: string]: string } |
39 | | - } |
40 | | - url: string |
41 | | - body?: ReadableStream<Uint8Array> |
42 | | - detectedLocale?: string |
43 | | -} |
44 | | - |
45 | | -const normalizeRequestURL = ( |
46 | | - originalURL: string, |
47 | | - nextConfig?: RequestData['nextConfig'], |
48 | | -): { url: string; detectedLocale?: string } => { |
49 | | - const url = new URL(originalURL) |
50 | | - |
51 | | - let pathname = removeBasePath(url.pathname, nextConfig?.basePath) |
52 | | - |
53 | | - // If it exists, remove the locale from the URL and store it |
54 | | - const { detectedLocale } = normalizeLocalePath(pathname, nextConfig?.i18n?.locales) |
55 | | - |
56 | | - if (!nextConfig?.skipMiddlewareUrlNormalize) { |
57 | | - // We want to run middleware for data requests and expose the URL of the |
58 | | - // corresponding pages, so we have to normalize the URLs before running |
59 | | - // the handler. |
60 | | - pathname = normalizeDataUrl(pathname) |
61 | | - |
62 | | - // Normalizing the trailing slash based on the `trailingSlash` configuration |
63 | | - // property from the Next.js config. |
64 | | - if (nextConfig?.trailingSlash) { |
65 | | - pathname = addTrailingSlash(pathname) |
66 | | - } |
67 | | - } |
68 | | - |
69 | | - url.pathname = addBasePath(pathname, nextConfig?.basePath) |
70 | | - |
71 | | - return { |
72 | | - url: url.toString(), |
73 | | - detectedLocale, |
74 | | - } |
75 | | -} |
76 | | - |
77 | | -export const localizeRequest = ( |
78 | | - url: URL, |
79 | | - nextConfig?: { |
80 | | - basePath?: string |
81 | | - i18n?: I18NConfig | null |
82 | | - }, |
83 | | -): { localizedUrl: URL; locale?: string } => { |
84 | | - const localizedUrl = new URL(url) |
85 | | - localizedUrl.pathname = removeBasePath(localizedUrl.pathname, nextConfig?.basePath) |
86 | | - |
87 | | - // Detect the locale from the URL |
88 | | - const { detectedLocale } = normalizeLocalePath(localizedUrl.pathname, nextConfig?.i18n?.locales) |
89 | | - |
90 | | - // Add the locale to the URL if not already present |
91 | | - localizedUrl.pathname = addLocale( |
92 | | - localizedUrl.pathname, |
93 | | - detectedLocale ?? nextConfig?.i18n?.defaultLocale, |
94 | | - ) |
95 | | - |
96 | | - localizedUrl.pathname = addBasePath(localizedUrl.pathname, nextConfig?.basePath) |
97 | | - |
98 | | - return { |
99 | | - localizedUrl, |
100 | | - locale: detectedLocale, |
101 | | - } |
102 | | -} |
103 | | - |
104 | | -export const buildNextRequest = ( |
105 | | - request: Request, |
106 | | - context: Context, |
107 | | - nextConfig?: RequestData['nextConfig'], |
108 | | -): RequestData => { |
| 5 | +export const buildNextRequest = (request: Request): RequestData => { |
109 | 6 | const { url, method, body, headers } = request |
110 | | - const { country, subdivision, city, latitude, longitude, timezone } = context.geo |
111 | | - const geo: RequestData['geo'] = { |
112 | | - city, |
113 | | - country: country?.code, |
114 | | - region: subdivision?.code, |
115 | | - latitude: latitude?.toString(), |
116 | | - longitude: longitude?.toString(), |
117 | | - timezone, |
118 | | - } |
119 | 7 |
|
120 | | - const { detectedLocale, url: normalizedUrl } = normalizeRequestURL(url, nextConfig) |
| 8 | + // we don't really use it but Next.js expects a signal |
| 9 | + const abortController = new AbortController() |
121 | 10 |
|
122 | 11 | return { |
123 | 12 | headers: Object.fromEntries(headers.entries()), |
124 | | - geo, |
125 | | - url: normalizedUrl, |
126 | 13 | method, |
127 | | - ip: context.ip, |
| 14 | + // nextConfig?: { |
| 15 | + // basePath?: string; |
| 16 | + // i18n?: I18NConfig | null; |
| 17 | + // trailingSlash?: boolean; |
| 18 | + // experimental?: Pick<ExperimentalConfig, 'cacheLife' | 'authInterrupts' | 'clientParamParsingOrigins'>; |
| 19 | + // }; |
| 20 | + // page?: { |
| 21 | + // name?: string; |
| 22 | + // params?: { |
| 23 | + // [key: string]: string | string[] | undefined; |
| 24 | + // }; |
| 25 | + // }; |
| 26 | + url, |
128 | 27 | body: body ?? undefined, |
129 | | - nextConfig, |
130 | | - detectedLocale, |
| 28 | + signal: abortController.signal, |
| 29 | + /** passed in when running in edge runtime sandbox */ |
| 30 | + // waitUntil?: (promise: Promise<any>) => void; |
131 | 31 | } |
132 | 32 | } |
0 commit comments