Static site hosting via tangled
1import PagesService from "./pages-service.js";
2
3let pagesService = null;
4
5// idk how long cloudflare will keep this around.
6// it would be better to save the config in a KV store
7// but this is good enough for now
8function getPagesService(env) {
9 if (!pagesService) {
10 pagesService = new PagesService({
11 domain: env.KNOT_DOMAIN,
12 ownerDid: env.OWNER_DID,
13 repoName: env.REPO_NAME,
14 branch: env.BRANCH,
15 verbose: env.NODE_ENV === "development",
16 });
17 }
18 return pagesService;
19}
20
21export default {
22 async fetch(request, env, ctx) {
23 const route = new URL(request.url).pathname;
24 const pagesService = getPagesService(env);
25 const { status, content, contentType } = await pagesService.getPage(route);
26 return new Response(content, {
27 status,
28 headers: { "Content-Type": contentType },
29 });
30 },
31};