Skip to content
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

MiddlewareRequest on ntl dev throws error: MiddlewareRequest only works in a Netlify Edge Function environment #2735

Open
pdme opened this issue Dec 29, 2024 · 0 comments

Comments

@pdme
Copy link

pdme commented Dec 29, 2024

The following middleware.ts in a newly created next app using npx create-next-app@latest, running with netlify dev, throws an error:

import { MiddlewareRequest, NextRequest } from '@netlify/next'

export async function middleware(req: NextRequest) {

  const request = new MiddlewareRequest(req)
  
  return request.next()
}

The error:

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

I'm aware this is a copy of #2003, but the solution given -- use netlify dev instead of next dev -- does not seem to work (anymore).

Maybe something to do with new both React and Next versions with major upgrades?

This feature is important to me, it determines whether to switch from Vercel or not...

EDIT: The same error occurs when running a build. The build itself completes without errors, but the app throws a 500 with the same error message as above.

@pieh pieh added Next.js e2e test failure Errors identified through the Next.js repo e2e tests and removed Next.js e2e test failure Errors identified through the Next.js repo e2e tests labels Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants