@variance-authority/route-collector
Turn pages your application already serves into Variance Authority subjects.
Part of Variance Authority.
Point your coding agent at llms.txt.
What this is for
You already run a server that renders your pages. This package points a browser
at URLs you name, reads a subtree of each page, and hands the result to
@variance-authority/cli to compare against the image you approved last time.
When something changed, the report names the component that drew it and the
file:line it was written at.
A subject is one named UI state you asked for and can ask for again — here,
one route at one viewport, under an id you choose. A collector is a module
you write that gives the CLI the subjects that exist and how to open them; this
package builds one from a list of routes, a sitemap, or a directory of built
HTML.
It navigates the URLs you give it and nothing else. It does not crawl links, it
does not start your application, and it has no cookie, header, or storage-state
login support — for a route that only exists after a login, use
@variance-authority/playwright-test and keep the login in the Playwright test
that already performs it.
There is no router or framework configuration to write. The collector sees your
pages the way a browser does, over HTTP, so whatever produced them has already
finished its job by the time this runs.
Requirements
- Node 22 or newer. Every
@variance-authority/* package is ESM-only.
- A Chromium binary from Playwright.
profile: "chromium" is what resolves
layout, computed style and pixels; jsdom cannot produce an image, so a route
run needs Chromium.
- A running application, or a built directory. The URLs in your collector
must resolve from the machine that runs the comparison, not only from your
laptop.
React is optional. The package declares no React dependency and imports nothing
from React: it reads the __reactFiber$… expando react-dom writes onto host
nodes.
| React |
Status |
| 19, 18.3 |
Exercised by this package's own suite |
| 17 |
Read by the same path — same expando, same work tags — and not exercised |
| 16 |
Read through the older __reactInternalInstance$ expando, and not exercised |
On a page React never rendered, the Suspense wait below reports the root as
unobserved and collection continues; set wiring: false to drop the React
reading entirely.
Put one route through review
1. Install
npm install --save-dev @variance-authority/cli @variance-authority/route-collector
npx playwright install chromium
The second command is there because Playwright's browser binaries do not arrive
with an npm install.
2. Start the application
Run the same server whose redirects, assets, and runtime behavior you want to
observe — npm run dev, npm start, whatever you already use. Leave it
running; nothing below starts or stops it.
3. Write the collector module
// variance/routes.mjs
import { routeCollector } from '@variance-authority/route-collector';
export default routeCollector({
routes: {
'checkout/empty': 'http://localhost:3000/checkout',
'checkout/one-item': 'http://localhost:3000/checkout?items=1',
},
roots: ['#app'],
ready: { 'checkout/one-item': '[data-testid="cart-ready"]' },
source: { dirs: ['src'] },
});
roots is an ordered list of CSS selectors, and the first one that matches is
the subject. The default is ['body'], meaning the whole page is under review.
A tighter root keeps shared navigation or application chrome out of every route,
so an edit to the header does not change forty routes at once. If none of the
selectors matches, the route is a collection failure rather than an empty
capture.
source.dirs is what turns a component name into a file:line. It is optional;
a path that matches no files is refused by name rather than producing a report
where every component resolves to nothing.
4. Write the run config
// variance.config.json
{
"project": "checkout-ui",
"profile": "chromium",
"viewport": { "width": 1280, "height": 800 },
"retention": "durable",
"subjects": {
"kind": "list",
"ids": ["checkout/empty", "checkout/one-item"],
"collector": "variance/routes.mjs"
},
"baselines": { "kind": "directory", "root": ".variance/baselines" },
"fonts": [],
"report": ".variance/report.json"
}
profile is not a browser name — it is what the collector is able to observe.
The engine is a separate optional browser key (chromium, firefox or
webkit, defaulting to chromium). retention: "durable" is what makes a
first run report new and a second report unchanged.
An id planned by the config with no matching route is reported as a collection
failure; it is never silently dropped. A route present only in the collector is
never planned. Adding or removing coverage is then a configuration change
somebody reviews.
5. Check the machine, then run
variance doctor --config variance.config.json
variance run --config variance.config.json
npx because you installed the CLI as a devDependency, so variance is not on
your PATH. doctor launches the browser and measures the fonts you asserted
rather than consulting a version table.
The first durable run exits 0 with both routes reported as new: nothing moved, and nothing is approved until you accept it. 0 means
nothing needs review, 1 means the run happened and found something a person
must decide about, and 2 means the run did not happen as configured.
6. Look at the candidates
Write the report as HTML beside its JSON source, so its relative image links
still work, and open it:
variance report --config variance.config.json --format html > .variance/report.html
report uses the same exit codes as run: it writes the whole report and
then exits 1 when the report shows something to review. Under set -e that
ends the script, so allow it — || true in a shell recipe, continue-on-error
in a CI step — and read the code from run instead.
7. Approve what you meant
variance accept --config variance.config.json checkout/empty checkout/one-item
variance run --config variance.config.json
accept promotes exactly the image the run under review produced; it never
renders a replacement. The rerun exits 0 and reports unchanged.
The full signature:
variance accept [--config <path>] <subject>... | --all | --shape <fingerprint>[,...] [--by <name>] [--message-file <path> [--message <text>]]
--shape takes a fingerprint — the shape digest the report attaches to each
changed region — so one decision covers every subject where the same kind of
difference landed. Keep --all out of an unattended job: it does not separate a
candidate somebody reviewed from one nobody opened.
With baselines.kind: "directory", approval arrives as a commit: the .png and
.json under .variance/baselines are ordinary tracked files, and the new
approved image is a diff in the pull request. If your repository ignores
.variance/, exclude its contents rather than the directory:
.variance/*
!.variance/baselines/
What you get
.variance/report.json, abridged — one route whose padding changed:
{
"runVersion": 1,
"identity": {
"renderer": "playwright-chromium@1.49.0",
"engine": "chromium@131",
"platform": "linux-x64",
"deviceScaleFactor": 1,
"fonts": ["Inter"]
},
"retention": "durable",
"observations": [
{
"subject": "checkout/one-item",
"verdict": "changed",
"because": "12 pixels differ inside the label",
"changedPixels": 12,
"regions": [
{
"x": 4,
"y": 8,
"width": 60,
"height": 18,
"pixels": 12,
"component": "CartSummary",
"where": "main → list item 2 of 3",
"file": "src/checkout/CartSummary.tsx:42",
"cause": true,
"fingerprint": "b5c1f0a2"
}
]
},
{ "subject": "checkout/empty", "verdict": "unchanged" }
],
"notObserved": []
}
identity is the machine the pixels were made on, and it is part of the key a
baseline is stored under. cause: true marks the region the semantic tier named
as a root of the change rather than something the change pushed around.
notObserved lists subjects the run did not observe, with why — absent is not
empty, because silence about a subject cannot support the sentence "nothing
needs review".
Sitemap and directory route sources
Use exactly one of routes, sitemap, or directory.
When a sitemap or built directory is already the canonical inventory, let the
collector plan the subjects and set subjects.kind to collector in your
otherwise complete variance.config.json:
// variance/routes.mjs
import { routeCollector } from '@variance-authority/route-collector';
export default routeCollector({
sitemap: 'http://localhost:3000/sitemap.xml',
roots: ['main'],
source: { dirs: ['src'] },
});
// a fragment of variance.config.json — every other key stays as it is
{
"subjects": {
"kind": "collector",
"collector": "variance/routes.mjs"
}
}
Discovered ids come from the URL path with leading and trailing slashes trimmed,
and a sitemap listing two URLs whose paths collide is refused rather than
resolved. Each page is visited on the server the sitemap came from, keeping the
entry's path and query, so a local build whose sitemap names production is still
the build photographed. For static output, replace sitemap with
directory: './build'; the collector serves it locally and creates one subject
per .html file. Ids drop
the extension and index.html resolves to its directory, so cart/empty.html is
the subject cart/empty and about/index.html is about — the name you type
back at --subjects and variance accept. The root index.html has no
directory above it to be named after, so its id is /. Two files that would
answer to one id — cart/empty.html beside cart/empty/index.html — are
refused by name rather than resolved.
A page dropped from the sitemap or the build stops being watched with no config
diff to approve. The run still reports it by id — the baseline store keeps an
approved image the plan did not contain — but you learn about it from a run
rather than from a review. Keep the explicit routes form where removing a
subject should be something somebody signs off. A sitemap index is not followed:
its own <loc> values are treated as pages, and links on pages are never
crawled.
Leave a page out
A sitemap lists every page a site publishes, and not every page is one you
review as pixels: a long article, a changelog, a generated reference. Such a
page changes on every prose edit and can be tens of thousands of pixels tall.
Mark it on the page itself, with the reason as the value:
<article data-variance-exclude="a markdown document: its change is reviewed as text">
The attribute can sit on any element of the document, inside the roots or not,
and it applies to the whole page. Put it in the component that renders the kind
of page — the layout every article uses — and a page added later leaves the run
with no edit to the collector. The collector navigates, waits for ready, sees
the attribute, and reads and photographs nothing. The run lists the page as
excluded, with the reason the page gave, beside the subjects the plan left
out; an excluded page never sends a run to review. A page that drops the
attribute is photographed on the next run.
Observe responsive layouts
Use widths when each route must be evaluated at more than one breakpoint:
// variance/routes.mjs
import { routeCollector } from '@variance-authority/route-collector';
export default routeCollector({
routes: { home: 'http://localhost:3000/' },
widths: [375, 1280],
roots: ['main'],
});
Each width becomes its own subject — home@375 and home@1280 — with its own
navigation, baseline, and verdict. The page is navigated at the requested width
so code that reads matchMedia during mount makes the correct decision. The
height stays the run's; only the width changes. The suffix is part of the id, so
--subjects 'home@375' selects exactly one and an ignore rule globbing
home* still matches both.
Options
A band in the table below is the kind of difference a report names, ordered
loudest first: a11y, geometry, token, content, texture.