The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * Static-asset behavior shared across website hosts. |
| 3 | * |
| 4 | * Origin routing (`notFoundHandling`, `htmlHandling`) is host-agnostic. |
| 5 | * `Content-Type` / `Cache-Control` used when uploading a client directory |
| 6 | * (Fly Tigris today) are also host-agnostic HTTP policy: HTML is never |
| 7 | * cached so a deploy is visible immediately; content-hashed files are |
| 8 | * immutable. CDN topology (CloudFront vs Tigris vs none) stays host-specific. |
| 9 | * |
| 10 | * AWS.Website.AssetDeployment has its own table: it takes `fileOptions` / |
| 11 | * `textEncoding` overrides and historically used `application/javascript`. |
| 12 | * Do not force that API onto this module until those knobs are needed here. |
| 13 | * |
| 14 | * Keep {@link contentTypeOf} in sync with the MIME table inlined by |
| 15 | * `makeNodeServeEntrySource` (`frontend-frameworks` NodeServe) — that |
| 16 | * generated origin cannot import this file. |
| 17 | */ |
| 18 | |
| 19 | /** |
| 20 | * How unmatched GET paths are answered. Same names as Cloudflare |
| 21 | * Workers `assets.notFoundHandling`. |
| 22 | */ |
| 23 | export type WebsiteNotFoundHandling = "none" | "single-page-application" | "404-page"; |
| 24 | |
| 25 | /** |
| 26 | * Static-asset routing on the origin (`notFoundHandling`, `htmlHandling`). |
| 27 | */ |
| 28 | export interface WebsiteAssetsProps { |
| 29 | notFoundHandling?: WebsiteNotFoundHandling; |
| 30 | htmlHandling?: "none" | "drop-trailing-slash"; |
| 31 | } |
| 32 | |
| 33 | /** Map {@link WebsiteAssetsProps} onto the generated Node serve entry. */ |
| 34 | export const staticConfigFromAssets = ( |
| 35 | assets: WebsiteAssetsProps | undefined, |
| 36 | defaults?: { notFoundHandling?: WebsiteNotFoundHandling }, |
| 37 | ): { spa?: boolean; errorPage?: string } => { |
| 38 | const handling = assets?.notFoundHandling ?? defaults?.notFoundHandling; |
| 39 | if (handling === "single-page-application") return { spa: true }; |
| 40 | if (handling === "404-page") return { errorPage: "404.html" }; |
| 41 | if (handling === "none") return { spa: false }; |
| 42 | return {}; |
| 43 | }; |
| 44 | |
| 45 | /** HTML documents: never cached so a new deploy is visible immediately. */ |
| 46 | export const htmlCacheControl = "max-age=0,no-cache,no-store,must-revalidate"; |
| 47 | |
| 48 | /** Content-hashed static files (JS/CSS/images): cache forever. */ |
| 49 | export const assetCacheControl = "max-age=31536000,public,immutable"; |
| 50 | |
| 51 | /** |
| 52 | * `Content-Type` for a static website file, from its extension. |
| 53 | * |
| 54 | * JS is `text/javascript` (the HTML-spec JavaScript MIME type) so |
| 55 | * `<script type="module">` loads. Unknown extensions are |
| 56 | * `application/octet-stream`. |
| 57 | */ |
| 58 | export const contentTypeOf = (relative: string): string => { |
| 59 | const slash = Math.max(relative.lastIndexOf("/"), relative.lastIndexOf("\\")); |
| 60 | const base = slash >= 0 ? relative.slice(slash + 1) : relative; |
| 61 | const dot = base.lastIndexOf("."); |
| 62 | const ext = dot > 0 ? base.slice(dot + 1).toLowerCase() : ""; |
| 63 | switch (ext) { |
| 64 | case "html": |
| 65 | case "htm": |
| 66 | return "text/html; charset=utf-8"; |
| 67 | case "js": |
| 68 | case "mjs": |
| 69 | return "text/javascript; charset=utf-8"; |
| 70 | case "css": |
| 71 | return "text/css; charset=utf-8"; |
| 72 | case "json": |
| 73 | return "application/json"; |
| 74 | case "svg": |
| 75 | return "image/svg+xml"; |
| 76 | case "png": |
| 77 | return "image/png"; |
| 78 | case "jpg": |
| 79 | case "jpeg": |
| 80 | return "image/jpeg"; |
| 81 | case "gif": |
| 82 | return "image/gif"; |
| 83 | case "webp": |
| 84 | return "image/webp"; |
| 85 | case "ico": |
| 86 | return "image/x-icon"; |
| 87 | case "woff": |
| 88 | return "font/woff"; |
| 89 | case "woff2": |
| 90 | return "font/woff2"; |
| 91 | case "txt": |
| 92 | return "text/plain; charset=utf-8"; |
| 93 | case "wasm": |
| 94 | return "application/wasm"; |
| 95 | case "map": |
| 96 | return "application/json"; |
| 97 | default: |
| 98 | return "application/octet-stream"; |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | /** |
| 103 | * `Cache-Control` for a static website file. HTML (and `.htm`) is never |
| 104 | * cached; everything else is immutable. Frameworks content-hash JS/CSS |
| 105 | * into `/assets/...`, so a new deploy changes the URL instead of mutating |
| 106 | * a cached object. |
| 107 | */ |
| 108 | export const cacheControlOf = (relative: string): string => { |
| 109 | const lower = relative.toLowerCase(); |
| 110 | return lower.endsWith(".html") || lower.endsWith(".htm") ? htmlCacheControl : assetCacheControl; |
| 111 | }; |