-
-
Notifications
You must be signed in to change notification settings - Fork 519
Expand file tree
/
Copy pathindex.jsx
More file actions
executable file
·59 lines (46 loc) · 2.25 KB
/
Copy pathindex.jsx
File metadata and controls
executable file
·59 lines (46 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import * as preact from 'preact';
import * as hooks from 'preact/hooks';
import { hydrate, prerender as ssr } from 'preact-iso';
import App from './components/app';
import './analytics';
import './style/index.css';
// allows users to play with preact in the browser developer console
globalThis.preact = { ...preact, ...hooks };
if (typeof window !== 'undefined') {
hydrate(<App />, document.getElementById('app'));
// Might need to keep this around indefinitely, unfortunately
if ('serviceWorker' in navigator) {
navigator.serviceWorker.getRegistrations().then(registrations => {
for (const registration of registrations) {
registration.unregister();
}
});
}
}
let initialized = false;
export async function prerender() {
const init = async () => {
globalThis.prismWorker = await import('./components/code-block/prism.worker.js');
// DOMParser polyfill for `preact-markup`
const { DOMParser } = await import('@xmldom/xmldom');
globalThis.DOMParser = DOMParser;
// fetch latest release data
const { default: releaseLambda } = await import('./lambda/release.js');
const { version, url } = await (await releaseLambda()).json();
globalThis.prerenderPreactVersion = version;
globalThis.prerenderPreactReleaseUrl = url;
initialized = true;
};
if (!initialized) await init();
const res = await ssr(<App />);
const elements = new Set([
{ type: 'meta', props: { name: 'description', content: globalThis.description } },
{ type: 'meta', props: { property: 'og:url', content: `${import.meta.env.NETLIFY_DEPLOY_URL}${location.pathname}` } },
{ type: 'meta', props: { property: 'og:title', content: globalThis.title } },
{ type: 'meta', props: { property: 'og:description', content: globalThis.description } },
location.pathname.includes('/v8/') && { type: 'meta', props: { name: 'robots', content: 'noindex' } },
process.env.BRANCH && { type: 'script', children: `ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga('set','dimension1','${process.env.BRANCH}');onerror=function(e,f,l,c){ga('send','event','exception',e,f+':'+l+':'+c)}` }
].filter(Boolean));
res.html += '<script async defer src="https://www.google-analytics.com/analytics.js"></script>';
return { ...res, head: { lang: 'en', title: globalThis.title, elements } };
}