Netlify doesn't recognise next.js middleware when deployed

Locally it works wonderfully with netlify dev. basically no setup required.
When deployed to Netlify it only recognises the next/image handler.

I tried setting the NEXT_USE_NETLIFY_EDGE even though the documentation says its default.
All our pages are build on request.

Anyone with similar issues and hopefully a fix/explanation? :smiley:

Regards.
Jimmi

Welcome to the Netlify Forums, Jimmi! :tada:

When you deploy the site, do you know what version of the Next.js Runtime it uses? It would also be great if you could share the Netlify site URL?

Cheers,
Ivan

Hi Ivan.
thanks :slight_smile:

I was just about to post a url - had to make a simple div instead of production content.
The site loads fine - the middleware just takes care of some redirects.

Site url: https://feature-customi18--profound-cendol-9e18bd.netlify.app/
Using Next.js Runtime - v4.28.4
β€œnext” at version β€œ12.3.1”

────────────────────────────────────────────────────────────────
12:56:44 PM: 4. Functions bundling
12:56:44 PM: ────────────────────────────────────────────────────────────────
12:56:44 PM: ​
12:56:44 PM: Packaging Functions from .netlify/functions-internal directory:
12:56:44 PM: - ___netlify-handler/___netlify-handler.js
12:56:44 PM: - ___netlify-odb-handler/___netlify-odb-handler.js
12:56:44 PM: - _ipx/_ipx.js
12:56:44 PM: ​
12:56:58 PM: ​
12:56:58 PM: (Functions bundling completed in 14s)
12:56:58 PM: ​
12:56:58 PM: ──────
──────────────────────────────────────────────────────────
12:56:58 PM: 5. Edge Functions bundling
12:56:58 PM: ────────────────────────────────────────────────────────────────
12:56:58 PM: ​
12:56:58 PM: Packaging Edge Functions from .netlify/edge-functions directory:
12:56:58 PM: - ipx

Regards.
Jimmi

Hi Ivan.
Thanks. My reply was hidden from because i pasted some logs i think:

Here is the siteurl : https://profound-cendol-9e18bd.netlify.app/
The site builds and loads fine. I had to remove the production content so its just showing a simple div.
The middleware is just taking care of some simple redirects.

β€œnext” at version β€œ12.3.1”
Using Next.js Runtime - v4.28.4

Regards.
Jimmi

Hey @jimmirbn,

This is strange indeed. Do you think you can share the repo with us to see what could be happening here?

Hi hrishikesh
Unfortunately not. But I tried setting up a very simple repo and I discovered that middleware/edge-function gets loaded. So it must be some configuration in my projects repo.
Maybe cause I have it all in a /src folder. The create next app doesn’t do that.
I will try and debug some more and let you know.

Hey @hrishikesh
I tried adding the demo project to a src folder - which next.js supports.
But it seems like netlify then doesn’t load the middleware file.
Demo repo: GitHub - jimmi-rbn/nextjsMiddleware
Demo site: https://remarkable-melba-324069.netlify.app/

I will now try and move my production site out of /src folder
It if doesn’t work - should I make a issue in the next-runtime repo?

Regards Jimmi

Hey @hrishikesh
I can now confirm it works when I move my project out of /src folder.

Regards
Jimmi

Great to hear you found a way to get things working! Is that a tenable long-term solution for you?

Hi @fool.
It sort of works, sometimes. I experience often that it doesn’t run the middleware function even though Netlify detects it. The edge-function log is empty.

I wont use it for now, seems too Beta.

regards
Jimmi