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

Commit ee88a68

Browse files
authored
fix(core, lambda-at-edge): render page to HTML properly for next.js 11.1+ versions (#1596)
1 parent c3789b4 commit ee88a68

File tree

17 files changed

+3589
-181
lines changed

17 files changed

+3589
-181
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"jest": "^27.0.4",
6666
"lerna": "^4.0.0",
6767
"lint-staged": "^11.1.2",
68-
"next": "^10.2.3",
68+
"next": "^11.1.0",
6969
"prettier": "^2.0.5",
7070
"react": "^17.0.2",
7171
"react-dom": "^17.0.2",

packages/libs/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"@hapi/accept": "^5.0.1",
3636
"cookie": "^0.4.1",
3737
"jsonwebtoken": "^8.5.1",
38+
"next": "^11.1.0",
3839
"path-to-regexp": "^6.1.0",
3940
"regex-parser": "^2.2.10"
4041
},

packages/libs/core/src/handle/fallback.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
StaticRoute
1111
} from "../types";
1212
import { notFoundPage } from "../route/notfound";
13+
import { renderPageToHtml } from "../utils/renderUtils";
1314

1415
type FallbackRoute = StaticRoute & {
1516
fallback: string | null;
@@ -49,7 +50,8 @@ const renderFallback = async (
4950

5051
const page = getPage(route.page);
5152
try {
52-
const { html, renderOpts } = await page.renderReqToHTML(
53+
const { html, renderOpts } = await renderPageToHtml(
54+
page,
5355
req,
5456
res,
5557
"passthrough"

packages/libs/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from "./handle";
33
export * from "./revalidate";
44
export * from "./route";
55
export * from "./types";
6+
export * from "./utils";

packages/libs/core/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./renderUtils";
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { resultToChunks } from "next/dist/server/utils";
2+
import { IncomingMessage, ServerResponse } from "http";
3+
4+
/**
5+
* Render to HTML helper. Starting in Next.js 11.1 a change was introduced so renderReqToHTML no longer returns a string.
6+
* See: https://github.com/vercel/next.js/pull/27319
7+
* This is a helper to properly render it in backwards compatible way.
8+
* @param page
9+
* @param req
10+
* @param res
11+
* @param renderMode
12+
*/
13+
export const renderPageToHtml = async (
14+
page: {
15+
renderReqToHTML: (
16+
req: IncomingMessage,
17+
res: ServerResponse,
18+
renderMode: string | boolean
19+
) =>
20+
| PromiseLike<{ renderOpts: Record<string, any>; html: string }>
21+
| { renderOpts: Record<string, any>; html: string };
22+
},
23+
req: IncomingMessage,
24+
res: ServerResponse,
25+
renderMode: "export" | "passthrough" | true
26+
): Promise<{ html: string; renderOpts: Record<string, any> }> => {
27+
const { renderOpts, html: htmlResult } = await page.renderReqToHTML(
28+
req,
29+
res,
30+
renderMode
31+
);
32+
33+
let html;
34+
if (typeof htmlResult === "string") {
35+
html = htmlResult;
36+
} else {
37+
const htmlChunks = htmlResult ? await resultToChunks(htmlResult) : [];
38+
html = htmlChunks.join("");
39+
}
40+
41+
return { html, renderOpts };
42+
};

0 commit comments

Comments
 (0)