Next.js Discord

Discord Forum

Middleware not working

Answered
Amur catfish posted this in #help-forum
Open in Discord
Avatar
Amur catfishOP
My middleware is not working.

Even with something simple like this:

WHAT I TRIED:
- I have the last version of next, i came back to a previous one - not working
- Reinstall next/dependencies - not working
- Reboot vscode/project/pc - not working
- Create a new project to test - not working
- Try a different browser

I don't even understand the problem so fixing it is hard, if you have any idea I am open! :fine:
PS: the rest of the project works fine!
Image
Answered by Asian Whale
for me, i found out that having the src folder conflicted with my middleware running and next-auth functioning properly. not sure if this solution will work for you, but i simply got rid of my src folder so that my project files sat in app instead of src/app
View full answer

45 Replies

Avatar
it looks like there is something more wrong than what you shared. I created a reproduction of the code that you shared. It looks like it works fine. Whats the file name of your middleware?

https://codesandbox.io/p/devbox/elegant-alex-zg97gh
Avatar
Amur catfishOP
middleware.ts
I also changed the place of the file multiple times but no ones work, rn it's like this:
Image
Avatar
your file is at the wrong place. It need to be placed outside the app and outside of the src folder. Place it on the same level as your tsconfig.json
Avatar
Amur catfishOP
it doesn't work too
Image
The project work well, no pb about it. It just works like if i didn't have a middleware
Avatar
can you download the codesandbox files and execute them on your machine? Take a log at the server logs and see if it get triggered
Avatar
Amur catfishOP
Okay so
- I downloaded it.
- Used npm install to install the dependencies
- Used npm run dev
- Opened the console and I still have nothing
Avatar
what console do you open?
Avatar
Amur catfishOP
browser
Avatar
the middleware is only executed on the server. But the browser is the client. So check your serverside console
Avatar
Amur catfishOP
There is still nothing
Avatar
can you show a screenshot of it?
Avatar
Amur catfishOP
Image
Avatar
can open and reload the page?
should look like this
Image
Avatar
Amur catfishOP
It doesn't change anything when i reload
Avatar
open your browser console and checkmark the disable cache. Keep the console open. Refresh the page
Image
Avatar
Amur catfishOP
Also when i go on network i don't see it communicate w the middleware
okk
Avatar
now it should log at least GET / 200 in numberMs
as said: the middleware is only handled serverside. You won't see anything from it on the client
Avatar
Amur catfishOP
Isn't suppose to send a request to the middleware from the browser?
Image
Avatar
it's included in this request
Image
Avatar
Amur catfishOP
Ok i understand better
Avatar
Amur catfishOP
nothing happens
Avatar
you should see at least request of the page as the cache is disabled. You should see GET / 200 in 0ms inside your server console
Avatar
Amur catfishOP
yes my bad, I can see that, I mean I don't see the message saying that the middleware si triggered
Image
Avatar
well... I guess I runned out of ideas. Thanks for testing everything. Update this thread when you found a solution. I currently have none, sorry
Avatar
Amur catfishOP
Thank you for you time! No worries, I will keep trying
Avatar
for me, i found out that having the src folder conflicted with my middleware running and next-auth functioning properly. not sure if this solution will work for you, but i simply got rid of my src folder so that my project files sat in app instead of src/app
Answer
Avatar
Amur catfishOP
I moved the whole folder app out from the srx folder. But it didn't work so I discarded all the changes from my last push and now the middleware is working, however the rest of the project is broken
Avatar
ah that’s quite strange, i unfortunately don’t have any other suggestions at the moment
Avatar
Amur catfishOP
I had to clone my repo, it's still doesn't work even though now my project is working. I ll try to find a new solution
How did u find that btw ahah
it's so random
Avatar
oh it just occurred to me that it's something i might want to try
since the nextjs-dashboard project that you build when following the learn next.js guide didn't have the src folder and the middleware and auth stuff worked for that project
Avatar
Amur catfishOP
okok make sense i guess
OMG it's working thx, i had to change the tailwind.config.ts but it's working
thanks a lot, i spent like 5 hours on this 😢
Avatar
all good, glad i could suggest something
i feel you, i have my own thread open for something else auth related that no one has responded to yet 😔