fix: restore dark post view and API revalidation

This commit is contained in:
lezi-fun committed 2026-08-11 13:34:44 +08:00
1 parent 773db0dec7
commit b60aff7c25
12 files changed
+100 -31

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
const REVALIDATE_CACHE_CONTROL = 'public, max-age=0, must-revalidate';
function matchesEtag(ifNoneMatch, etag) {
if (!ifNoneMatch) return false;
const normalized = etag.replace(/^W\//, '');
return ifNoneMatch.split(',').some((candidate) => {
const value = candidate.trim();
return value === '*' || value.replace(/^W\//, '') === normalized;
});
}
async function createEtag(body) {
const bytes = new TextEncoder().encode(body);
const digest = await crypto.subtle.digest('SHA-256', bytes);
const hash = Array.from(new Uint8Array(digest), (byte) =>
byte.toString(16).padStart(2, '0')
).join('');
return `"${hash}"`;
}
export async function jsonWithEtag(req, data, init = {}) {
const body = JSON.stringify(data);
const etag = await createEtag(body);
const headers = new Headers(init.headers);
headers.set('Cache-Control', REVALIDATE_CACHE_CONTROL);
headers.set('ETag', etag);
if (matchesEtag(req.headers.get('if-none-match'), etag)) {
return new Response(null, { status: 304, headers });
}
headers.set('Content-Type', 'application/json; charset=utf-8');
return new Response(body, { ...init, headers });
}