Next.js Discord

Discord Forum

I think im fucked with nextjs edge runtime in middleware

Answered
Cane di Oropa posted this in #help-forum
Open in Discord
Cane di OropaOP
did anyone found any workaround? i am using lucia auth and postgresql, in middleware pg is using some node modules not supported by nextjs in edge runtime. guess im fucked
Answered by "use php"
You can mark this message as a solution if resolved
View full answer

54 Replies

Cane di OropaOP
does it mean nextjs middleware is unusable?
cause the edge runs on the server and if u import client only functions yk that wont work
Um so... Basically what i found is that if you need a auth guard you'll need to use the pages or layout itself
Its not gonna work well with the middleware unfortunately
@Cane di Oropa using middleware as your only layer of defense for auth isn't good practice anyways
Cane di OropaOP
@Clown Um so... Basically what i found is that if you need a auth guard you'll need to use the pages or layout itself
Cane di OropaOP
doing auth check at layout is pretty bad because layout could be cache / not guaranteed re-rendered everytime

pages wise, if i have 10, or 20 pages to be protected it's very inconvecient to manage and most critically you might miss out some pages
but im just trying to do a session check on whether user is logged in
@gin cause the edge runs on the server and if u import client only functions yk that wont work
Cane di OropaOP
not client function, the function exists in nodejs runtime, but edge runtime does not support all of the nodejs runtime
@Cane di Oropa doing auth check at layout is pretty bad because layout could be cache / not guaranteed re-rendered everytime pages wise, if i have 10, or 20 pages to be protected it's very inconvecient to manage and most critically you might miss out some pages
Yes however if you are doing database sessions and using a ORM for example prisma which doesn't directly support Edge w/o Accelerate, then you'll have to do a combination of a DAL(doing a auth guard before accessing data) + a check in layout.

You could do without it but there's going a delay where your layout ends up rendering (most of time but not guaranteed), and THEN you get sent back to the login page. It can be quite jarring.
You can still fetch in middleware, fetch to a backend api which is running the node runetime.
Cane di OropaOP
i used to love nextjs, until middleware restrictied to edge runtime which is really unreasonable
@Jboncz You can still fetch in middleware, fetch to a backend api which is running the node runetime.
Cane di OropaOP
yes mate, im creating an api, which is extra effort
nextjs has been restricted to the edge runtime for a longggg time.
They have stated they intend to allow people to opt into the node runtime in middleware at some point, but it wouldnt be available while hosting through vercel.
Cane di OropaOP
bro u seems like interested to talk about middleware runtiem in nextjs
if i self hosted nextjs in docker and k8s, what would be the runtime in middleware.ts? should be nodejs?
@Cane di Oropa bro u seems like interested to talk about middleware runtiem in nextjs
Thats what your post is about? What else would I discuss? Im just trying to give you relevant information?
@Cane di Oropa if i self hosted nextjs in docker and k8s, what would be the runtime in middleware.ts? should be nodejs?
Self hosting currently the edge runtime is still the only thing available, but im saying in the future there will be an option to make your middleware the node runtime
Cane di OropaOP
got it, i thought if i self hosted the runtime would be different
Nope, limitation of the framework at this time.
Node runtime is too slow to startup and too big. That's why they use edge. Its faster to startup but has less functionality. For a middleware that is running on every single request, node will just slow you down
you could have a layout do the redirecting
you should still have auth on each call that accesses sensitive info anyways so it shouldn't be a security issue
Since I self host on our internal network and its not exposed outside I pretty much do all of my authentication in the middleware, and I do authentication everytime a server action is called. You either can or cant get into a page, and you either can or cant use a server action 🤷‍♂️
@Lukas you could have a layout do the redirecting
Cane di OropaOP
having auth/session check on layout is bad
layout not necessary always been rendered
@Jboncz Since I self host on our internal network and its not exposed outside I pretty much do all of my authentication in the middleware, and I do authentication everytime a server action is called. You either can or cant get into a page, and you either can or cant use a server action 🤷‍♂️
Cane di OropaOP
middleware is the best gate to catch request like what you had mentioned..else if implementing check on each page, 3 pages are manageable, what if 20-30 pages? human might miss out
currently im doing the session check in this way
user visit page on brower -> trigger middleware -> middleware calls route handler -> route handler redirect -> browser redirect
if you are using a api route, you can user a higher up function
Like

you can define a function AuthMiddleware, and use it in route.ts, and it can execute it once it has verified the auth
thats the most weird thing about nextjs, forced to use edge in middleware.

But for me, I use edge in my entire app, so not a problem with me :)
may i know how much u paying for hosting on vercel monthly?
I don't use vercel
Cane di OropaOP
oh?..
u self hosting?
no
Cf pages
Cane di OropaOP
i see
For vercel, I'll have to pay $20/month initially, and I only get about 1-10K visits/month, so not worth it
For large platforms its worth it
Cane di OropaOP
cf has many freebies, do u host for free there
It has 100K reqs free per day
Cane di OropaOP
i dont trust vercel
After that, just a $5/month plan + usage
@Cane di Oropa i dont trust vercel
Lets continue the convo @ #off-topic
@Cane di Oropa resolved?
This issue
Answer