Skip to content

Open source alternative to Semrush and Ahrefs

fix(audit): inSitemap false negatives for sitemap pages past the page budget (EVE-222) (#632)

Ben Senescudc3b25d451ff
diff --git a/src/server/features/audit/AuditScratchpad.test.ts b/src/server/features/audit/AuditScratchpad.test.ts
index 1b373068..05ef4453 100644
--- a/src/server/features/audit/AuditScratchpad.test.ts
+++ b/src/server/features/audit/AuditScratchpad.test.ts
@@ -8,6 +8,7 @@ import { DatabaseSync } from "node:sqlite";
import { beforeEach, describe, expect, it } from "vitest";
import {
BROKEN_LINKS_SQL,
+ FRONTIER_DISCOVER_SQL,
ORPHAN_PAGES_SQL,
SCRATCHPAD_SCHEMA_SQL,
} from "@/server/features/audit/scratchpad-sql";
@@ -231,3 +232,24 @@ describe("query plans", () => {
expect(detail.filter((line) => /SCAN m\b/.test(line))).toHaveLength(1);
});
});
+
+describe("frontier link discovery", () => {
+ // Seeding covers the whole sitemap, so most linked pages already have a
+ // sitemap row. A link must still give them a click depth and link-queue
+ // priority, or the deep-page check and breadth-first crawl order go blind.
+ it("gives a pending sitemap seed the link's depth and priority, keeping its sitemap flag", () => {
+ const url = "https://site.test/post";
+ db.prepare(
+ `INSERT INTO frontier (url, depth, source, in_sitemap) VALUES (?, NULL, 'sitemap', 1)`,
+ ).run(url);
+
+ db.prepare(FRONTIER_DISCOVER_SQL).run(url, 2);
+ db.prepare(FRONTIER_DISCOVER_SQL).run(url, 3);
+
+ expect(
+ db
+ .prepare(`SELECT depth, source, in_sitemap FROM frontier WHERE url = ?`)
+ .get(url),
+ ).toEqual({ depth: 2, source: "link", in_sitemap: 1 });
+ });
+});
diff --git a/src/server/features/audit/AuditScratchpad.ts b/src/server/features/audit/AuditScratchpad.ts
index b6b1ebce..d42b5105 100644
--- a/src/server/features/audit/AuditScratchpad.ts
+++ b/src/server/features/audit/AuditScratchpad.ts
@@ -22,6 +22,7 @@ import { DurableObject, env } from "cloudflare:workers";
import type { CrawlThrottleState } from "@/server/lib/audit/crawl-throttle";
import {
BROKEN_LINKS_SQL,
+ FRONTIER_DISCOVER_SQL,
ORPHAN_PAGES_SQL,
SCRATCHPAD_SCHEMA_SQL,
} from "@/server/features/audit/scratchpad-sql";
@@ -214,13 +215,7 @@ export class AuditScratchpad extends DurableObject {
}
}
for (const found of input.discovered) {
- // OR IGNORE: already-seen URLs (crawled, leased, or pending) keep
- // their existing row — this is the global dedup.
- this.ctx.storage.sql.exec(
- `INSERT OR IGNORE INTO frontier (url, depth, source, in_sitemap) VALUES (?, ?, 'link', 0)`,
- found.url,
- found.depth,
- );
+ this.ctx.storage.sql.exec(FRONTIER_DISCOVER_SQL, found.url, found.depth);
}
return this.stats();
}
diff --git a/src/server/features/audit/scratchpad-sql.ts b/src/server/features/audit/scratchpad-sql.ts
index 2b0eaaa7..c1fe136e 100644
--- a/src/server/features/audit/scratchpad-sql.ts
+++ b/src/server/features/audit/scratchpad-sql.ts
@@ -49,6 +49,21 @@ export const SCRATCHPAD_SCHEMA_SQL = `
CREATE INDEX IF NOT EXISTS page_mirror_redirect_idx ON page_mirror (redirect_url);
`;
+/**
+ * Queue one link-discovered URL. Already-seen URLs keep their row (the
+ * global dedup), except that a pending row with no click depth yet — almost
+ * always a sitemap seed — takes the link's depth and link-queue priority
+ * while keeping its in-sitemap flag. Sitemap seeding covers the whole
+ * sitemap, so without this every linked sitemap page would crawl in sitemap
+ * order with no depth. Takes (url, depth).
+ */
+export const FRONTIER_DISCOVER_SQL = `
+ INSERT INTO frontier (url, depth, source, in_sitemap) VALUES (?, ?, 'link', 0)
+ ON CONFLICT(url) DO UPDATE SET source = 'link', depth = excluded.depth
+ WHERE frontier.state = 'pending' AND frontier.depth IS NULL
+ AND excluded.depth IS NOT NULL
+`;
+
/**
* Every internal link whose target we crawled and saw fail. `json_each`
* expands a page's target array into one row per edge; the join is on
diff --git a/src/server/lib/audit/discovery.test.ts b/src/server/lib/audit/discovery.test.ts
new file mode 100644
index 00000000..80e9585c
--- /dev/null
+++ b/src/server/lib/audit/discovery.test.ts
@@ -0,0 +1,36 @@
+import { describe, expect, it, vi } from "vitest";
+import { discoverUrls } from "@/server/lib/audit/discovery";
+
+function sitemapXml(urls: string[]): string {
+ const entries = urls.map((url) => `<url><loc>${url}</loc></url>`).join("");
+ return `<?xml version="1.0" encoding="UTF-8"?><urlset>${entries}</urlset>`;
+}
+
+describe("discoverUrls", () => {
+ // Seeding the frontier is what sets a page's in-sitemap flag, so a URL
+ // dropped here reports inSitemap: false once the crawl reaches it through a
+ // link. Real sitemaps bury the homepage in the middle of the list
+ // (bannerbear.com lists it 387th of 1,328).
+ it("returns sitemap URLs listed past the crawl's page budget", async () => {
+ const posts = Array.from(
+ { length: 5 },
+ (_, index) => `https://example.com/blog/post-${index}/`,
+ );
+ vi.stubGlobal(
+ "fetch",
+ vi.fn((url: string) =>
+ Promise.resolve(
+ url.endsWith("/robots.txt")
+ ? new Response("User-agent: *\nAllow: /")
+ : new Response(sitemapXml([...posts, "https://example.com/"]), {
+ headers: { "content-type": "application/xml" },
+ }),
+ ),
+ ),
+ );
+
+ const { urls } = await discoverUrls("https://example.com", 2);
+
+ expect(urls).toContain("https://example.com/");
+ });
+});
diff --git a/src/server/lib/audit/discovery.ts b/src/server/lib/audit/discovery.ts
index faeda32b..e42b7180 100644
--- a/src/server/lib/audit/discovery.ts
+++ b/src/server/lib/audit/discovery.ts
@@ -367,10 +367,15 @@ export async function discoverUrls(
);
}
- // Cap at the crawl's page budget: these are seeds, the crawl can never use
- // more — and an uncapped list can blow the ~1MiB Workflow step-state limit.
+ // Every discovered URL is returned, not just the first maxPages. Seeding is
+ // also what records each page's in-sitemap flag on the frontier, so
+ // truncating here made every sitemap URL past the budget report
+ // inSitemap: false once the crawl reached it through a link. The set is
+ // already bounded by maxDiscoveredUrls, seeds go straight into the
+ // scratchpad DO rather than through durable step state, and the crawl still
+ // stops at maxPages.
return {
- urls: Array.from(allUrls).slice(0, maxPages),
+ urls: Array.from(allUrls),
robotsText,
};
}