Skip to content

[Bug]: Running Next Dev locally with MiddlewareRequest causes error #2003

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

Closed
3 tasks
isaac-martin opened this issue Mar 21, 2023 · 5 comments
Closed
3 tasks
Labels
status: awaiting user response type: bug code to address defects in shipped code

Comments

@isaac-martin
Copy link

Summary

Running next dev locally with MiddlewareRequest inside middleware.ts causes an error MiddlewareRequest only works in a Netlify Edge Function environment

image

If you have to run locally using netlify dev this should be called out inside the documentation.

Subsequently there is a typescript error also Types of property 'geo' are incompatible.

Steps to reproduce

Inside middleware.ts import { MiddlewareRequest } from "@netlify/next";
create a new netlify middleware request with the below code
const request = new MiddlewareRequest(req);
go to a url in browser and notice the error

A link to a reproduction repository

No response

Next Runtime version

x

Is your issue related to the app directory (beta)?

  • Yes, I am using the app directory

More information about your build

  • I am building using the CLI
  • I am building using file-based configuration (netlify.toml)

What OS are you using?

Mac OS

Your netlify.toml file

`netlify.toml`
# Paste content of your `netlify.toml` file here

Your public/_redirects file

`_redirects`
# Paste content of your `_redirects` file here

Your next.config.js file

`next.config.js`
# Paste content of your `next.config.js` file here. Check there is no private info in there.

Builds logs (or link to your logs)

Build logs
# Paste logs here

Function logs

Function logs
# Paste logs here

.next JSON files

generated .next JSON files
# Paste file contents here. Please check there isn't any private info in them
# You can either build locally, or download the deploy from Netlify by clicking the arrow next to the deploy time.
@isaac-martin isaac-martin added the type: bug code to address defects in shipped code label Mar 21, 2023
@sarahetter
Copy link

Hi @isaac-martin . I am unable to duplicate this behaviour. Here are the steps I took:

  1. Created a next app with npx create-next-app@latest, choosing the options below (hitting enter all the way down)

Screenshot 2023-03-21 at 2 55 44 PM

  1. Installed @netlify/next with npm i @netlify/next
  2. Added a middleware.ts file in the root:
import { MiddlewareRequest, NextRequest } from '@netlify/next'


export async function middleware(req: NextRequest) {

  const request = new MiddlewareRequest(req)
  
  return request.next()
}
  1. Ran ntl dev

I am seeing the site running with no errors.

Let me know what you have done differently than this to get your result.

@sarahetter
Copy link

Closing as stale

@isaac-martin
Copy link
Author

Sorry @sarahetter have been off work for the last few months. Am i correct in assuming that you are running your local dev environment with netlify dev ? Is this a requirement for using this?

@sarahetter
Copy link

Yes, I was running with netlify dev. The @netlify/next package makes use of edge functions, and so using our dev command rather than next dev is required.

I'll create an issue to make this clearer in the documentation.

@ghost
Copy link

ghost commented Dec 28, 2024

I am experiencing the same issue, even when running ntl dev.

To reproduce, I followed the steps above. I created a new next project (tried with several different options, app/pages, src, etc), installed @netlify/next, added the middleware.ts just like above, and ran ntl dev.

Then I get:

Error: MiddlewareRequest only works in a Netlify Edge Function environment
    at middleware (middleware.ts:6:18)
  4 | export async function middleware(req: NextRequest) {
  5 |
> 6 |   const request = new MiddlewareRequest(req)
    |                  ^
  7 |   
  8 |   return request.next()
  9 | }

My versions:

"dependencies": {
    "@netlify/next": "^1.4.9",
    "next": "15.1.3",
    "react": "^19.0.0",
    "react-dom": "^19.0.0"
  },

Can we please reopen this issue?

EDIT: even the demo app from Netlify itself for NextJS edge middleware seems broken... https://next-edge-middleware.netlify.app/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: awaiting user response type: bug code to address defects in shipped code
Projects
None yet
Development

No branches or pull requests

2 participants