Skip to content
This repository has been archived by the owner on Jan 28, 2025. It is now read-only.

Commit

Permalink
fix(serverless-next.js): Fix the issue with TypeError hasHeader is no…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
a14m authored Apr 4, 2020
1 parent 217513a commit 20f42fd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,47 @@ describe("Response Tests", () => {
});
});

it("hasHeader", () => {
const { res } = create({
request: {
path: "/",
headers: {}
}
});
res.setHeader("x-custom-1", "1");

expect(res.hasHeader("x-custom-1")).toBe(true);
expect(res.hasHeader("x-custom-2")).toBe(false);
});

it("case insensitive headers", () => {
const { res } = create({
request: {
path: "/",
headers: {}
}
});
res.setHeader("x-custom-1", "1");
res.setHeader("X-CUSTOM-2", "2");
res.setHeader("X-cUsToM-3", "3");

expect(res.getHeader("X-CUSTOM-1")).toEqual("1");
expect(res.getHeader("x-custom-2")).toEqual("2");
expect(res.getHeader("x-CuStOm-3")).toEqual("3");

expect(res.getHeaders()).toEqual({
"x-custom-1": "1",
"x-custom-2": "2",
"x-custom-3": "3"
});

res.removeHeader("X-CUSTOM-1");
res.removeHeader("x-custom-2");
res.removeHeader("x-CUSTom-3");

expect(res.getHeaders()).toEqual({});
});

it(`res.write('ok')`, () => {
const { res, responsePromise } = create({
request: {
Expand Down
7 changes: 5 additions & 2 deletions packages/serverless-nextjs-component/next-aws-cloudfront.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,20 @@ const handler = event => {
});

res.setHeader = (name, value) => {
res.headers[name] = value;
res.headers[name.toLowerCase()] = value;
};
res.removeHeader = name => {
delete res.headers[name];
delete res.headers[name.toLowerCase()];
};
res.getHeader = name => {
return res.headers[name.toLowerCase()];
};
res.getHeaders = () => {
return res.headers;
};
res.hasHeader = name => {
return !!res.getHeader(name);
};

return {
req,
Expand Down
6 changes: 3 additions & 3 deletions packages/serverless-nextjs-component/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 20f42fd

Please sign in to comment.