Skip to content

fix: add missing data to middleware request object #1634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/src/middleware/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class MiddlewareRequest extends Request {
}

get nextUrl() {
return this.nextRequest.url
return this.nextRequest.nextUrl
}

get url() {
Expand Down
7 changes: 7 additions & 0 deletions packages/runtime/src/helpers/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { copy, copyFile, emptyDir, ensureDir, readJSON, readJson, writeJSON, wri
import type { MiddlewareManifest } from 'next/dist/build/webpack/plugins/middleware-plugin'
import type { RouteHas } from 'next/dist/lib/load-custom-routes'

import { getRequiredServerFiles } from './config'

// This is the format as of [email protected]
interface EdgeFunctionDefinitionV1 {
env: string[]
Expand Down Expand Up @@ -198,6 +200,11 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {

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

const { publish } = netlifyConfig.build
const nextConfigFile = await getRequiredServerFiles(publish)
const nextConfig = nextConfigFile.config
await writeJSON(join(edgeFunctionRoot, 'edge-shared', 'nextConfig.json'), nextConfig)

if (!process.env.NEXT_DISABLE_EDGE_IMAGES) {
console.log(
'Using Netlify Edge Functions for image format detection. Set env var "NEXT_DISABLE_EDGE_IMAGES=true" to disable.',
Expand Down
Empty file.
6 changes: 4 additions & 2 deletions packages/runtime/src/templates/edge/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Context } from 'https://edge.netlify.com'
// Available at build time
import matchers from './matchers.json' assert { type: 'json' }
import nextConfig from '../edge-shared/nextConfig.json' assert { type: 'json' }
import edgeFunction from './bundle.js'
import { buildResponse } from '../edge-shared/utils.ts'
import { getMiddlewareRouteMatcher, MiddlewareRouteMatch, searchParamsToUrlQuery } from '../edge-shared/next-utils.ts'
Expand Down Expand Up @@ -32,7 +33,7 @@ export interface RequestData {
name?: string
params?: { [key: string]: string }
}
url: string
url: URL
body?: ReadableStream<Uint8Array>
}

Expand Down Expand Up @@ -81,10 +82,11 @@ const handler = async (req: Request, context: Context) => {
const request: RequestData = {
headers: Object.fromEntries(req.headers.entries()),
geo,
url: url.toString(),
url,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes addresses the missing pathname property on req.nextURL

method: req.method,
ip: context.ip,
body: req.body ?? undefined,
nextConfig,
}

try {
Expand Down