Next.js Discord

Discord Forum

Middleware and getServerSideProps

Answered
thedevkim posted this in #help-forum
Open in Discord
Hello. I have a question. Which one do run first? Middleware or getServerSideProps ?
Answered by Ray
middlware
View full answer

11 Replies

Answer
Hi. I have a question. How can i execute only once the middleware ? i see that it executes multiple times on one page load only.

@Ray
@Ray you could use matcher to control when should the middleware run https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher
Got a question regarding matcher.

export const config = {
  matcher: "/data/shipments",
  matcher: "/data/shipments/:path*",
  matcher: "/data/shipments/:path*/edit",
};


I have this config.

The middleware does not execute when the url is /data/shipments

But it does on
/data/shipments/1
/data/shipments/1/edit

do you have any idea ?
@Ray You should pass an array of string to it
Hello. Got another question.

From middleware data. I want to pass a data from middlware to getServerProps so i can render it on my component.

Is it possible ?
@Ray not possible, they are running in different runtime
Hmm do you have any idea on what should i do.

in my middleware i request permissions. I get data wether user have write or read on the screen. How can i pass it to the screen ?
@Ray please open a new post
Okay.