· 2 min read
Reading time, counted at build time
Added minutes to every Entry in content.ts: the words in the file without its frontmatter, at 200 words a minute, rounded up, at least 1.
What we did
- Added
minutesto everyEntryincontent.ts: the words in the file without its frontmatter, at 200 words a minute, rounded up, at least 1. - Shown as
· N min readinArticleHeader(the post and entry pages) andTeaserCard(the lists and the home page).
What I learned
content.tsis the only place that has every file as text, all at once. The layout only gets the rendered content (a snippet), and the[slug]page gets a compiled component, not words./blogneeds the number for every card. Computed in the post page, it would mean loading every post's body to show a list. Computed incontent.ts, it's a few bytes of data in the prerendered page ("minutes":14in__data.json).- Rule of thumb: compute a value where its input already is, and pass it as data to everyone who needs it. Tags work the same way: they're already on every
Entry, and the list of all tags is one more step overallPosts. - Counting the raw markdown means HTML tags and Shiki's
<span>s are never counted. That's a side effect, not the reason. - 200 words a minute is a bit under the usual ~238 for non-fiction: these posts are technical and full of code.
Links
Next
- Tag pages,
/blog/tags/[tag].