Repository navigation
Description
Problem
A link to #pricing or /docs/setup#install whose target id no longer exists still "works": the page loads, nothing errors, and the reader lands at the top of the page instead of the section they clicked. It happens all the time after a redesign or a CMS migration: headings get new auto-generated ids, a "Jump to pricing" button in the site nav still points at the old one, a table of contents is copied from another page, a skip link points at #main-content on a template that renamed its <main>.
squirrel never checks this. links/broken-links normalizes every link target with the fragment stripped before looking up its status, so /docs#does-not-exist reads as a healthy link to /docs. a11y/skip-link passes as soon as a skip link exists and does not look at its target.
Proposal
New rule links/broken-anchors.
- Warn (same page): a link to
#idon the page itself, where no element on that page hasid="id"and no<a name="id">exists (the two targets the HTML spec resolves a fragment to). - Warn (another crawled page): a link to
/other-page#idwhere/other-pagewas crawled this run and has no suchidorname. - One finding per target URL and fragment, listing the pages that link to it, so a broken anchor in the site nav is one finding, not one per page.
- Unconfirmed (info), not broken: the target page was fetched without rendering and looks client-rendered (the ids can be added by JavaScript after load).
- Labels skip links: when the broken link is a skip link, the finding says so.
a11y/skip-linkkeeps its current verdict, so the missing target is reported once, here.
Not reported:
#alone, and#topwhen the page has no element with that id (both scroll to the top by spec).- Hash routes and state, which are not element ids: fragments starting with
/or!(#/route,#!/route), fragments containing=(#access_token=...,#q=shoes), and text fragments (#:~:text=). - Links to pages the crawl did not fetch, to external sites, or to pages that answered 4xx/5xx (
links/broken-linksalready reports those).
Matching follows the browser: the fragment is percent-decoded and compared case-sensitively.
Notes
- Cost: the same-page check reads the DOM a page rule already has. The cross-page check needs each crawled page's set of ids and
<a name>values as a small, capped per-page record, so it also works in streaming audits that do not keep every page's DOM in memory. When a page's id set hits the cap, links into it are not judged. - False-positive risk: low for same-page links in server-rendered HTML. Higher for SPA pages and JS-built tables of contents, which is why unrendered client-rendered targets are reported as unconfirmed.
- Related: docs: make validate does not catch broken #anchor links #649 is the same problem for this repo's own docs build, not for audited sites.
Acceptance Criteria
- Fixture: a page with
<a href="#pricing">and no element withid="pricing"produces a warning naming the link, the fragment and the page - Fixture: the same page with
<section id="pricing">, or with<a name="pricing">, produces no finding - Fixture: page A links to
/b#install, page B is crawled and has noid="install": one warning naming/b#installand listing page A as a source - Fixture: five pages share a nav link to
/#pricingand the homepage has noid="pricing": one finding listing the five source pages, not five findings -
href="#",#top(with noid="top"on the page),#/settings,#!/home,#access_token=abcand#:~:text=helloproduce no finding - A link to
/c#xwhere/cwas not crawled, or answered 404, produces no finding from this rule - An id with non-ASCII or percent-encoded characters matches its decoded link (
#caf%C3%A9findsid="café"), and#Pricingdoes not matchid="pricing" - Fixture: in an unrendered audit, a link to
/app#settingswhere/appis an empty client-rendered shell (<div id="root"></div>plus a script) is reported as unconfirmed (info), not as a warning - A skip link to a missing
#main-contentis reported once, by this rule, and labelled as a skip link; thea11y/skip-linkverdict for that page is unchanged - The cross-page check gives the same result in a streaming audit as in a non-streaming one
- The rule has a docs page under
docs/rules/links/
Activity
test(audit-engine): recompute canonical golden counts for links/broke…
feat(rules): add links/broken-anchors for in-page anchor links #706
feat(rules): add links/broken-anchors #706
feat(rules): add security.txt and version-disclosure rules #713