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

Commit c9980d8

Browse files
authored
fix(core): fix renderReqToHtml function for next.js 12 (#1971)
1 parent e7c81ef commit c9980d8

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export const renderPageToHtml = async (
1717
res: ServerResponse,
1818
renderMode?: "export" | "passthrough" | true
1919
) =>
20-
| PromiseLike<{ renderOpts: Record<string, any>; html: string }>
21-
| { renderOpts: Record<string, any>; html: string };
20+
| PromiseLike<{ renderOpts: Record<string, any>; html: any }>
21+
| { renderOpts: Record<string, any>; html: any };
2222
},
2323
req: IncomingMessage,
2424
res: ServerResponse,
@@ -30,24 +30,24 @@ export const renderPageToHtml = async (
3030
renderMode
3131
);
3232

33-
let html;
34-
try {
35-
if (typeof htmlResult === "string") {
36-
html = htmlResult; // Next.js < 11.1
37-
} else {
38-
html = htmlResult ? await resultsToString([htmlResult]) : ""; // Next >= 11.1.1
33+
let html = undefined;
34+
if (typeof htmlResult === "string") {
35+
html = htmlResult; // Next.js < 11.1
36+
} else {
37+
if (htmlResult) {
38+
html = await htmlResult.toUnchunkedString?.(); // Next >= 12
39+
40+
if (!html) {
41+
try {
42+
html = await resultsToString([htmlResult]); // Next >= 11.1.1
43+
} catch (e) {
44+
console.log("html could not be rendered using resultsToString().");
45+
}
46+
}
3947
}
40-
} catch (e) {
41-
// Fallback to using renderReqToHtml without renderMode specified,
42-
// which will render html based on the page's renderReqToHtml,
43-
// which should always work (but adds another *warm* render cost)
44-
console.log(
45-
"Exception occurred, falling back to using page's rendering function for html"
46-
);
47-
html = (await page.renderReqToHTML(req, res)) as unknown as string;
4848
}
4949

50-
if (!html || html === "") {
50+
if (!html) {
5151
console.log(
5252
"html is empty, falling back to using page's rendering function for html"
5353
);

0 commit comments

Comments
 (0)