2024-02-22 12:14:58 +00:00
|
|
|
import ReactDOMServer from "react-dom/server";
|
|
|
|
import { escapeInject, dangerouslySkipEscape } from "vike/server";
|
|
|
|
import type { OnRenderHtmlAsync } from "vike/types";
|
|
|
|
import { getPageMetadata } from "./utils";
|
2024-02-23 19:36:10 +07:00
|
|
|
import App from "./app";
|
2024-02-22 12:14:58 +00:00
|
|
|
|
|
|
|
export const onRenderHtml: OnRenderHtmlAsync = async (
|
|
|
|
pageContext
|
|
|
|
): ReturnType<OnRenderHtmlAsync> => {
|
|
|
|
const { Page } = pageContext;
|
|
|
|
|
|
|
|
if (!Page)
|
|
|
|
throw new Error(
|
|
|
|
"My onRenderHtml() hook expects pageContext.Page to be defined"
|
|
|
|
);
|
|
|
|
|
|
|
|
const page = ReactDOMServer.renderToString(
|
2024-02-23 19:36:10 +07:00
|
|
|
<App pageContext={pageContext}>
|
2024-02-22 12:14:58 +00:00
|
|
|
<Page />
|
2024-02-23 19:36:10 +07:00
|
|
|
</App>
|
2024-02-22 12:14:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// See https://vike.dev/head
|
2024-03-03 18:55:51 +07:00
|
|
|
const metadata = getPageMetadata(pageContext);
|
2024-02-22 12:14:58 +00:00
|
|
|
|
|
|
|
const documentHtml = escapeInject`<!DOCTYPE html>
|
|
|
|
<html lang="en" class="dark">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8" />
|
|
|
|
<link rel="icon" href="/favicon.ico" />
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
2024-03-03 18:55:51 +07:00
|
|
|
${dangerouslySkipEscape(metadata)}
|
2024-02-22 12:14:58 +00:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="react-root">${dangerouslySkipEscape(page)}</div>
|
|
|
|
</body>
|
|
|
|
</html>`;
|
|
|
|
|
|
|
|
return {
|
|
|
|
documentHtml,
|
|
|
|
pageContext: {},
|
|
|
|
};
|
|
|
|
};
|