Next.js Discord

Discord Forum

Next13 middleware treated as a client side component

Unanswered
Aerial yellowjacket posted this in #help-forum
Open in Discord
Aerial yellowjacketOP
When using middleware, if any module has "server-only", Next will not build. This is a MAJOR issue: Next intentionally blurs the line between client and server code, so ensuring that one does not accidently include sensitive server information in the client build is paramount. However, including any server component that has "server-only" in middleware will break the build forcing one to remove the "server-only" This is a huge issue because it's all too easy to accidentally import a function that shouldn’t be imported especially with VSCode’s help to auto complete imports. You can do it without even noticing.

To note that there is an issue raised in Next (https://github.com/vercel/next.js/issues/54549) however it is getting very little airtime. I am wondering why? This is such a big issue that it may derail us from using Next. Anyone have any answers?

8 Replies

European sprat
yeah it's annoying. i've had to separate out my files so i have things like "auth.ts" and "middleware-auth.ts" so i make sure things stay separated
middleware runs on edge runtime only
Aerial yellowjacketOP
It's more than annoying... it makes Next unusable. Auth is the most important thing (I do agree), but anything that leaks into middleware that has server-only crashes it
What I can't understand is why Next devs are giving this so little airtime
European sprat
yeah that's why i've had to separate out files so that i don't have "server-only". edge runtime is considered "browser" in some ways for whatever reason
Aerial yellowjacketOP
Do you think it is possible to alter webpack (I assume they are using that) to remove "server-only" from all middleware before building?
Actually, this would open up a can of worms
Aerial yellowjacketOP
@European sprat FYI, this problem has been fixed now