Skip to content

Open source alternative to Semrush and Ahrefs

perf(reports): cache the public report document at the edge for a minute (#620)

Ben Senescu4e1b06a3eb5d
diff --git a/src/client/features/reports/shared.tsx b/src/client/features/reports/shared.tsx
index 0cd3992f..65c38163 100644
--- a/src/client/features/reports/shared.tsx
+++ b/src/client/features/reports/shared.tsx
@@ -178,7 +178,7 @@ export function ShareReportModal({
<span className="block text-xs text-base-content/60">
{shared
? "Anyone with the link can view. No sign-in needed."
- : "Only members of your organization can open it."}
+ : "Only members of your organization can open it. A link that was open can keep loading for up to a minute."}
</span>
</span>
<input
diff --git a/src/routes/s/$token/raw.ts b/src/routes/s/$token/raw.ts
index 0226d6bc..496ee76f 100644
--- a/src/routes/s/$token/raw.ts
+++ b/src/routes/s/$token/raw.ts
@@ -21,6 +21,21 @@ import { sharePath } from "@/shared/report-share";
// kill switch off. A revoked link must not confirm that it once worked.
const NOT_SHARED_BODY = "This report isn't shared.";
+/**
+ * How long a colo may keep the document. A revoked link keeps loading for up
+ * to this long from an edge that already has it, which is what the share modal
+ * tells the user. ETags are deliberately out of scope: a revalidation answered
+ * before the token lookup would make revocation advisory.
+ */
+const EDGE_CACHE_CONTROL = "public, max-age=0, s-maxage=60";
+
+/** Every redirect this endpoint answers: never stored, by anyone. */
+const bounceTo = (location: string) =>
+ new Response(null, {
+ status: 302,
+ headers: { Location: location, "Cache-Control": "no-store" },
+ });
+
/**
* A stable pseudonym for one link, so views can be counted per report without
* a person profile and without putting the token itself (the capability) into
@@ -47,6 +62,12 @@ async function handleSharedReportRequest(
// Shape first, so a scanner walking /s/<anything>/raw costs no query.
if (!SHARE_TOKEN_PATTERN.test(token)) return notShared();
+ // The query string is part of the cache key, so `?cachebust=<random>` would
+ // otherwise turn every request back into an origin hit. Bounce to the bare
+ // path before any query runs: there is no parameter this endpoint reads.
+ const url = new URL(request.url);
+ if (url.search !== "") return bounceTo(`${sharePath(token)}/raw`);
+
const report = await ReportRepository.getSharedReportByToken(token);
if (!report || report.archived) return notShared();
@@ -58,13 +79,12 @@ async function handleSharedReportRequest(
// Sec-Fetch-Dest at all (an old browser, a link-preview fetcher, curl) would
// otherwise be bounced to a page whose frame it cannot load either, so an
// absent header is served the document.
+ //
+ // The 200 is the only cacheable answer here, so the response never needs to
+ // vary on Sec-Fetch-Dest: the redirect path is a 302, served `no-store`, and
+ // a shared cache stores neither it nor the 404.
const dest = request.headers.get("Sec-Fetch-Dest");
- if (dest !== null && dest !== "iframe") {
- return new Response(null, {
- status: 302,
- headers: { Location: sharePath(token), "Cache-Control": "no-store" },
- });
- }
+ if (dest !== null && dest !== "iframe") return bounceTo(sharePath(token));
const html = await ReportRepository.getReportHtml(
report.projectId,
@@ -93,7 +113,7 @@ async function handleSharedReportRequest(
);
return reportDocumentResponse(html, {
- cacheControl: "no-store",
+ cacheControl: EDGE_CACHE_CONTROL,
noindex: true,
});
}
diff --git a/src/server/features/reports/shareRawRoute.test.ts b/src/server/features/reports/shareRawRoute.test.ts
index 7f9f6865..d79925a0 100644
--- a/src/server/features/reports/shareRawRoute.test.ts
+++ b/src/server/features/reports/shareRawRoute.test.ts
@@ -44,6 +44,12 @@ const topLevel = () =>
const noFetchMetadata = () =>
new Request(`https://app.example.com/s/${TOKEN}/raw`);
+/** A cache-key buster: any query string at all. */
+const withQuery = () =>
+ new Request(`https://app.example.com/s/${TOKEN}/raw?x=1`, {
+ headers: { "Sec-Fetch-Dest": "iframe" },
+ });
+
const SHARED_REPORT = {
id: "report_1",
projectId: "project_1",
@@ -75,7 +81,7 @@ describe("handleSharedReportRequest", () => {
"referrer-policy": "no-referrer",
"x-content-type-options": "nosniff",
"x-robots-tag": "noindex, nofollow",
- "cache-control": "no-store",
+ "cache-control": "public, max-age=0, s-maxage=60",
});
});
@@ -86,9 +92,21 @@ describe("handleSharedReportRequest", () => {
expect(response.status).toBe(302);
expect(response.headers.get("location")).toBe(`/s/${TOKEN}`);
+ expect(response.headers.get("cache-control")).toBe("no-store");
expect(mocks.getReportHtml).not.toHaveBeenCalled();
});
+ // The query string is part of the edge cache key, so it is stripped before
+ // anything is read.
+ it("bounces a query string to the bare path without querying", async () => {
+ const response = await handleSharedReportRequest(TOKEN, withQuery());
+
+ expect(response.status).toBe(302);
+ expect(response.headers.get("location")).toBe(`/s/${TOKEN}/raw`);
+ expect(response.headers.get("cache-control")).toBe("no-store");
+ expect(mocks.getSharedReportByToken).not.toHaveBeenCalled();
+ });
+
// The redirect needs the header to say so. Bouncing a client that sends no
// fetch metadata would send it to a page whose frame it cannot load either.
it("serves the document when the request carries no Sec-Fetch-Dest", async () => {
@@ -133,6 +151,7 @@ describe("handleSharedReportRequest", () => {
expect(response.status).toBe(404);
expect(await response.text()).toBe(NOT_SHARED_BODY);
+ expect(response.headers.get("cache-control")).toBe("private, no-store");
expect(mocks.getReportHtml).not.toHaveBeenCalled();
});
diff --git a/web/content/docs/skills/seo-report.mdx b/web/content/docs/skills/seo-report.mdx
index 82ce674f..deb825df 100644
--- a/web/content/docs/skills/seo-report.mdx
+++ b/web/content/docs/skills/seo-report.mdx
@@ -24,7 +24,7 @@ Reports are free. They do not spend credits, and there is no practical limit on
## Sharing a report
-On openseo.so, open a report, click Share and turn on Public link. Send the link to a client or a teammate; they do not need an OpenSEO account. It always shows the latest saved version, and it is not indexed by search engines. Turn the toggle off and the link stops working.
+On openseo.so, open a report, click Share and turn on Public link. Send the link to a client or a teammate; they do not need an OpenSEO account. It always shows the latest saved version, and it is not indexed by search engines. Turn the toggle off and the link stops working, stopping new visitors within a minute.
## How to get the best result