From db4b1f3bb2b023fb2b78da00cd790f327c745e78 Mon Sep 17 00:00:00 2001 From: Collin Barrett <6483057+collinbarrett@users.noreply.github.com> Date: Fri, 20 Jun 2025 05:03:33 -0500 Subject: [PATCH] feat(infra): support tiered caching --- .../filterlists-api-host-rewrite-worker.js | 29 ++++++------------- .../filterlists-next-host-rewrite-worker.js | 20 ++++++++----- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/infra/cloudflare-workers/filterlists-api-host-rewrite-worker.js b/infra/cloudflare-workers/filterlists-api-host-rewrite-worker.js index 6774236ec..28d593e0a 100644 --- a/infra/cloudflare-workers/filterlists-api-host-rewrite-worker.js +++ b/infra/cloudflare-workers/filterlists-api-host-rewrite-worker.js @@ -1,34 +1,23 @@ /** * Cloudflare Worker for api.filterlists.com - * - * This worker enables two key functionalities: - * 1. Azure App Service Free Tier Custom Domain: Proxies requests to `app-filterlists-directory-prod.azurewebsites.net`, allowing a custom domain (`api.filterlists.com`) with Azure's free tier. - * 2. Respect Origin Cache-Control: Ensures Cloudflare's edge cache (via Cache API) honors the `Cache-Control` headers sent by the origin for all successful responses, optimizing caching for JSON APIs and static assets. */ export default { - async fetch(request, _, ctx) { - const cache = caches.default; - const cacheKey = request; - - let response = await cache.match(cacheKey); - - if (response) { - return response; - } - + async fetch(request, _, __) { const url = new URL(request.url); + const azureHostname = "app-filterlists-directory-prod.azurewebsites.net"; const originUrl = `https://${azureHostname}${url.pathname}${url.search}`; const originRequest = new Request(originUrl, request); + // HACK: trick Azure App Service free tier into supporting custom domain originRequest.headers.set("Host", azureHostname); - response = await fetch(originRequest); - - if (response.ok) { - const responseToCache = response.clone(); - ctx.waitUntil(cache.put(cacheKey, responseToCache)); - } + const response = await fetch(originRequest, { + cf: { + // force tiered edge caching for API responses + cacheEverything: true, + }, + }); return response; }, diff --git a/infra/cloudflare-workers/filterlists-next-host-rewrite-worker.js b/infra/cloudflare-workers/filterlists-next-host-rewrite-worker.js index b17df1454..da65f04c0 100644 --- a/infra/cloudflare-workers/filterlists-next-host-rewrite-worker.js +++ b/infra/cloudflare-workers/filterlists-next-host-rewrite-worker.js @@ -1,21 +1,25 @@ /** * Cloudflare Worker for next.filterlists.com - * - * Azure App Service Free Tier Custom Domain: Proxies requests to `app-filterlists-web-prod-h2hecsdqbrach3dt.eastus2-01.azurewebsites.net`, allowing a custom domain (`next.filterlists.com`) with Azure's free tier. */ export default { async fetch(request, _, __) { const url = new URL(request.url); - const azureHostname = "app-filterlists-web-prod-h2hecsdqbrach3dt.eastus2-01.azurewebsites.net"; + + const azureHostname = + "app-filterlists-web-prod-h2hecsdqbrach3dt.eastus2-01.azurewebsites.net"; const originUrl = `https://${azureHostname}${url.pathname}${url.search}`; const originRequest = new Request(originUrl, request); - // re-write Host header to trick App Service free tier into supporting custom domain + // HACK: trick Azure App Service free tier into supporting custom domain originRequest.headers.set("Host", azureHostname); - // delete cookie to trick Cloudflare into caching static HTML - originRequest.headers.delete('cookie'); - - return await fetch(originRequest); + const response = await fetch(originRequest, { + cf: { + // force tiered edge caching for server components, next.js pages, etc. + cacheEverything: true, + }, + }); + + return response; }, };