Apply API middlewares to getServerSideProps
See original GitHub issueFeature request
API request handlers use middleware (such as cookie parsing). I was surprised to find that getServerSideProps does not apply similar middleware as well, and I believe this would be a rather straightforward and desirable feature to include by default.
For instance, if I were to create a new API endpoint, the cookie parser attached to the req object is available to use.
// pages/api/endpoint.js
export default (req, res) => {
if (req.cookies.myCookie) {
// do something with cookie
}
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ name: 'John Doe' }))
}
However, it seems the cookie parser (API middleware) is not applied to getServerSideProps and so the following example does not work as desired:
// pages/Page.js
export async function getServerSideProps({ req, res }) {
if (req.cookies.myCookie) { /* cookie parser object not available */ }
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ name: 'John Doe' }))
Describe the solution you’d like
Apply API middleware to getServerSideProps by default.
Thanks!
Thank you for all of your amazing work on next.js!
Issue Analytics
- State:
- Created 4 years ago
- Reactions:31
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Advanced Features: Middleware - Next.js
Learn how to use Middleware to run code before a request is completed. ... Match all request paths except for the ones starting...
Read more >How to pass a value from Next.js middleware to API routes ...
Compute a value in middleware and pass it to your API route or getServerSideProps. Works in both Node and Edge runtimes.
Read more >How to add middleware to getServerSideProps : r/nextjs - Reddit
Hi, I need to add some node js middleware on this function. export async function getServerSideProps(ctx) {
Read more >Passing variables from middleware to page in Next.js 12 new ...
You'd need to use getServerSideProps in that page to access res.session . You can't access it directly from the component itself. – juliomalves....
Read more >Handling POST requests in Next.js getServerSideProps
urlencoded() returns an middleware function that has the common 3 arguments, request , response and next . We use promisify to turn the ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

It looks like req.query is still missing. Only cookies was added. Can you reopen this issue? The ability to write common utilities that operate both in page and api routes is stifled by them not having the same properties & types due to this middleware mismatch.
Wow @BoiForus thanks for the tip - looks like https://github.com/vercel/next.js/pull/19724 (by @mattfwood, reviewed + merged by @timneutkens ) also added the types
req.cookiestogetServerSideProps🎉 🙌 Should be available innext@10.0.4🚀@aperaham is this enough? Can this issue be closed now?