question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Apply API middlewares to getServerSideProps

See original GitHub issue

Feature 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:closed
  • Created 4 years ago
  • Reactions:31
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
emcommented, Sep 29, 2021

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.

2reactions
karlhorkycommented, Dec 24, 2020

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.cookies to getServerSideProps 🎉 🙌 Should be available in next@10.0.4 🚀

@aperaham is this enough? Can this issue be closed now?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found