diff --git a/frontend/content/blog/link-checker-114-anchors.md b/frontend/content/blog/link-checker-114-anchors.md new file mode 100644 index 00000000..219c8638 --- /dev/null +++ b/frontend/content/blog/link-checker-114-anchors.md @@ -0,0 +1,94 @@ +--- +title: Your link checker says 114 anchors are broken. They aren't. +description: We added lychee to CI to check every link and anchor on our static site, offline. Its first run reported 114 missing anchors, and every one of them existed. Why folder-style URLs fool it, the one setting that fixes it, and how we split link checking so it blocks bad deploys without flaking. +date: 2026-12-11T12:00:00+13:00 +author: Nathan Broadbent +draft: true +--- + +We wanted every link on visualci.com checked before a deploy: every page, every file, and every `#anchor`. The site is static, built by Vite and prerendered, so the check can run on the build in CI without a network. We used [lychee](https://lychee.cli.rs/): + +```sh +lychee --offline --include-fragments --root-dir dist 'dist/**/*.html' +``` + +At the time the site had 71 pages and 7,339 links. The first run reported 114 errors, all the same kind: + +``` +[ERROR] file:///…/dist/#pricing | Cannot find fragment +[ERROR] file:///…/dist/docs/agents#make-the-check-block-merges | Cannot find fragment +``` + +Seventy of them were links to `/#pricing`, and the rest were links into the docs. We opened the target pages, and every one of those ids was there. + +## Every failing link pointed at a folder + +Grouping the errors by target made the pattern plain. Every failing link pointed at a folder (`/`, `/docs/agents`), and none at a file. + +Our pages are folders. `/docs/agents/` is `docs/agents/index.html` in the build, as with most static site generators: Astro, Hugo and Vite's prerendering all lay pages out this way. A web server turns `/docs/agents` into that folder's `index.html` without being asked. lychee doesn't, unless you tell it to. Offline, it resolved `/docs/agents` to the directory itself and looked for the fragment in the directory, and a directory has no HTML. So every anchor link to a page counted as broken, and the anchors weren't missing at all. + +lychee has a setting for exactly this (`index_files`, in 0.24): + +```toml +# lychee.toml +include_fragments = "full" +# Pages are folders with an index.html (/docs/agents/ is docs/agents/index.html). +index_files = ["index.html"] +``` + +With it, the 114 errors went to none. A check that goes from 114 errors to 0 might simply have stopped checking, so we planted three faults: a link to a page that doesn't exist, a link to an anchor that doesn't exist, and a broken anchor in one of the site's own absolute links. All three failed, as they should. + +The site has grown since. Run against today's build without the setting, lychee reports 461 false errors. With it, it reported one, and that one was real. A page for agents gave the address of our CI guides as a pattern, `https://visualci.com/docs/ci/.md`, so anything that followed it landed on `/docs/ci/`, which returns 404. The fix lists each guide's address instead. + +## Check the site's own absolute links too + +Many of our links are absolute (`https://visualci.com/docs/...`), especially in the Markdown copies of the docs we publish for agents. Offline, lychee skips any link that would need the network, those included. A remap points them at the build instead: + +```sh +lychee --offline --root-dir "$site" \ + --remap "^https?://(www\.)?visualci\.com/?(.*)\$ file://$site/\$2" \ + "$site/**/*.html" "$site/**/*.md" "$site/llms.txt" +``` + +The remap is passed on the command line, not in `lychee.toml`, because it needs the build's absolute path. Only the first matching remap applies to a link, so put specific ones before general ones. + +Two more entries in `lychee.toml` keep the checks honest: + +```toml +exclude = [ + # Sign-in pages and API endpoints answer only signed-in or API requests. + '^https://app\.visualci\.com/(auth|api|oauth)/', + '^https://(api|mcp)\.visualci\.com', +] +remap = [ + # GitHub builds a repository page's #readme anchor in JavaScript, + # so it's never in the HTML lychee reads. + '^(https://github\.com/[^#]+)#readme$ $1', +] +``` + +Each exclusion has its reason next to it. An exclusion without one is how a link checker slowly stops checking anything. + +## Two jobs: offline and blocking, online and nightly + +Checking other people's sites in CI makes builds flaky: a site that's slow or down for a minute fails your deploy, and you didn't change anything. So we run two jobs. + +- **In CI, offline, blocking.** Right after the production site is built, lychee checks every page, file and anchor the site links to, its own absolute links included. It gives the same answer every time for the same build, so it can block the deploy. +- **Nightly, online, reporting.** A scheduled job reads the live `sitemap.xml` and checks every page's links, outside ones included. That's where another site's moved page or expired domain turns up, without making anyone's pull request red. `GITHUB_TOKEN` keeps lychee's github.com checks inside the API's rate limit. + +```yaml +- env: + GITHUB_TOKEN: ${{ github.token }} + run: | + curl -fsS --retry 3 --max-time 20 https://visualci.com/sitemap.xml \ + | grep -o '[^<]*' | cut -c6- > pages.txt + lychee --format compact --files-from pages.txt +``` + +## What to take from it + +- **If your pages are folders, set `index_files`.** Without it, an offline check reports every anchor link to a page as broken. +- **When a check goes from many errors to none, plant a few faults** to make sure it still fails. +- **Remap your own absolute links onto the build,** so they're checked offline too. +- **Give every exclusion a reason.** +- **Block on what you control, and report on what you don't.** Your own links block the deploy, and other sites' links get a nightly report.