-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources.js
54 lines (45 loc) · 1.21 KB
/
resources.js
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
import fs from 'node:fs';
import path from 'node:path';
if (!(await tables.Post.get('0'))) {
await tables.Post.put({
id: '0',
title: 'Hello, World!',
body: 'This is a test post. Please leave a comment! 📝',
comments: [],
});
}
const template = fs.readFileSync(path.join(import.meta.dirname, 'dist/client/index.html'), 'utf-8');
const serverEntry = await import('./dist/server/entry-server.js');
async function renderPost(post) {
const rendered = serverEntry.render({ initialPostData: post });
const html = template
.replace(`<!--app-head-->`, rendered.head ?? '')
.replace(`<!--app-html-->`, rendered.html ?? '')
.replace(`<!--app-data-->`, `<script>window.__INITIAL_POST_DATA__ = ${JSON.stringify(post)};</script>`);
return html;
}
export class UncachedBlog extends tables.Post {
async get() {
return {
status: 200,
headers: { 'Content-Type': 'text/html' },
body: await renderPost(this),
};
}
}
class PageBuilder extends tables.Post {
async get() {
return {
content: await renderPost(this),
};
}
}
tables.BlogCache.sourcedFrom(PageBuilder);
export class CachedBlog extends tables.BlogCache {
async get() {
return {
contentType: 'text/html',
data: this.content,
};
}
}