Skip to content

Commit 930e809

Browse files
committed
fix: add missing data to middleware request object
When the middleware runs on the edge, it's missing data that is hydrated by the NextServer when run in the context of the origin server. Also fix which property is accessed on nextURL in the context of the MiddlewareRequest.
1 parent 28ccc90 commit 930e809

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

packages/next/src/middleware/request.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class MiddlewareRequest extends Request {
8383
}
8484

8585
get nextUrl() {
86-
return this.nextRequest.url
86+
return this.nextRequest.nextUrl
8787
}
8888

8989
get url() {

packages/runtime/src/helpers/edge.ts

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { copy, copyFile, emptyDir, ensureDir, readJSON, readJson, writeJSON, wri
77
import type { MiddlewareManifest } from 'next/dist/build/webpack/plugins/middleware-plugin'
88
import type { RouteHas } from 'next/dist/lib/load-custom-routes'
99

10+
import { getRequiredServerFiles } from './config'
11+
1012
// This is the format as of [email protected]
1113
interface EdgeFunctionDefinitionV1 {
1214
env: string[]
@@ -198,6 +200,11 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
198200

199201
await copy(getEdgeTemplatePath('../edge-shared'), join(edgeFunctionRoot, 'edge-shared'))
200202

203+
const { publish } = netlifyConfig.build
204+
const nextConfigFile = await getRequiredServerFiles(publish)
205+
const nextConfig = nextConfigFile.config
206+
await writeJSON(join(edgeFunctionRoot, 'edge-shared', 'nextConfig.json'), nextConfig)
207+
201208
if (!process.env.NEXT_DISABLE_EDGE_IMAGES) {
202209
console.log(
203210
'Using Netlify Edge Functions for image format detection. Set env var "NEXT_DISABLE_EDGE_IMAGES=true" to disable.',

packages/runtime/src/templates/edge-shared/nextConfig.json

Whitespace-only changes.

packages/runtime/src/templates/edge/runtime.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Context } from 'https://edge.netlify.com'
22
// Available at build time
33
import matchers from './matchers.json' assert { type: 'json' }
4+
import nextConfig from '../edge-shared/nextConfig.json' assert { type: 'json' }
45
import edgeFunction from './bundle.js'
56
import { buildResponse } from '../edge-shared/utils.ts'
67
import { getMiddlewareRouteMatcher, MiddlewareRouteMatch, searchParamsToUrlQuery } from '../edge-shared/next-utils.ts'
@@ -32,7 +33,7 @@ export interface RequestData {
3233
name?: string
3334
params?: { [key: string]: string }
3435
}
35-
url: string
36+
url: URL
3637
body?: ReadableStream<Uint8Array>
3738
}
3839

@@ -81,10 +82,11 @@ const handler = async (req: Request, context: Context) => {
8182
const request: RequestData = {
8283
headers: Object.fromEntries(req.headers.entries()),
8384
geo,
84-
url: url.toString(),
85+
url,
8586
method: req.method,
8687
ip: context.ip,
8788
body: req.body ?? undefined,
89+
nextConfig,
8890
}
8991

9092
try {

0 commit comments

Comments
 (0)