Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serverless: switching from getInitialProps to getServerSideProps causes "res.hasHeader is not a function" error #11223

Closed
zorzysty opened this issue Mar 20, 2020 · 3 comments

Comments

@zorzysty
Copy link

zorzysty commented Mar 20, 2020

Bug report

Describe the bug

Component using SSR works flawlessly on localhost both using getInitialProps and getServerSideProps .
It also works on serverless when using getInitialProps, but causes a 500 error on serverless when using getServerSideProps.

Here are the error logs:

ERROR	TypeError: res.hasHeader is not a function
    at sendPayload (/var/task/pages/[id].js:13625:41)
    at renderReqToHTML (/var/task/pages/[id].js:25515:13)
    at process._tickCallback (internal/process/next_tick.js:68:7)

Here's the code for getInitialProps:

Id.getInitialProps = async ({ query: { id } }) => {
  const url = `/${id}`

  const getData = async () => {
    try {
      const { status, user_id: userId, username } = await API.get(url)

      const user = { id: userId, name: username }

      return { status, user }
    } catch (err) {
      return { status: 'error' }
    }
  }

  const { status, user } = await getData()

  return {
    status,
    user,
  }
}

Here's the code for getServerSideProps:

export const getServerSideProps = async ({ query: { id } }) => {
  const url = `/${id}`

  const getData = async () => {
    try {
      const { status, user_id: userId, username } = await API.get(url)

      const user = { id: userId, name: username }

      return { status, user }
    } catch (err) {
      return { status: 'error' }
    }
  }

  const { status, user } = await getData()

  return {
    props: {
      status,
      user,
    },
  }
}

Screenshots

Here's what's actually being rendered
obraz

System information

  • AWS CloudFront
  • Node v 12.x
  • Nextjs v 9.3.1
  • serverless v 1.67.0
  • serverless-next.js v 1.67.0
