Next.js Discord

Discord Forum

-

Unanswered
American Crocodile posted this in #help-forum
Open in Discord
Original message was deleted.

37 Replies

Original message was deleted
you can make a nodejs route handler/api route and fetch that api route in your middleware
Original message was deleted
oh yeah forgot middleware captures that as well...

use the middleware matcher to exclude that api route
Black carp
@American Crocodile is there any specific reason why u want to use prisma in middleware?
I dont think this is the right way to do that..generally middlewares are for anything to protect before actually handling the api endpoints..u can do this in api endpoints itself
What u mean by repetitive?
U can add conditions
Okay got it
So u need to access the db before the endpoint
Original message was deleted
Black carp
Currently what I could think of is creating a middleware function which can handle ur prisma..and can wrap api function
Original message was deleted
Black carp
Exclude from middleware?
Original message was deleted
Black carp
U can simple do if else …if the url is that u want to exclude then simply
return NextResponse.next()
Original message was deleted
Black carp
Pathname only gives the path like /api/auth
U dont have to write complete url
Show ur complete middleware
Do one thing
Fetch before the if else
What that auth/session is doing first of all can u show?
It matters ..u have added a simple fetch
Original message was deleted
Black carp
But u are not storing it in a variable?
Just get?
Ok if ít doesn’t matters then simple what u could do is…just fetch that api and on next line return nextresponse.next
No need of if else
Just add then a condition if url not equal to /auth/session then only fetch other wise just just return next
No this condition is different
import { NextRequest, NextResponse } from 'next/server'

export async function middleware(request: NextRequest) {
  if (!request.url.startsWith('http://localhost:3000/api/auth/session')) {
   await fetch('http://localhost:3000/api/auth/session')

return NextResponse.next()

  } 
   
    return NextResponse.next()

}
Try this..still im not sure..this should work
how about next-connect if you want kinda middleware for node,js runtime?
i think so. it’s just lib so it works self hosted servers as well as AWS lambda.
Edge Runtime is subset of Node.js
i understand your frustration
or host on AWS, GCP and so on
prisma is implemented in Rust tokio so it’s not that simple to work in Edge Runtime, Cloudflare Worker
Black carp
@American Crocodile the current best approah is..u need to create a middleware function for ur api route..that what I said previously.
@American Crocodile Three pings here but idk what happened, but please do not delete all your messages and your post as many other people may share the same question as you and come to this post. What would happen if all stackoverflow questions were deleted right after they are answered?