-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
_document.tsx
180 lines (163 loc) · 5.62 KB
/
_document.tsx
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { ServerResponse } from 'http';
import React from 'react';
import { renderToNodeStream } from 'react-dom/server';
import Index from './index';
import Result from './result';
import Compare from './compare';
import NotFound from './404';
import ServerError from './500';
import ParseFailure from './parse-failure';
import { getResultProps } from '../page-props/results';
import { getCompareProps } from '../page-props/compare';
import { containerId, pages, hostname } from '../util/constants';
import OctocatCorner from '../components/OctocatCorner';
const existingPaths = new Set(Object.values(pages));
const logoSize = 108;
const title = 'Package Phobia';
const description = 'Find the cost of installing a node dependency';
const css = `
body {
margin: 0;
padding: 0;
background: #fafafa;
font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Fira Sans,Droid Sans,Helvetica Neue,sans-serif;
}
*,
*::before,
*::after {
box-sizing: border-box;
}
#spinner {
background: url(${pages.logo_svg});
box-sizing: border-box;
height: ${logoSize}px;
width: ${logoSize}px;
margin-top: calc(50vh - ${logoSize / 2}px);
margin-left: calc(50vw - ${logoSize / 2}px);
animation: rotate 1s infinite linear;
}
@keyframes rotate {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.content-container {
display: flex;
}
.stats-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin: 0 3rem;
}
@media screen and (max-width: 48em) {
.content-container {
flex-wrap: wrap;
}
.stats-container {
margin: 0 auto;
}
.bar-graph-container {
margin: 0 auto;
}
}
#codefund { margin-top: 1em; }
@media screen and (min-width: 75em) {
#codefund {
position: absolute;
top: 5vh;
left: 1vw;
}
}`;
export async function renderPage(
res: ServerResponse,
pathname: string,
query: ParsedUrlQuery,
tmpDir: string,
gaId: string,
) {
res.statusCode = getStatusCode(pathname);
res.write(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>${title}</title>
<meta name="description" content="${description}">
<style>${css}</style>
${
pathname === pages.result
? '<link rel="preconnect" href="https://badgen.net">'
: ''
}
<link rel="apple-touch-icon" href="/apple-touch-icon.png" sizes="180x180">
<link rel="icon" href="/favicon-32x32.png" sizes="32x32" type="image/png">
<link rel="icon" href="/favicon-16x16.png" sizes="16x16" type="image/png">
<link rel="manifest" href="/site.webmanifest">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#33aa33">
<meta name="msapplication-TileColor" content="#333333">
<meta name="theme-color" content="#333333">
<meta property="og:title" content="${title}">
<meta property="og:image" content="https://${hostname}${pages.logo_png}">
<meta property="og:description" content="${description}">
</head>
<body>
<div id="spinner"></div>
<script>document.getElementById('spinner').style.display='block'</script>
${OctocatCorner()}
<div id="${containerId}">`);
const factory = await routePage(pathname, query, tmpDir);
const stream = renderToNodeStream(factory);
stream.pipe(res, { end: false });
stream.on('end', () => {
res.end(`</div>
<script>
const input = document.querySelector('input[type=file]');
input.onchange = () => input.form.submit();
</script>
<script>document.getElementById('spinner').style.display='none'</script>
<script>
if (window.location.hostname === '${hostname}') {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '${gaId}', 'auto');
ga('send', 'pageview');
}
</script>
</body>
</html>`);
});
}
async function routePage(pathname: string, query: ParsedUrlQuery, tmpDir: string) {
try {
switch (pathname) {
case pages.index:
return <Index />;
case pages.parseFailure:
return <ParseFailure />;
case pages.result:
return (query.p || '').includes(',') ? (
<Compare {...await getCompareProps(query, tmpDir)} />
) : (
<Result {...await getResultProps(query, tmpDir)} />
);
default:
return <NotFound />;
}
} catch (e) {
console.error(`ERROR: ${e.message}`);
return <ServerError />;
}
}
function getStatusCode(pathname: string) {
if (existingPaths.has(pathname)) {
return 200;
}
return 404;
}