-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathserver.ts
More file actions
97 lines (78 loc) · 2.74 KB
/
server.ts
File metadata and controls
97 lines (78 loc) · 2.74 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
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
import {
AngularNodeAppEngine,
createNodeRequestHandler,
isMainModule,
writeResponseToNodeResponse,
} from '@angular/ssr/node';
import express from 'express';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
const serverDistFolder = dirname(fileURLToPath(import.meta.url));
const browserDistFolder = resolve(serverDistFolder, '../browser');
const app = express();
const angularApp = new AngularNodeAppEngine();
const isBot = (ua: string) => /bot|googlebot|crawler|spider|robot|crawling/i.test(ua);
app.use(
express.static(browserDistFolder, {
maxAge: '1y',
index: false,
redirect: false,
})
);
app.use((req, res, next) => {
const startTime = performance.now();
const userAgent = req.headers['user-agent'] || '';
const isSearchBot = isBot(userAgent);
const url = req.originalUrl;
const isStaticFile = url.includes('/static/') || url.includes('/assets/');
const isSystemFile = url.includes('.well-known') || url.endsWith('.json') || url.endsWith('.ico');
if (isStaticFile || isSystemFile) {
return angularApp
.handle(req)
.then((response) => (response ? writeResponseToNodeResponse(response, res) : next()))
.catch(next);
}
return angularApp
.handle(req)
.then((response) => {
if (!response) return next();
const ttfb = performance.now() - startTime;
const responseForUser = response.clone();
writeResponseToNodeResponse(responseForUser, res);
setImmediate(async () => {
try {
let isComplete = true;
const shouldSample = isSearchBot || Math.random() < 0.05;
if (shouldSample && response.status === 200) {
const html = await response.text();
const hasTitle = html.includes('<title');
const hasAppRootClosed = html.includes('</osf-root>');
const isEmptyApp = html.includes('<osf-root></osf-root>');
isComplete = hasTitle && hasAppRootClosed && !isEmptyApp;
}
const body = {
url: req.originalUrl,
status: response.status,
ttfb: Math.round(ttfb),
isBot: isSearchBot,
isComplete: isComplete,
timestamp: new Date().toISOString(),
};
// eslint-disable-next-line no-console
console.log(body);
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
}
});
})
.catch(next);
});
if (isMainModule(import.meta.url)) {
const port = process.env['PORT'] || 4000;
app.listen(port, () => {
// eslint-disable-next-line no-console
console.log(`Node Express server listening on http://localhost:${port}`);
});
}
export const reqHandler = createNodeRequestHandler(app);