aboutsummaryrefslogtreecommitdiff
path: root/src/worker.ts
blob: 024b01a5464dd7c69095cdcee5be3ec6007b10d9 (plain)
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
// Cloudflare Workers entry point. Static files under public/ are served by
// Workers Static Assets before the worker runs (see wrangler.jsonc).
import { Eta } from "eta";
import { createApp } from "./app.ts";
import { views } from "./views.generated.ts";

// Workers disallow runtime code generation, so templates are precompiled by
// scripts/compile-views.ts and preloaded into Eta's cache here. With
// cache: true, Eta serves them from the cache and never compiles.
const eta = new Eta({ cache: true });

eta.resolvePath = (templatePath: string) => {
  let name = templatePath.replace(/^\.\//, "");
  if (!name.endsWith(".eta")) name += ".eta";
  return name;
};

// Only reached on a cache miss, i.e. a template missing from the build.
eta.readFile = (path: string) => {
  throw new Error(`Unknown template: ${path}`);
};

for (const [name, fn] of Object.entries(views)) {
  eta.loadTemplate(name, fn);
}

export default createApp(eta);