-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
78 lines (61 loc) · 2.01 KB
/
server.ts
File metadata and controls
78 lines (61 loc) · 2.01 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
import fs from 'node:fs/promises';
import path from 'node:path';
import express from 'express';
import { createServer as createViteServer } from 'vite';
import { getPosts } from './src/lib/getPosts';
import rss from './src/routers/rss';
import sitemap from './src/routers/sitemap';
const PORT = 1337;
const createServer = async () => {
const app = express();
const vite = await createViteServer({
server: {
middlewareMode: true,
watch: {
ignored: /public\//,
},
},
appType: 'custom',
});
app.use(vite.middlewares);
const titles = getPosts(`${process.cwd()}/posts`).map(
(filename) => filename.match(/posts\/(.+)\/index\.md$/)?.[1],
);
for (const title of titles) {
app.use(
`/post/${title}/assets`,
express.static(`${process.cwd()}/posts/${title}/assets`),
);
}
app.get(/\/($|post(?!\/assets)|tag|search|archive)/, async (req, res) => {
const url = req.originalUrl;
const { render } = await vite.ssrLoadModule('./src/entry-server.tsx');
const { ssr, helmet, state } = await render(url);
const rawHtml = (
await fs.readFile(`${process.cwd()}/index.html`)
).toString();
const template = await vite.transformIndexHtml(url, rawHtml);
const hydration = `<script>window.__JOTAI_STATE__ = new Map(${JSON.stringify(
Array.from(state.entries()),
)})</script>`;
const html = template
.replace(
'<!--app-head-->',
`${helmet.title.toString()}${helmet.meta.toString()}`,
)
.replace('<!--app-body-->', `${ssr}${hydration}`);
res.contentType('text/html').status(200).end(html);
});
app.use('/', express.static(path.join(process.cwd(), 'dist/client')));
app.use('/', sitemap);
app.use('/', rss);
app.get('/health', (req, res) => {
res.status(200).end('OK');
});
app.listen(PORT, () => {
const link = `http://localhost:${PORT}`;
const clickable = `\x1b]8;;${link}\x1b\\${link}\x1b]8;;\x1b\\`;
console.log(`Listening ${clickable}...`);
});
};
createServer();