Dumb Next question (noob?)
I need to take a piece of data I fetch in my middleware and pass it to my routes and pages.
Right now, I fetch the data in my middleware, and then I have to fetch it again in my app.
The middleware controls redirects and the app consumes the data.
I'll attach a pic for clarity, it's just a user object returned from my API endpoint (it's on a separate server)
Ideally I'd like to have the API endpoint be called once in the middleware, then all other times it's called it's either cached or the middleware sends the data from the API directly to the client.
In Express, I'd use
Right now, I fetch the data in my middleware, and then I have to fetch it again in my app.
The middleware controls redirects and the app consumes the data.
I'll attach a pic for clarity, it's just a user object returned from my API endpoint (it's on a separate server)
Ideally I'd like to have the API endpoint be called once in the middleware, then all other times it's called it's either cached or the middleware sends the data from the API directly to the client.
In Express, I'd use
request.locals for this but idk how to do it in Next.
Solution
Not sure how your app has the auth implemented but as far as I know Next automaticaly deduplicates fetch requests (fires it only once and then serves it from the cache) and actually recommends to repeat the fetch queries throughout your app code.
So if your main goal is to reduce DB calls this takes care of it.
So if your main goal is to reduce DB calls this takes care of it.