Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function responseXML(body: string, status: Status) {
export const withExpiry = <T>(response: Response, ttlInSeconds: number) => {
const clonedResponse = response.clone();
clonedResponse.headers.set("Cache-Control", `max-age=${ttlInSeconds}`);
clonedResponse.headers.set("Expires", new Date(Date.now() + ttlInSeconds * 1000).toUTCString());
return clonedResponse;
};

Expand Down
23 changes: 21 additions & 2 deletions routes/api/feeds/[seriesId].ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ import { responseJSON, responseXML, withExpiry } from "../../../lib/utils.ts";

const FEED_CACHE_KEY = "feeds-cache";

// Deno's cache API does not respect these headers.
// Until it does, manual expiry handling is needed.
// When it's working, this function would not be needed.
// See: https://github.com/denoland/deno/issues/25795
function isExpired(headers: Headers) {
const expiresHeader = headers.get("Expires");
if (!expiresHeader) {
console.log("No Expires header found, treating as expired");
return true;
}

const now = new Date();
const expiryDate = new Date(expiresHeader);
const expired = now > expiryDate;
return expired;
}

export const handler = async (
request: Request,
ctx: FreshContext,
Expand All @@ -13,11 +30,13 @@ export const handler = async (

const cache = await caches.open(FEED_CACHE_KEY);
const cachedResponse = await cache.match(request);
if (cachedResponse) {

if (cachedResponse && !isExpired(cachedResponse.headers)) {
console.log(`Cache hit for feed ${seriesId}`);
return cachedResponse;
}

console.log(`Fetching feed for ${seriesId}`);
const series = await caching.getSeries({ id: seriesId });
if (!series) {
return responseJSON({ message: "Series not found" }, STATUS_CODE.NotFound);
Expand All @@ -28,7 +47,7 @@ export const handler = async (
const response = withExpiry(
responseXML(feed, STATUS_CODE.OK),
// 2 hours
60 * 60 * 2,
2 * 60 * 60,
);

await cache.put(request, response.clone());
Expand Down