a14m added a commit to infographicsgroup/serverless-next.js that referenced this issue Mar 25, 2020
When deploying to netlify or AWS (both depend on AWS-lambda)
The SSR components (using `getServerSideProps` fails with the following
error:
```
  ERROR	TypeError: res.hasHeader is not a function
    at sendPayload (/var/task/pages/test.js:5787:41)
    at renderReqToHTML (/var/task/pages/test.js:1773:13)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Module.render (/var/task/pages/test.js:1816:22)
```
This is caused by nextjs trying to use the default node `hasHeader`
method here: https://github.com/zeit/next.js/blob/v9.3.1/packages/next/next-server/server/send-payload.ts#L25

This commit fix the issue reported here: vercel/next.js#11223
which I could verify also happening on our stack by adding the method
that mimics the nodeJS method (using `res.getHeader(name)` internally wrapping it
in boolean response)
@a14m
Copy link
Contributor

a14m commented Mar 25, 2020

@zorzysty I had the same issue and digging deep I found the culprit to be next-aws-lambda not having the method (with the lambda wrapper)... I think this can be closed in favor of the other PR

@zorzysty
Copy link
Author

Thanks @a14m. This indeed looks like an issue with serverless-next.js and not next.js itself.
I'm closing the ticket.

danielcondemarin pushed a commit to serverless-nextjs/serverless-next.js that referenced this issue Mar 26, 2020
* Fix the issue with TypeError hasHeader is not a function

When deploying to netlify or AWS (both depend on AWS-lambda)
The SSR components (using `getServerSideProps` fails with the following
error:
```
  ERROR	TypeError: res.hasHeader is not a function
    at sendPayload (/var/task/pages/test.js:5787:41)
    at renderReqToHTML (/var/task/pages/test.js:1773:13)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Module.render (/var/task/pages/test.js:1816:22)
```
This is caused by nextjs trying to use the default node `hasHeader`
method here: https://github.com/zeit/next.js/blob/v9.3.1/packages/next/next-server/server/send-payload.ts#L25

This commit fix the issue reported here: vercel/next.js#11223
which I could verify also happening on our stack by adding the method
that mimics the nodeJS method (using `res.getHeader(name)` internally wrapping it
in boolean response)

* Add testing for `hasHeader` method
a14m added a commit to infographicsgroup/serverless-next.js that referenced this issue Apr 3, 2020
When deploying to netlify or AWS (both depend on AWS-lambda)
The SSR components (using `getServerSideProps` fails with the following
error:
```
  ERROR	TypeError: res.hasHeader is not a function
    at sendPayload (/var/task/pages/test.js:5787:41)
    at renderReqToHTML (/var/task/pages/test.js:1773:13)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Module.render (/var/task/pages/test.js:1816:22)
```
This is caused by nextjs trying to use the default node `hasHeader`
method here: https://github.com/zeit/next.js/blob/v9.3.1/packages/next/next-server/server/send-payload.ts#L25

This commit fix the issue reported here: vercel/next.js#11223
which I could verify also happening on our stack by adding the method
that mimics the nodeJS method (using `res.getHeader(name)` internally wrapping it
in boolean response)

Previously fixed for next-aws-lambda in serverless-nextjs#329
a14m added a commit to infographicsgroup/serverless-next.js that referenced this issue Apr 3, 2020
When deploying to netlify or AWS (both depend on AWS-lambda)
The SSR components (using `getServerSideProps` fails with the following
error:
```
  ERROR	TypeError: res.hasHeader is not a function
    at sendPayload (/var/task/pages/test.js:5787:41)
    at renderReqToHTML (/var/task/pages/test.js:1773:13)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Module.render (/var/task/pages/test.js:1816:22)
```
This is caused by nextjs trying to use the default node `hasHeader`
method here: https://github.com/zeit/next.js/blob/v9.3.1/packages/next/next-server/server/send-payload.ts#L25

This commit fix the issue reported here: vercel/next.js#11223
which I could verify also happening on our stack by adding the method
that mimics the nodeJS method (using `res.getHeader(name)` internally wrapping it
in boolean response)

Previously fixed for next-aws-lambda in serverless-nextjs#329
danielcondemarin pushed a commit to serverless-nextjs/serverless-next.js that referenced this issue Apr 4, 2020
…t a function (#342)

* Fix the issue with TypeError hasHeader is not a function

When deploying to netlify or AWS (both depend on AWS-lambda)
The SSR components (using `getServerSideProps` fails with the following
error:
```
  ERROR	TypeError: res.hasHeader is not a function
    at sendPayload (/var/task/pages/test.js:5787:41)
    at renderReqToHTML (/var/task/pages/test.js:1773:13)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Module.render (/var/task/pages/test.js:1816:22)
```
This is caused by nextjs trying to use the default node `hasHeader`
method here: https://github.com/zeit/next.js/blob/v9.3.1/packages/next/next-server/server/send-payload.ts#L25

This commit fix the issue reported here: vercel/next.js#11223
which I could verify also happening on our stack by adding the method
that mimics the nodeJS method (using `res.getHeader(name)` internally wrapping it
in boolean response)

Previously fixed for next-aws-lambda in #329

* Fix the headers to always work with lower case

Previously the following behaviour could be observed:
```
res.setHeader("X-CUSTOM", "VAL")
res.getHeader("X-CUSTOM") => null
res.hasHeader("X-CUSTOM") => false

res.setHeader("x-custom", "val")
res.getHeader("X-CUSTOM") => "val"
res.hasHeader("X-CUSTOM") => true
```
There is no way of accessing a header that isn't set in lower case,
This fixes the issue by always storing the header name as lower case,
allowing a similar behaviour to node HTTP ResponseObject behaviour
 - https://nodejs.org/api/http.html#http_request_getheader_name
 - https://nodejs.org/api/http.html#http_request_removeheader_name
 - https://nodejs.org/api/http.html#http_request_setheader_name_value

Previously fixed for next-aws-lambda in #331

* Update the npm package-lock
sclaughl pushed a commit to sclaughl/serverless-next.js that referenced this issue Jul 16, 2020
…nextjs#329)

* Fix the issue with TypeError hasHeader is not a function

When deploying to netlify or AWS (both depend on AWS-lambda)
The SSR components (using `getServerSideProps` fails with the following
error:
```
  ERROR	TypeError: res.hasHeader is not a function
    at sendPayload (/var/task/pages/test.js:5787:41)
    at renderReqToHTML (/var/task/pages/test.js:1773:13)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Module.render (/var/task/pages/test.js:1816:22)
```
This is caused by nextjs trying to use the default node `hasHeader`
method here: https://github.com/zeit/next.js/blob/v9.3.1/packages/next/next-server/server/send-payload.ts#L25

This commit fix the issue reported here: vercel/next.js#11223
which I could verify also happening on our stack by adding the method
that mimics the nodeJS method (using `res.getHeader(name)` internally wrapping it
in boolean response)

* Add testing for `hasHeader` method
sclaughl pushed a commit to sclaughl/serverless-next.js that referenced this issue Jul 16, 2020
…t a function (serverless-nextjs#342)

* Fix the issue with TypeError hasHeader is not a function

When deploying to netlify or AWS (both depend on AWS-lambda)
The SSR components (using `getServerSideProps` fails with the following
error:
```
  ERROR	TypeError: res.hasHeader is not a function
    at sendPayload (/var/task/pages/test.js:5787:41)
    at renderReqToHTML (/var/task/pages/test.js:1773:13)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Module.render (/var/task/pages/test.js:1816:22)
```
This is caused by nextjs trying to use the default node `hasHeader`
method here: https://github.com/zeit/next.js/blob/v9.3.1/packages/next/next-server/server/send-payload.ts#L25

This commit fix the issue reported here: vercel/next.js#11223
which I could verify also happening on our stack by adding the method
that mimics the nodeJS method (using `res.getHeader(name)` internally wrapping it
in boolean response)

Previously fixed for next-aws-lambda in serverless-nextjs#329

* Fix the headers to always work with lower case

Previously the following behaviour could be observed:
```
res.setHeader("X-CUSTOM", "VAL")
res.getHeader("X-CUSTOM") => null
res.hasHeader("X-CUSTOM") => false

res.setHeader("x-custom", "val")
res.getHeader("X-CUSTOM") => "val"
res.hasHeader("X-CUSTOM") => true
```
There is no way of accessing a header that isn't set in lower case,
This fixes the issue by always storing the header name as lower case,
allowing a similar behaviour to node HTTP ResponseObject behaviour
 - https://nodejs.org/api/http.html#http_request_getheader_name
 - https://nodejs.org/api/http.html#http_request_removeheader_name
 - https://nodejs.org/api/http.html#http_request_setheader_name_value

Previously fixed for next-aws-lambda in serverless-nextjs#331

* Update the npm package-lock
@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants