Skip to content

Commit 879ceaa

Browse files
authored
chore: serve static files from Cloudflare assets without the Worker (#421)
run_worker_first: true counted every JSON API request against the Workers free-plan quota (58k/day). Only the dynamic endpoints and the clean-URL redirects now run the Worker; the API headers move to a generated _headers file, API misses get a JSON 404 from api/v1/404.html, and the alias-host redirects are zone Redirect Rules.
1 parent 6efb845 commit 879ceaa

4 files changed

Lines changed: 70 additions & 3 deletions

File tree

‎src/build/build.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from "../data/paths.js";
1919
import { SITE_URL } from "../data/site.js";
2020
import { buildRobotsTxt } from "../data/robots.js";
21+
import { buildHeadersFile } from "../data/headers.js";
2122
import {
2223
API_PATH,
2324
DISAMBIGUATION_PATH,
@@ -66,6 +67,14 @@ async function writeLlmsFiles(models: Model[]): Promise<void> {
6667

6768
async function writeRobotsAndSitemap(models: Model[]): Promise<void> {
6869
await fs.writeFile(path.join(DIST_DIR, "robots.txt"), buildRobotsTxt(SITE_URL), "utf8");
70+
await fs.writeFile(path.join(DIST_DIR, "_headers"), buildHeadersFile(), "utf8");
71+
// The asset layer answers a miss with the nearest 404.html, so API callers
72+
// get a JSON body (typed by _headers) instead of the HTML not-found page.
73+
await fs.writeFile(
74+
path.join(DIST_API_DIR, "404.html"),
75+
JSON.stringify({ error: "not_found" }) + "\n",
76+
"utf8",
77+
);
6978

7079
// Sitemaps list canonical, indexable HTML pages only — the JSON API and the
7180
// .txt agent files are intentionally excluded (they're not search results).

‎src/data/headers.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Cloudflare `_headers` builder. Static files are served straight from the
2+
// asset layer without invoking the Worker (Worker requests count against the
3+
// free plan's daily quota; asset requests do not), so the headers vercel.json
4+
// set on the JSON API and llms*.txt are declared here instead.
5+
6+
const CATALOG_CACHE = "public, max-age=300, s-maxage=3600";
7+
8+
const RULES: Array<[path: string, headers: Record<string, string>]> = [
9+
[
10+
"/api/v1/*",
11+
{
12+
"Content-Type": "application/json; charset=utf-8",
13+
"Access-Control-Allow-Origin": "*",
14+
"Cache-Control": CATALOG_CACHE,
15+
"X-Robots-Tag": "noindex",
16+
},
17+
],
18+
...["/llms.txt", "/llms-full.txt"].map((path): [string, Record<string, string>] => [
19+
path,
20+
{
21+
"Access-Control-Allow-Origin": "*",
22+
"Content-Type": "text/plain; charset=utf-8",
23+
"Cache-Control": CATALOG_CACHE,
24+
},
25+
]),
26+
];
27+
28+
export function buildHeadersFile(): string {
29+
return RULES.map(
30+
([path, headers]) =>
31+
[path, ...Object.entries(headers).map(([key, value]) => ` ${key}: ${value}`)].join("\n") +
32+
"\n",
33+
).join("\n");
34+
}

‎tests/headers.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, expect, it } from "vitest";
2+
import { buildHeadersFile } from "../src/data/headers.js";
3+
4+
describe("buildHeadersFile", () => {
5+
const file = buildHeadersFile();
6+
7+
it("types and caches the static JSON API", () => {
8+
expect(file).toContain(
9+
[
10+
"/api/v1/*",
11+
" Content-Type: application/json; charset=utf-8",
12+
" Access-Control-Allow-Origin: *",
13+
" Cache-Control: public, max-age=300, s-maxage=3600",
14+
" X-Robots-Tag: noindex",
15+
].join("\n"),
16+
);
17+
});
18+
19+
it.each(["/llms.txt", "/llms-full.txt"])("serves %s as plain text with CORS", (path) => {
20+
expect(file).toContain(`${path}\n Access-Control-Allow-Origin: *\n Content-Type: text/plain`);
21+
});
22+
});

‎wrangler.jsonc‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
"assets": {
88
"directory": "./dist",
99
"binding": "ASSETS",
10-
// The Worker runs first: it redirects the alias hosts, answers /mcp and
11-
// /api/v1/validate, and adds the headers vercel.json used to set.
12-
"run_worker_first": true,
10+
// Only the dynamic endpoints and the clean-URL redirects (307 → 308) run
11+
// the Worker. Every other file is served straight from assets, which does
12+
// not count against the Workers request quota; its headers come from
13+
// dist/_headers and the alias-host redirects are zone Redirect Rules.
14+
"run_worker_first": ["/mcp", "/api/mcp", "/api/v1/validate", "/*.html", "/*/"],
1315
// Same URLs as Vercel's cleanUrls + trailingSlash: false.
1416
"html_handling": "drop-trailing-slash",
1517
// Unknown paths get the branded dist/404.html with a 404 status.

0 commit comments

Comments
 (0)