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

Commit 8f3ad77

Browse files
committed
Netlify Function Handler: Use promise rather than callback
Use the promise approach of next-aws-lambda rather than the callback approach, because it makes the code easier to read and puts it in the correct order of execution.
1 parent 9b0e645 commit 8f3ad77

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

lib/templates/netlifyFunction.js

+15-25
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,7 @@ const compat = require("./compat");
66
// Load the NextJS page
77
const page = require("./nextJsPage");
88

9-
// next-aws-lambda is made for AWS. There are some minor differences between
10-
// Netlify and AWS which we resolve here.
11-
const callbackHandler = (callback) =>
12-
// The callbackHandler wraps the callback
13-
(argument, response) => {
14-
// Convert header values to string. Netlify does not support integers as
15-
// header values. See: https://github.com/netlify/cli/issues/451
16-
Object.keys(response.multiValueHeaders).forEach((key) => {
17-
response.multiValueHeaders[key] = response.multiValueHeaders[
18-
key
19-
].map((value) => String(value));
20-
});
21-
22-
response.multiValueHeaders["Cache-Control"] = ["no-cache"];
23-
24-
// Invoke callback
25-
callback(argument, response);
26-
};
27-
28-
exports.handler = (event, context, callback) => {
9+
exports.handler = async (event, context, callback) => {
2910
// x-forwarded-host is undefined on Netlify for proxied apps that need it
3011
// fixes https://github.com/netlify/next-on-netlify/issues/46
3112
if (!event.multiValueHeaders.hasOwnProperty("x-forwarded-host")) {
@@ -37,15 +18,24 @@ exports.handler = (event, context, callback) => {
3718
console.log("[request]", path);
3819

3920
// Render the page
40-
compat(page)(
21+
const response = await compat(page)(
4122
{
4223
...event,
4324
// Required. Otherwise, compat() will complain
4425
requestContext: {},
4526
},
46-
context,
47-
// Wrap the Netlify callback, so that we can resolve differences between
48-
// Netlify and AWS (which next-aws-lambda optimizes for)
49-
callbackHandler(callback)
27+
context
5028
);
29+
30+
// Convert header values to string. Netlify does not support integers as
31+
// header values. See: https://github.com/netlify/cli/issues/451
32+
Object.keys(response.multiValueHeaders).forEach((key) => {
33+
response.multiValueHeaders[key] = response.multiValueHeaders[
34+
key
35+
].map((value) => String(value));
36+
});
37+
38+
response.multiValueHeaders["Cache-Control"] = ["no-cache"];
39+
40+
callback(null, response);
5141
};

0 commit comments

Comments
 (0)