Clarify docs where to put middleware.ts
Answered
Greenish Elaenia posted this in #help-forum
Greenish ElaeniaOP
For App Router, at https://nextjs.org/docs/app/building-your-application/authentication#protecting-routes-with-middleware, it says:
Setting Up Middleware:
Create a middleware.ts or .js file in your project's root directory.
Include logic to authorize user access, such as checking for authentication tokens.
Given the project structure looks like this (image), what does "project root directory" mean?
I tried putting a middleware.ts in /middleware folder but it doesn't seem to be running
Setting Up Middleware:
Create a middleware.ts or .js file in your project's root directory.
Include logic to authorize user access, such as checking for authentication tokens.
Given the project structure looks like this (image), what does "project root directory" mean?
I tried putting a middleware.ts in /middleware folder but it doesn't seem to be running
23 Replies
Answer
Yellow Crazy Ant
Is there any effect if you use a folder or not
you can't use a folder 🤷â€â™‚ï¸
it needs to be in root path
Yellow Crazy Ant
Okay but my question, is what are the effects or disadvantage of putting it in a folder
you can't put it in a folder man
the file needs to be in the root path
Yellow Crazy Ant
Okay
so, your problem is solved?
Yellow Crazy Ant
Not really sir, because all I know is not to put it in a folder, so I don't know what damage that might come up
why do you want to put it in a folder?
Yellow Crazy Ant
I don't know just feel is nice to do
lmao, but you can't
Next.js only reads it in the root dir or the
/src
dirif you put it in a folder then Next.js can't read the file. its not a matter of common practice, but its part of the Next.js file convention
Yellow Crazy Ant
Thank you for explanation
Really help me alot
no worriess
Greenish ElaeniaOP
Tried it in a middleware folder, doing it that way means middleware.ts won't run
Yellow Crazy Ant
Okay