Middleware not working
Answered
Amur catfish posted this in #help-forum
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!
PS: the rest of the project works fine!
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!
PS: the rest of the project works fine!
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
45 Replies
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
https://codesandbox.io/p/devbox/elegant-alex-zg97gh
Amur catfishOP
middleware.ts
I also changed the place of the file multiple times but no ones work, rn it's like this:
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
Amur catfishOP
it doesn't work too
The project work well, no pb about it. It just works like if i didn't have a middleware
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
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
- I downloaded it.
- Used npm install to install the dependencies
- Used npm run dev
- Opened the console and I still have nothing
what console do you open?
Amur catfishOP
browser
the middleware is only executed on the server. But the browser is the client. So check your serverside console
Amur catfishOP
There is still nothing
can you show a screenshot of it?
Amur catfishOP
can open and reload the page?
should look like this
Amur catfishOP
It doesn't change anything when i reload
open your browser console and checkmark the
disable cache
. Keep the console open. Refresh the pageAmur catfishOP
Also when i go on network i don't see it communicate w the middleware
okk
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
Amur catfishOP
Isn't suppose to send a request to the middleware from the browser?
Amur catfishOP
Ok i understand better
what's the result of this?https://nextjs-forum.com/post/1299464303979663411#message-1299473236702138399
Amur catfishOP
nothing happens
you should see at least request of the page as the cache is disabled. You should see
GET / 200 in 0ms
inside your server consoleAmur catfishOP
yes my bad, I can see that, I mean I don't see the message saying that the middleware si triggered
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
Amur catfishOP
Thank you for you time! No worries, I will keep trying
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
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
ah that’s quite strange, i unfortunately don’t have any other suggestions at the moment
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
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
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 😢
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 😔