One link for every stat

For a long time, the middle of my GitHub profile was four links. One drew my stats, one my top languages, one my streak and one a view counter, made by three different projects, with a row of badges from yet another. Each had its own look, its own theme options and its own idea of how big a card should be, and I spent more time making them line up than I'd like to admit.
Then the streak service went down, and the middle of my profile was a broken image.
So I wrote my own: readme-stats, one small Bun server on my home server that draws every card on my profile. I thought drawing the cards would be the hard part. It wasn't. Every surprise came after, once the cards had to live inside a README, and they all came within the first day.
Three cards become one
Three of the four links only existed because each project did one thing. Mine knows everything about the profile at once, so the overview, the streak, the languages and a year of contributions became one card, drawn from one set of answers from GitHub.
The streak was the interesting part. GitHub only gives contributions a year at a time, and a streak can cross New Year. So the server asks for every year since the account was made in one GraphQL request, with an alias for each:
Today not having a contribution yet doesn't end the streak, either. The day isn't over.

The fonts had to come along, too. The cards use JetBrains Mono, but GitHub shows them as images, and an SVG shown as an image can't load anything from anywhere else. So every card carries its font inside it, as base64, cut down to Latin, with only the weights that card uses. A badge doesn't need the extra-bold the banner's title does.
The first paste: a skills card with one skill
Every card comes in a dark and a light version, and <picture> shows the one that matches the reader's theme. So that's what I pasted into my README:
Light mode was fine. Dark mode drew a skills card with exactly one skill on it.
srcset isn't one link, it's a list of them, separated by commas. The browser read the dark card's link as ending at typescript. The fix is to write the commas as %2C: the server reads them the same, and srcset doesn't see a comma any more. Today, the builder on the server's own page writes them that way by itself.

The first change: nothing happened
Next I changed how a card looked, opened my profile, and saw the old one.
GitHub doesn't show the cards from my server. It copies every image in a README through its own proxy and shows that copy, for as long as the card says it's good for. My first version said a card was good for a while and could be served stale for a day while it was refreshed. That's a sensible thing to tell a CDN. But the proxy passes those headers on to the browser, and browsers took them literally: for up to a day, I was looking at yesterday's card.
Now cards that show GitHub's numbers say ten minutes, cards that only change with their link say an hour, and the view counter says not at all. Every card has an ETag, so asking again costs a 304 if nothing changed. And every card takes a v= that does nothing at all. Bumping it gives the card a new link, which the proxy has never seen, so a new look shows straight away.
Short cache times raised the old worry, though: a card that takes two seconds loads after the rest of the page. So GitHub's answers are kept in memory, and once they're half an hour old, the next request still gets the old answer straight away while a fresh one is fetched in the background. Only the very first request after a restart would wait on GitHub, and the server makes that one itself as it starts.
If GitHub doesn't answer, the last good answer keeps being shown. And when there's no answer at all, the error is a picture too, a small chip saying GitHub isn't answering. A broken image is exactly what I started all this to get away from.
The first screen reader: "GitHub stats"
A screen reader can't read a card. It reads the image's alt text, and the alt text is written in the README, so it can't change when the numbers do. "GitHub stats" is all it would ever say.
So the server also gives the profile card's numbers as JSON, at /stats.json. A GitHub Action in my profile repository runs every morning, fetches them and writes them into the card's alt text:
If anything changed, it commits the README. The commit is made with the workflow's own token, and those don't start workflows, so it doesn't set itself off.
One link
Four links to three projects became one server, and every card on my profile is one link to it: the stats, the streak and the languages in one card, the project cards, the skills, the badges, the banner and the view counter. All in one style, and if it ever goes down, it's mine to fix.
I got a few things I didn't know I wanted, too, like cards that never show up broken and alt text that's never out of date. It's open source, so if your profile is held together by other people's projects too, you can run your own. It comes with a builder that shows each card as you change it and writes the links for you.





No comments yet. Be the first!