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
10 changes: 10 additions & 0 deletions src/rewriter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,14 @@ describe("HTMLRewriter integration", () => {
expect(resp.headers.get("content-security-policy")).toBeNull();
expect(resp.headers.get("access-control-allow-origin")).toBe("*");
});

it("rewrites data-src attributes for lazy-loaded images", async () => {
const resp = await worker.fetch("/browse/https://www.wired.com");
if (resp.status !== 200) return;
const html = await resp.text();
const dataSrcMatches = html.match(/data-src="([^"]*)"/g) || [];
if (dataSrcMatches.length === 0) return; // skip if wired changed their pattern
const allProxied = dataSrcMatches.every((m) => m.includes("/browse/"));
Comment on lines +112 to +117
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test can pass without asserting the new behavior: it returns early when Wired is down/non-200, and also returns early when no data-src attributes are present. That makes CI green even if the rewrite regresses or Wired changes/blocks requests. Consider using Vitest's skip mechanism (so it shows as skipped) and/or make the test deterministic by transforming a small HTML fixture through buildRewriter() and asserting the rewritten data-src value(s).

Copilot uses AI. Check for mistakes.
Comment on lines +115 to +117
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion m.includes("/browse/") is quite loose and can produce false positives if the original URL happens to contain /browse/ as part of its path/query. It would be more robust to capture the attribute value and assert it starts with the proxy prefix (e.g., data-src="/browse/), or parse out the value and check value.startsWith("/browse/").

Suggested change
const dataSrcMatches = html.match(/data-src="([^"]*)"/g) || [];
if (dataSrcMatches.length === 0) return; // skip if wired changed their pattern
const allProxied = dataSrcMatches.every((m) => m.includes("/browse/"));
const dataSrcMatches = [...html.matchAll(/data-src="([^"]*)"/g)];
if (dataSrcMatches.length === 0) return; // skip if wired changed their pattern
const allProxied = dataSrcMatches.every((m) => m[1].startsWith("/browse/"));

Copilot uses AI. Check for mistakes.
expect(allProxied).toBe(true);
});
});
11 changes: 8 additions & 3 deletions src/rewriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ class URLRewriter implements HTMLRewriterElementContentHandlers {
}

class SrcsetRewriter implements HTMLRewriterElementContentHandlers {
constructor(private baseUrl: string) {}
constructor(
private baseUrl: string,
private attr: string = "srcset",
) {}

element(el: Element) {
const srcset = el.getAttribute("srcset");
const srcset = el.getAttribute(this.attr);
if (!srcset) return;
const rewritten = srcset
.split(",")
Expand All @@ -74,7 +77,7 @@ class SrcsetRewriter implements HTMLRewriterElementContentHandlers {
return parts.join(" ");
})
.join(", ");
el.setAttribute("srcset", rewritten);
el.setAttribute(this.attr, rewritten);
}
}

Expand Down Expand Up @@ -108,7 +111,9 @@ export function buildRewriter(targetUrl: string): HTMLRewriter {
.on("meta", new MetaCSPRemover())
.on("a, area", new URLRewriter(targetUrl, "href"))
.on("img", new URLRewriter(targetUrl, "src"))
.on("img, source", new URLRewriter(targetUrl, "data-src"))
.on("img, source", new SrcsetRewriter(targetUrl))
.on("img, source", new SrcsetRewriter(targetUrl, "data-srcset"))
.on("video", new URLRewriter(targetUrl, "src"))
.on("video", new URLRewriter(targetUrl, "poster"))
.on("audio", new URLRewriter(targetUrl, "src"))
Expand Down
Loading