Skip to content

Commit d055699

Browse files
committed
middleware simplification and unification with routing EF
1 parent 0438780 commit d055699

File tree

11 files changed

+1219
-1364
lines changed

11 files changed

+1219
-1364
lines changed

edge-runtime/lib/middleware.ts

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,5 @@
1-
import type { Context } from '@netlify/edge-functions'
2-
3-
import type { ElementHandlers } from '../vendor/deno.land/x/[email protected]/src/index.ts'
41
import { getCookies } from '../vendor/deno.land/[email protected]/http/cookie.ts'
52

6-
type NextDataTransform = <T>(data: T) => T
7-
8-
interface ResponseCookies {
9-
// This is non-standard that Next.js adds.
10-
// https://github.com/vercel/next.js/blob/de08f8b3d31ef45131dad97a7d0e95fa01001167/packages/next/src/compiled/@edge-runtime/cookies/index.js#L158
11-
readonly _headers: Headers
12-
}
13-
14-
interface MiddlewareResponse extends Response {
15-
originResponse: Response
16-
dataTransforms: NextDataTransform[]
17-
elementHandlers: Array<[selector: string, handlers: ElementHandlers]>
18-
get cookies(): ResponseCookies
19-
}
20-
21-
interface MiddlewareRequest {
22-
request: Request
23-
context: Context
24-
originalRequest: Request
25-
next(): Promise<MiddlewareResponse>
26-
rewrite(destination: string | URL, init?: ResponseInit): Response
27-
}
28-
29-
export function isMiddlewareRequest(
30-
response: Response | MiddlewareRequest,
31-
): response is MiddlewareRequest {
32-
return 'originalRequest' in response
33-
}
34-
35-
export function isMiddlewareResponse(
36-
response: Response | MiddlewareResponse,
37-
): response is MiddlewareResponse {
38-
return 'dataTransforms' in response
39-
}
40-
413
export const addMiddlewareHeaders = async (
424
originResponse: Promise<Response> | Response,
435
middlewareResponse: Response,

edge-runtime/lib/next-request.ts

Lines changed: 20 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,32 @@
11
import type { Context } from '@netlify/edge-functions'
22

3-
import {
4-
addBasePath,
5-
addLocale,
6-
addTrailingSlash,
7-
normalizeDataUrl,
8-
normalizeLocalePath,
9-
removeBasePath,
10-
} from './util.ts'
3+
import type { RequestData } from './types'
114

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 => {
1096
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-
}
1197

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()
12110

12211
return {
12312
headers: Object.fromEntries(headers.entries()),
124-
geo,
125-
url: normalizedUrl,
12613
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,
12827
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;
13131
}
13232
}

0 commit comments

Comments
 (0)