Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit 2ab1154

Browse files
committed
Support custom headers for SSR pages and API endpoints
There are some minor differences between Netlify and AWS, such as AWS having support for multi-value headers. next-aws-lambda is made for AWS, so we need to resolve those differences to make everything work with Netlify. We support headers in SSR pages and API endpoints by converting the first entry in each multi-value header into a plain header. Netlify also requires all header values to be in string format, so we take care of that, too.
1 parent 2d30782 commit 2ab1154

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

lib/routerTemplate.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,34 @@ const getRoute = path => {
2424
return route || { file: "pages/_error.js" }
2525
}
2626

27+
// There are some minor differences between Netlify and AWS, such as AWS having
28+
// support for multi-value headers.
29+
// next-aws-lambda is made for AWS, so we need to resolve those differences to
30+
// make everything work with Netlify.
31+
const callbackHandler = callback => (
32+
// The callbackHandler wraps the callback
33+
(argument, response) => {
34+
35+
// Convert multi-value headers to plain headers, because Netlify does not
36+
// support multi-value headers.
37+
// See: https://github.com/netlify/cli/issues/923
38+
response.headers = {}
39+
Object.keys(response.multiValueHeaders).forEach(key => {
40+
response.headers[key] = response.multiValueHeaders[key][0]
41+
})
42+
delete response.multiValueHeaders
43+
44+
// Convert header values to string. Netlify does not support integers as
45+
// header values. See: https://github.com/netlify/cli/issues/451
46+
Object.keys(response.headers).forEach(key => {
47+
response.headers[key] = String(response.headers[key])
48+
})
49+
50+
// Invoke callback
51+
callback(argument, response)
52+
}
53+
)
54+
2755
exports.handler = (event, context, callback) => {
2856
// Get the request URL
2957
const { path } = event
@@ -50,6 +78,8 @@ exports.handler = (event, context, callback) => {
5078
multiValueQueryStringParameters: event.queryStringParameters
5179
},
5280
context,
53-
callback
81+
// Wrap the Netlify callback, so that we can resolve differences between
82+
// Netlify and AWS (which next-aws-lambda optimizes for)
83+
callbackHandler(callback)
5484
)
5585
};

0 commit comments

Comments
 (0)