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 2 commits
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ Next.js Middleware works out of the box on Netlify. By default, middleware runs
support for running Middleware at the origin, set the environment variable `NEXT_DISABLE_NETLIFY_EDGE` to `true`. Be
aware that this will result in slower performance, as all pages that match middleware must use SSR.

### Limitations

Due to how the site configuration is handled when it's run using Netlify Edge Functions, data such as `locale` and `defaultLocale` will be missing on the `req.nextUrl` object when running `netlify dev`.
Copy link
Contributor

Choose a reason for hiding this comment

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

When you say "such as", does that mean there are other things missing?

Copy link
Author

Choose a reason for hiding this comment

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

I haven't yet done an audit of all the properties that are affected by this so I'm not 100% certain that these are the only properties affected on the nextUrl object.

Something to add here as well but the pathname (as reported by the user who originally raised this issue with us) was also incorrect before these changes and should be mentioned.


However, this data is available on `req.nextUrl` in a production environment.

## Monorepos

If you are using a monorepo you will need to change `publish` to point to the full path to the built `.next` directory,
Expand Down
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