next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Docs: Middleware files needs same suffix as pages if pageExtensions config is used

Open eaardal opened this issue 2 years ago • 4 comments

What is the improvement or update you wish to see?

The middleware docs should mention that the middleware.ts file must adhere to the same naming convention as the normal pages if the pageExtensions config option is used in next.config.js. If you have pageExtensions: ['page.tsx', 'page.ts'], it must be middleware.page.ts.

Is there any context that might help us understand?

I've been trying for hours to understand why my middleware wasn't triggering at all. Tried putting a middleware.ts file in the repo's root, in src, in src/pages and in src/pages/{page} but just dead silence.

Realized that it might be that because I use the pageExtensions config in next.config.js, the middleware.ts must perhaps adhere to the same naming convention, which it apparatly does. Been using pageExtensions for so long I'd forgotten it isn't a default behaviour. I have it set to pageExtensions: ['page.tsx', 'page.ts', 'page.js', 'page.jsx'] and my pages are usually something.page.tsx so the middleware file must be middleware.page.ts to trigger.

Whether this is the correct behaviour or if it's a bug where the middleware files should instead ignore the pageExtensions, I leave to you to decide. But if it's the intended behaviour, it should be mentioned in the middleware docs somewhere.

Does the docs page already exist? Please link to it.

https://nextjs.org/docs/advanced-features/middleware

eaardal avatar Jan 06 '23 13:01 eaardal

Hi, this is currently documented under the pageExtensions docs here https://nextjs.org/docs/api-reference/next.config.js/custom-page-extensions

ijjk avatar Jan 06 '23 18:01 ijjk

Can it be linked to from the middleware docs then? It took a good while to connect the dots, and I was mainly sticking to the middleware documentation page as I didn't know where else to look.

eaardal avatar Jan 09 '23 08:01 eaardal

Related previous PRs

x-ref: https://github.com/vercel/next.js/pull/40016 x-ref: https://github.com/vercel/next.js/pull/40039

ijjk avatar Jan 09 '23 18:01 ijjk

Absolutely agree with @eaardal. There is no single reference in middleware docs about this aspect of this file naming use-case. 🤷🏻‍♂️ Very confusing IMO

demakoff avatar Jun 05 '24 14:06 demakoff