@brightsideoy/kama 0.1.0-alpha.149 → 0.1.0-alpha.150
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cli.js +58 -21
- package/dist/assets/{index-ckG1oyv-.js → index-DdJQwhNR.js} +1 -1
- package/dist/index.html +1 -1
- package/package.json +1 -1
- package/templates/astro-starter/src/middleware.ts +293 -0
- package/templates/astro-starter/src/pages/[...slug].astro +79 -39
- package/templates/astro-starter/middleware/api-purge.ts +0 -130
- package/templates/astro-starter/middleware/index.ts +0 -10
- package/templates/astro-starter/middleware/kv-cache.ts +0 -76
- package/templates/astro-starter/middleware/response-headers.ts +0 -43
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { defineMiddleware } from "astro:middleware";
|
|
2
|
-
import { env } from "cloudflare:workers";
|
|
3
|
-
|
|
4
|
-
export const kvCacheMiddleware = defineMiddleware(async (context, next) => {
|
|
5
|
-
const pathname = context.url.pathname;
|
|
6
|
-
const locals = context.locals as any;
|
|
7
|
-
const cfContext = locals.cfContext;
|
|
8
|
-
const workerEnv = (typeof env !== "undefined" ? env : (process?.env || {})) as any;
|
|
9
|
-
const HTML_CACHE = workerEnv?.HTML_CACHE;
|
|
10
|
-
|
|
11
|
-
const isWarmer = context.request.headers.get("X-Cache-Warmer") === "true";
|
|
12
|
-
const isHtmlPage = !pathname.includes('.') && !pathname.startsWith('/api/');
|
|
13
|
-
const cleanLookupPath = pathname.replace(/\/$/, "") || "/";
|
|
14
|
-
|
|
15
|
-
let activeCacheKey = cleanLookupPath;
|
|
16
|
-
|
|
17
|
-
// 🎯 1. READ PHASE
|
|
18
|
-
if (isHtmlPage && HTML_CACHE && !isWarmer) {
|
|
19
|
-
try {
|
|
20
|
-
const [siteVer, pageVer] = await Promise.all([
|
|
21
|
-
HTML_CACHE.get("site_version"),
|
|
22
|
-
HTML_CACHE.get(`v:${cleanLookupPath}`)
|
|
23
|
-
]);
|
|
24
|
-
|
|
25
|
-
activeCacheKey = `${cleanLookupPath}:v:${siteVer || "1"}:${pageVer || "1"}`;
|
|
26
|
-
|
|
27
|
-
const cachedHtml = await HTML_CACHE.get(activeCacheKey);
|
|
28
|
-
if (cachedHtml) {
|
|
29
|
-
return new Response(cachedHtml, {
|
|
30
|
-
status: 200,
|
|
31
|
-
headers: {
|
|
32
|
-
"Content-Type": "text/html; charset=utf-8",
|
|
33
|
-
"X-Cache-Status": "HIT-KV-STATICFILE",
|
|
34
|
-
"X-Cache-Key": activeCacheKey,
|
|
35
|
-
"Cache-Control": "no-store, no-cache, must-revalidate, proxy-revalidate"
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
} catch (e) {
|
|
40
|
-
console.warn("[KV Cache] Lookup bypassed:", e);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Process downstream SSR route
|
|
45
|
-
const response = await next();
|
|
46
|
-
|
|
47
|
-
// 🎯 2. WRITE PHASE
|
|
48
|
-
if (
|
|
49
|
-
isHtmlPage &&
|
|
50
|
-
HTML_CACHE &&
|
|
51
|
-
response.status === 200 &&
|
|
52
|
-
response.headers.get("content-type")?.includes("text/html")
|
|
53
|
-
) {
|
|
54
|
-
const htmlText = await response.clone().text();
|
|
55
|
-
|
|
56
|
-
const writeTask = (async () => {
|
|
57
|
-
if (activeCacheKey === cleanLookupPath || isWarmer) {
|
|
58
|
-
const forcedVersion = Date.now().toString();
|
|
59
|
-
await HTML_CACHE.put(`v:${cleanLookupPath}`, forcedVersion);
|
|
60
|
-
const siteVer = await HTML_CACHE.get("site_version") || "1";
|
|
61
|
-
activeCacheKey = `${cleanLookupPath}:v:${siteVer}:${forcedVersion}`;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
await HTML_CACHE.put(activeCacheKey, htmlText);
|
|
65
|
-
console.log(`✅ [KV Cache] Saved HTML under key: ${activeCacheKey}`);
|
|
66
|
-
})();
|
|
67
|
-
|
|
68
|
-
if (cfContext && typeof cfContext.waitUntil === 'function') {
|
|
69
|
-
cfContext.waitUntil(writeTask);
|
|
70
|
-
} else {
|
|
71
|
-
writeTask.catch((e: any) => console.error("KV write error:", e));
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return response;
|
|
76
|
-
});
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { defineMiddleware } from "astro:middleware";
|
|
2
|
-
|
|
3
|
-
export const responseHeadersMiddleware = defineMiddleware(async (context, next) => {
|
|
4
|
-
// Process downstream routes first so we have the response to modify
|
|
5
|
-
const response = await next();
|
|
6
|
-
const pathname = context.url.pathname;
|
|
7
|
-
|
|
8
|
-
// 1. LLMs & XML Configs
|
|
9
|
-
if (pathname === "/llms.txt" || pathname === "/llms-full.txt") {
|
|
10
|
-
response.headers.set("Content-Type", "text/plain; charset=utf-8");
|
|
11
|
-
response.headers.set("Cache-Control", "public, max-age=3600");
|
|
12
|
-
response.headers.set("Access-Control-Allow-Origin", "*");
|
|
13
|
-
}
|
|
14
|
-
else if (pathname === "/llxml.txt") {
|
|
15
|
-
response.headers.set("Content-Type", "application/xml; charset=utf-8");
|
|
16
|
-
response.headers.set("Cache-Control", "public, max-age=3600");
|
|
17
|
-
response.headers.set("Access-Control-Allow-Origin", "*");
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// 2. Static Asset Caching
|
|
21
|
-
else if (
|
|
22
|
-
pathname.startsWith("/media/") ||
|
|
23
|
-
pathname.startsWith("/_image") ||
|
|
24
|
-
pathname.match(/\.(png|jpg|jpeg|webp|avif|svg|gif|ico|woff2?|css|js)$/i)
|
|
25
|
-
) {
|
|
26
|
-
response.headers.set("Cache-Control", "public, max-age=31536000, s-maxage=31536000, immutable");
|
|
27
|
-
response.headers.set("Access-Control-Allow-Origin", "*");
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// 3. Security Headers for HTML
|
|
31
|
-
if (response.headers.get("content-type")?.includes("text/html")) {
|
|
32
|
-
response.headers.set("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload");
|
|
33
|
-
response.headers.set("X-Frame-Options", "SAMEORIGIN");
|
|
34
|
-
response.headers.set("X-Content-Type-Options", "nosniff");
|
|
35
|
-
response.headers.set("Cross-Origin-Opener-Policy", "same-origin-allow-popups");
|
|
36
|
-
response.headers.set(
|
|
37
|
-
"Content-Security-Policy",
|
|
38
|
-
"default-src 'self' https: data: 'unsafe-inline' 'unsafe-eval'; img-src 'self' https: data: blob:; connect-src 'self' https:; frame-ancestors 'self';"
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return response;
|
|
43
|
-
});
|