Spaces:
Running
Running
Download api/middleware/cache.ts from ahmdelbaz28/AHMDRTAP: direct link, hf CLI and curl.
- Browser
- Download file 3.05 kB
-
https://huggingface.co/spaces/ahmdelbaz28/AHMDRTAP/resolve/main/api/middleware/cache.ts
- Command line
-
hf download hf://spaces/ahmdelbaz28/AHMDRTAP/api/middleware/cache.ts
-
curl -L -o cache.ts https://huggingface.co/spaces/ahmdelbaz28/AHMDRTAP/resolve/main/api/middleware/cache.ts
3.05 kB
| /** | |
| * CACHE MIDDLEWARE | |
| * | |
| * Sets Cache-Control headers based on file type. | |
| * - Hashed assets (assets/*.js, *.css) → 1 year cache (immutable) | |
| * - Images (png, jpg, webp, svg, etc.) → 1 year cache | |
| * - Fonts (woff2, ttf, etc.) → 1 year cache | |
| * - Critical files (sw.js, cache-nuke.js, etc.) → no-cache (must be fresh) | |
| * - HTML pages → no-cache (handled by the SPA HTML handler) | |
| * | |
| * Extracted from boot.ts to keep the main server file focused on routing. | |
| */ | |
| import { createMiddleware } from "hono/factory"; | |
| // Files that must NEVER be cached (always fetch fresh) | |
| const NO_CACHE_FILES = new Set([ | |
| "sw.js", | |
| "sw.js.map", | |
| "workbox-*.js", | |
| "workbox-*.js.map", | |
| "cache-nuke.js", | |
| "clarity.js", | |
| "pii-mask.js", | |
| "rtl-detect.js", | |
| "build-id.json", | |
| ]); | |
| // File extensions that get 1-year cache (immutable, content-hashed) | |
| const LONG_CACHE_EXTENSIONS = new Set([ | |
| // JS/CSS with content hashes | |
| "js", "css", "mjs", | |
| // Images | |
| "png", "jpg", "jpeg", "gif", "svg", "webp", "avif", "ico", | |
| // Fonts | |
| "woff", "woff2", "ttf", "eot", "otf", | |
| // Other static | |
| "json", "xml", "txt", "webmanifest", "map", | |
| // Media (background video/audio in /public/) | |
| "mp4", "webm", "m4v", "ogg", "mp3", | |
| ]); | |
| /** | |
| * Hono middleware that sets Cache-Control headers based on file type. | |
| * Must be registered BEFORE serveStatic so it runs for matching requests. | |
| */ | |
| export const cacheMiddleware = createMiddleware(async (c, next) => { | |
| const url = new URL(c.req.url); | |
| const pathname = url.pathname; | |
| const filename = pathname.split("/").pop() || ""; | |
| const ext = filename.split(".").pop()?.toLowerCase() || ""; | |
| // Critical files: no-cache (must always be fresh) | |
| const isNoCache = NO_CACHE_FILES.has(filename) || | |
| filename.startsWith("workbox-"); | |
| if (isNoCache) { | |
| c.header("Cache-Control", "no-cache, no-store, must-revalidate, proxy-revalidate"); | |
| c.header("Pragma", "no-cache"); | |
| c.header("Expires", "0"); | |
| } else if (LONG_CACHE_EXTENSIONS.has(ext) && pathname.startsWith("/assets/")) { | |
| // 🚀 Hashed assets in /assets/ → 1 year cache (immutable) | |
| // SAFE: Vite rewrites these filenames with a content hash, so new content | |
| // always gets a new URL. Old cached copies can never mask new content. | |
| c.header("Cache-Control", "public, max-age=31536000, immutable"); | |
| } else if (LONG_CACHE_EXTENSIONS.has(ext)) { | |
| // Files in /public/ (logos, course images, icons...) keep STABLE filenames, | |
| // so new deployments change their content under the same URL. Caching them | |
| // for a year with `immutable` (the previous behavior) made broken/outdated | |
| // images stick in returning visitors' browsers for up to a year — e.g. the | |
| // PowerFactory logo fix was invisible to anyone who had cached the old SVG. | |
| // 1 day + stale-while-revalidate: fast repeat visits, fresh content within | |
| // a day, and revalidation (Last-Modified via serveStatic) after expiry. | |
| c.header("Cache-Control", "public, max-age=86400, stale-while-revalidate=604800, must-revalidate"); | |
| } | |
| await next(); | |
| }); | |