2022-09-08 00:30:04 -06:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const assert = require('assert');
|
|
|
|
|
|
|
|
const { getSerializableSpans } = require('../tracing/tracing-middleware');
|
|
|
|
const renderHydrogenToString = require('../hydrogen-render/render-hydrogen-to-string');
|
|
|
|
const sanitizeHtml = require('../lib/sanitize-html');
|
|
|
|
const safeJson = require('../lib/safe-json');
|
|
|
|
|
2023-04-19 12:48:12 -06:00
|
|
|
async function renderHydrogenVmRenderScriptToPageHtml({
|
|
|
|
pageOptions,
|
2022-09-08 00:30:04 -06:00
|
|
|
vmRenderScriptFilePath,
|
|
|
|
vmRenderContext,
|
2023-04-19 12:48:12 -06:00
|
|
|
}) {
|
2022-09-08 00:30:04 -06:00
|
|
|
assert(vmRenderScriptFilePath);
|
|
|
|
assert(vmRenderContext);
|
|
|
|
assert(pageOptions);
|
|
|
|
assert(pageOptions.title);
|
|
|
|
assert(pageOptions.styles);
|
|
|
|
assert(pageOptions.scripts);
|
2022-10-19 11:07:39 -06:00
|
|
|
assert(pageOptions.cspNonce);
|
2022-09-08 00:30:04 -06:00
|
|
|
|
|
|
|
const hydrogenHtmlOutput = await renderHydrogenToString({
|
|
|
|
vmRenderScriptFilePath,
|
|
|
|
vmRenderContext,
|
2022-10-19 11:07:39 -06:00
|
|
|
pageOptions,
|
2022-09-08 00:30:04 -06:00
|
|
|
});
|
|
|
|
|
2022-11-09 17:57:33 -07:00
|
|
|
// Serialize the state for when we run the Hydrogen render again client-side to
|
|
|
|
// re-hydrate the DOM
|
|
|
|
const serializedMatrixPublicArchiveContext = JSON.stringify({
|
|
|
|
...vmRenderContext,
|
|
|
|
});
|
|
|
|
|
2022-09-08 00:30:04 -06:00
|
|
|
const serializableSpans = getSerializableSpans();
|
|
|
|
const serializedSpans = JSON.stringify(serializableSpans);
|
|
|
|
|
2022-09-08 18:15:07 -06:00
|
|
|
// We shouldn't let some pages be indexed by search engines
|
|
|
|
let maybeNoIndexHtml = '';
|
2022-09-20 15:02:09 -06:00
|
|
|
if (!pageOptions.shouldIndex) {
|
2022-09-08 18:15:07 -06:00
|
|
|
maybeNoIndexHtml = `<meta name="robots" content="noindex, nofollow" />`;
|
|
|
|
}
|
|
|
|
|
2022-09-08 00:30:04 -06:00
|
|
|
const pageHtml = `
|
|
|
|
<!doctype html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
2022-09-08 18:15:07 -06:00
|
|
|
${maybeNoIndexHtml}
|
2022-09-08 00:30:04 -06:00
|
|
|
${sanitizeHtml(`<title>${pageOptions.title}</title>`)}
|
2022-11-11 13:50:41 -07:00
|
|
|
<link rel="icon" href="/img/favicon.ico" sizes="any">
|
|
|
|
<link rel="icon" href="/img/favicon.svg" type="image/svg+xml">
|
2022-09-08 00:30:04 -06:00
|
|
|
${pageOptions.styles
|
2022-10-19 11:07:39 -06:00
|
|
|
.map(
|
|
|
|
(styleUrl) =>
|
|
|
|
`<link href="${styleUrl}" rel="stylesheet" nonce="${pageOptions.cspNonce}">`
|
|
|
|
)
|
2022-09-08 00:30:04 -06:00
|
|
|
.join('\n')}
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
${hydrogenHtmlOutput}
|
2022-11-09 17:57:33 -07:00
|
|
|
|
|
|
|
${
|
|
|
|
/**
|
|
|
|
* This inline snippet is used in to scroll the Hydrogen timeline to the
|
|
|
|
* right place immediately when the page loads instead of waiting for
|
|
|
|
* Hydrogen to load, hydrate and finally scroll.
|
|
|
|
*/ ''
|
|
|
|
}
|
|
|
|
<script type="text/javascript" nonce="${pageOptions.cspNonce}">
|
|
|
|
const qs = new URLSearchParams(window?.location?.search);
|
|
|
|
const atEventId = qs.get('at');
|
|
|
|
if (atEventId) {
|
|
|
|
const el = document.querySelector(\`[data-event-id="\${atEventId}"]\`);
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
el && el.scrollIntoView({ block: 'center' });
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const el = document.querySelector('.js-bottom-scroll-anchor');
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
el && el.scrollIntoView({ block: 'end' });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<script type="text/javascript" nonce="${pageOptions.cspNonce}">
|
|
|
|
window.matrixPublicArchiveContext = ${safeJson(serializedMatrixPublicArchiveContext)}
|
|
|
|
</script>
|
|
|
|
|
2022-09-08 00:30:04 -06:00
|
|
|
${pageOptions.scripts
|
2022-10-19 11:07:39 -06:00
|
|
|
.map(
|
|
|
|
(scriptUrl) =>
|
|
|
|
`<script type="text/javascript" src="${scriptUrl}" nonce="${pageOptions.cspNonce}"></script>`
|
|
|
|
)
|
2022-09-08 00:30:04 -06:00
|
|
|
.join('\n')}
|
2022-10-19 11:07:39 -06:00
|
|
|
<script type="text/javascript" nonce="${pageOptions.cspNonce}">
|
|
|
|
window.tracingSpansForRequest = ${safeJson(serializedSpans)};
|
|
|
|
</script>
|
2022-09-08 00:30:04 -06:00
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`;
|
|
|
|
|
|
|
|
return pageHtml;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = renderHydrogenVmRenderScriptToPageHtml;
|