From b77d7372d003252eaf2925faf5d2b6bd1b6d8264 Mon Sep 17 00:00:00 2001 From: Omar Najjar Date: Sun, 13 Apr 2025 02:58:51 +1000 Subject: [PATCH] x --- worker.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/worker.js b/worker.js index 01a2023..ab49d8a 100644 --- a/worker.js +++ b/worker.js @@ -48,6 +48,18 @@ function validateRequest(request, requiredParams = []) { return null; } +// Helper function to get cache control headers +function getCacheControl(path) { + const cacheRules = { + '/api/health': 'public, max-age=3600', + '/api/proposals': 'public, max-age=300, stale-while-revalidate=60', + '/api/comments': 'public, max-age=60, stale-while-revalidate=30', + default: 'no-cache' + }; + + return cacheRules[path] || cacheRules.default; +} + export default { async fetch(request, env, ctx) { try { @@ -59,6 +71,11 @@ export default { return handleOptions(); } + // Set cache control headers based on path + const cacheControl = getCacheControl(path); + const headers = new Headers(); + headers.set('Cache-Control', cacheControl); + // Handle static assets if (path === '/styles.css' || path === '/index.html' || path === '/') { return serveStaticAsset(request, env); @@ -2042,21 +2059,4 @@ async function serveCustomizedHtml(proposalId, request, env) { // Fall back to regular page if something goes wrong return await fetch(request); } -} - -function getCacheControl(path) { - const cacheRules = { - '/api/health': 'public, max-age=3600', - '/api/proposals': 'public, max-age=300, stale-while-revalidate=60', - '/api/comments': 'public, max-age=60, stale-while-revalidate=30', - default: 'no-cache' - }; - - return cacheRules[path] || cacheRules.default; -} - -// In your fetch handler -if (request.method === "GET") { - const cacheControl = getCacheControl(path); - response.headers.set('Cache-Control', cacheControl); } \ No newline at end of file