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

Commit 224ad89

Browse files
committed
Refactor: Replace compat.js with renderNextPage.js
Wrap the logic for rendering the Next.js page into its own function. That keeps the netlifyFunction.js (function handler) minimal and easy to read. It will also make it easier for users to modify the function handler while keeping the render function unchanged (down the line, once we support this feature).
1 parent 8f3ad77 commit 224ad89

File tree

4 files changed

+39
-37
lines changed

4 files changed

+39
-37
lines changed

lib/helpers/setupNetlifyFunctionForPage.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ const setupNetlifyFunctionForPage = ({ filePath, functionsPath }) => {
2424
});
2525

2626
// Copy function helpers
27-
["compat.js", "reqResMapper.js"].forEach((helper) => {
27+
["renderNextPage.js", "reqResMapper.js"].forEach((helper) => {
2828
copySync(join(TEMPLATES_DIR, helper), join(functionDirectory, helper), {
2929
overwrite: false,
3030
errorOnExist: true,
3131
});
3232
});
3333

3434
// Copy page
35-
const nextPageCopyPath = join(functionDirectory, "nextJsPage.js");
35+
const nextPageCopyPath = join(functionDirectory, "nextPage.js");
3636
copySync(join(NEXT_DIST_DIR, "serverless", filePath), nextPageCopyPath, {
3737
overwrite: false,
3838
errorOnExist: true,

lib/templates/compat.js

-22
This file was deleted.

lib/templates/netlifyFunction.js

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// TEMPLATE: This file will be copied to the Netlify functions directory when
22
// running next-on-netlify
33

4-
// Compatibility wrapper for NextJS page
5-
const compat = require("./compat");
6-
// Load the NextJS page
7-
const page = require("./nextJsPage");
4+
// Render function for the Next.js page
5+
const renderNextPage = require("./renderNextPage");
86

97
exports.handler = async (event, context, callback) => {
108
// x-forwarded-host is undefined on Netlify for proxied apps that need it
@@ -17,15 +15,12 @@ exports.handler = async (event, context, callback) => {
1715
const { path } = event;
1816
console.log("[request]", path);
1917

20-
// Render the page
21-
const response = await compat(page)(
22-
{
23-
...event,
24-
// Required. Otherwise, compat() will complain
25-
requestContext: {},
26-
},
27-
context
28-
);
18+
// Render the Next.js page
19+
const response = await renderNextPage({
20+
...event,
21+
// Required. Otherwise, reqResMapper will complain
22+
requestContext: {},
23+
});
2924

3025
// Convert header values to string. Netlify does not support integers as
3126
// header values. See: https://github.com/netlify/cli/issues/451

lib/templates/renderNextPage.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Load the NextJS page
2+
const nextPage = require("./nextPage");
3+
const reqResMapper = require("./reqResMapper");
4+
5+
// Render the Next.js page
6+
const renderNextPage = (event) => {
7+
// The Next.js page is rendered inside a promise that is resolved when the
8+
// Next.js page ends the response via `res.end()`
9+
const promise = new Promise((resolve) => {
10+
// Create a Next.js-compatible request and response object
11+
// These mock the ClientRequest and ServerResponse classes from node http
12+
// See: https://nodejs.org/api/http.html
13+
const callback = (_null, response) => resolve(response);
14+
const { req, res } = reqResMapper(event, callback);
15+
16+
// Check if page is a Next.js page or an API route
17+
const isNextPage = nextPage.render instanceof Function;
18+
const isApiRoute = !isNextPage;
19+
20+
// Perform the render: render() for Next.js page or default() for API route
21+
if (isNextPage) return nextPage.render(req, res);
22+
if (isApiRoute) return nextPage.default(req, res);
23+
});
24+
25+
// Return the promise
26+
return promise;
27+
};
28+
29+
module.exports = renderNextPage;

0 commit comments

Comments
 (0)