Stats about this site
How the Stats page counts this site's own commits, tests, components and lines of code, with a Vite plugin that does the counting when the site is built, and the crash in Storybook that showed my story tests weren't catching crashes.
The Stats page has numbers about me: my age, my career, my health, my flights. Now it has some about itself too. A section called "This site" shows how many commits it's had, how many tests it runs, how many components it's made of and how many lines of code it has.
Most of the work went into one question: where do you count the files when the server that shows the page doesn't have them?
Commits, from GitHub
The obvious way to count commits is git log --oneline | wc -l, and that's what the section's prompt says. But the site is built on Vercel, which clones the repository with only its recent history, so git would count a handful of commits, not all of them.
GitHub knows the real number, but it doesn't have an endpoint that just returns it. The trick is to ask for one commit per page and read how many pages there are:
const commits = await fetch( `https://api.github.com/repos/${SITE_REPO}/commits?per_page=1`, { headers: githubHeaders() })const last = commits.headers .get("link") ?.match(/[?&]page=(\d+)>; rel="last"/)The Link header points at the last page, and with one commit per page, the last page's number is the number of commits. The same request asks for the repository itself, for the date it was made, which goes on the card's "Since Nov 2026" badge.
It runs alongside the other GitHub requests the page already makes. If it fails, the card isn't shown and the other three still are.
Everything else, from the files
Tests, stories, components and lines of code all come from the source files. The catch is that the source files aren't there when the page runs. Vercel's server gets the built site, not src/.
So the counting happens while the site is built, and the numbers are written into the build. The tool for that is a small Vite plugin.
A module that isn't a file
The Stats page imports the numbers like any other module:
import siteStats from "virtual:site-stats"There's no file called that. The plugin makes it up:
const MODULE_ID = "virtual:site-stats"
export function siteStatsPlugin() { return { name: "site-stats", resolveId: (id: string) => id === MODULE_ID ? `\0${MODULE_ID}` : undefined, load: (id: string) => id === `\0${MODULE_ID}` ? `export default ${JSON.stringify(readSiteStats())}` : undefined, }}A Vite plugin is an object with hooks that Vite calls as it builds. This one uses two:
resolveIdruns for every import, and asks "where is this?". The plugin answers forvirtual:site-statsand leaves every other import to someone else. The\0in front is a convention that marks a module as virtual, so no other plugin tries to find it on disk.loadasks "what's in it?". Instead of reading a file, the plugin counts the files and returns a one-line module:
export default {"tests":167,"stories":95,"components":74,"lines":31265}From there it's an ordinary module. Vite bundles it into the server's code and the browser's, so on the live site, siteStats.tests is just a number. Nothing is counted when you load the page, and the numbers change with every deploy.
TypeScript doesn't know about the module either, since there's no file for it to read, so it gets a declaration of its own:
declare module "virtual:site-stats" { const stats: import("@/lib/site-stats").SiteStats export default stats}Counting
The counting itself is Node's globSync and readFileSync:
- Stories are the named exports of every
*.stories.tsxfile. - Components are the
.tsxfiles insrc/components, not counting their stories. - Lines of code are the lines of TypeScript and CSS that aren't blank, leaving out the route tree TanStack Router generates.
- Tests are the unit tests plus the stories, as every story also runs as a test in a browser.
Unit tests were the fiddly one. Counting it( and test( gave 59, but Vitest runs 68. The difference is it.each, which runs one test for every row of a table:
it.each([ ["not an object", "nope"], ["a missing total", { ...body, hours: undefined }], // …])("rejects %s", (_, input) => {So the counter finds each it.each([, walks to its closing bracket, and counts the rows at the table's own level, skipping over brackets inside strings. Vitest's own vitest list doesn't help here: it lists it.each once too.
The tests for the counter had a funny problem of their own. They're full of example test code in strings, and the counter reads every test file, its own tests included. So the examples are put together from pieces, like call("it", '("adds", () => {})'), and the counter never sees a whole it( to count.
The crash my tests didn't see
My first version didn't use a plugin. Vite has a define option that replaces a name with a value when the site is built, so SITE_STATS in the code would become the numbers. It worked on the site, and the Stats stories in Storybook passed.
Then I opened the Stats story to take a screenshot, and it was the 500 page: SITE_STATS is not defined.
Storybook doesn't use the site's whole Vite config. It has a smaller one of its own, with just what stories need, and define wasn't in it. A plugin can go in both, which is part of why the numbers moved to one. Storybook's config now lists it next to the plugin that compiles the blog's posts.
The worse news was that the tests had passed. A page that fails doesn't throw anything a test can see: it catches the error and shows the 500 page, and the 500 page passes every accessibility check. Every page story could have been broken, and the tests would have stayed green.
So page stories now show the 500 page through a wrapper that notes the error first:
export const pageError: { current?: unknown } = {}
function RecordedServerError(props: ErrorComponentProps) { useEffect(() => { pageError.current = props.error }, [props.error]) return <ServerError {...props} />}And after every story, Storybook checks it:
afterEach({ parameters }) { if (pageError.current === undefined || parameters.expectServerError) return throw new Error("The page failed and showed the 500 page", { cause: pageError.current, })},The one story that's meant to show the 500 page says so with expectServerError. Every other story that ends up there fails, and the error behind it shows up as the cause, which is the part you want when you're fixing it.
To check the check, I broke the Stats page on purpose. Its stories failed, with the deliberate error as the cause. Then I put it back, and all 95 stories passed, so nothing else had been hiding a 500 page all along.
What I learnt
- Count where the files are. The server doesn't have the source, so the build counts it and leaves the answers behind.
- A virtual module is a small plugin. Two hooks,
resolveIdandload, and an import can be anything you can work out at build time. - A shallow clone can't count. For anything about a repository's whole history, ask GitHub, not the copy the build has.
- Check what your counter counts. Counting
it(was close, butit.eachmeant it was wrong, and only Vitest's own total showed it. - A green test can mean nothing ran. The stories passed with the page crashed, because the error page is a perfectly healthy page. Now a story that lands on it fails.
- Look at it. Every test passed. The screenshot didn't.