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
13 changes: 13 additions & 0 deletions apps/web/src/cloudflare/static-assets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ test("redirects moved objects to their new path without fetching the old one", a
response.headers.get("location"),
"https://static.anarlog.so/blog/library/products/otter/meeting-summary/2026-08-27-summary-card.webp",
);
const encoded = await worker.fetch(
new Request(
"https://static.anarlog.so/blog/articles/free-ai-notetakers/free%2D11.webp",
),
);
assert.equal(encoded.status, 301);
const preflight = await worker.fetch(
new Request(
"https://static.anarlog.so/blog/articles/free-ai-notetakers/free-11.webp",
{ method: "OPTIONS" },
),
);
assert.equal(preflight.status, 204);
const inherited = await worker.fetch(
new Request("https://static.anarlog.so/constructor"),
);
Expand Down
18 changes: 14 additions & 4 deletions apps/web/src/cloudflare/static-assets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { servePublicAsset } from "../lib/public-assets.ts";
import { MOVED_ASSETS } from "./moved-assets.ts";

// Matches how servePublicAsset reads paths, so encoded spellings of an old path redirect too.
function movedAsset(pathname: string) {
let path: string;
try {
path = decodeURIComponent(pathname.slice(1));
} catch {
return undefined;
}
return Object.hasOwn(MOVED_ASSETS, path) ? MOVED_ASSETS[path] : undefined;
}

export default {
async fetch(request: Request): Promise<Response> {
const url = new URL(request.url);
Expand All @@ -11,10 +22,9 @@ export default {
url.protocol = "https:";
return Response.redirect(url, 308);
}
const moved = Object.hasOwn(MOVED_ASSETS, url.pathname.slice(1))
? MOVED_ASSETS[url.pathname.slice(1)]
: undefined;
if (moved) {
const moved = movedAsset(url.pathname);
// Preflights are answered below: browsers do not follow a redirected preflight.
if (moved && (request.method === "GET" || request.method === "HEAD")) {
return new Response(null, {
status: 301,
headers: {
Expand Down
Loading