mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
feat(infra): support tiered caching
This commit is contained in:
parent
3ffc0d2091
commit
db4b1f3bb2
2 changed files with 21 additions and 28 deletions
|
|
@ -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;
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
},
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue