A Listing Page From 2.5 Seconds to 20 ms Without a Cache Layer
The work page on this site spent 2.3 to 3.2 seconds per request recomputing the same article projection fourteen times. Counting in one pass, computing the published list once per day, and rendering the cards on the server brought it to about 20 ms.
Where did the time go?
Each project card on /work shows how many articles draw on that project. The page computed the counts with a loop: for each project, ask for that project's articles. Asking for a project's articles meant asking for every published article in its public form, and producing the public form runs a set of text replacements over every field of every article. With fourteen projects and 173 articles, every request ran that projection about fourteen times over the whole library.
The page renders on each request, so the cost was paid by every visitor: 2.3 to 3.2 seconds before the first byte, measured on the production build running locally.
Why not just add a cache?
A cache in front of slow code hides it until the cache is cold, invalidated or bypassed, and here the slow part was not expensive work worth keeping; it was the same work repeated. Removing the repetition is simpler and also fixes every other caller of the same function.
What changed?
- Count in one pass. A new
getPostCountsByProject(slugs)takes the published list once and counts for every project from it. - Compute the published list once per day. Which articles are published changes only when the date does, because articles are released on a schedule. The public list is computed on the first call of a Malaysian calendar day and reused until the next; callers get a copy, so nothing can mutate the shared list.
- Render the cards on the server. The client-side filter now receives each card's slug, categories and finished markup, instead of full project objects it then rendered itself.
export function publishedPosts(isAuthorized = false): BlogPost[] {
const today = todayInMalaysia();
const cache = isAuthorized ? authorizedCache : publicCache;
if (cache.day !== today) {
const raw = allPosts.filter((p) => p.date <= today);
cache.posts = isAuthorized ? raw : raw.map((p) => projectBlogPost(p, false));
cache.sorted = [...cache.posts].sort((a, b) => (a.date < b.date ? 1 : -1));
cache.day = today;
}
return [...cache.posts];
}What was the result?
| Measure | Before | After |
|---|---|---|
| /work time to first byte (local production build) | 2.3–3.2 s | about 20 ms |
| /work HTML document (uncompressed) | 416 KB | 294 KB |
| Article page time to first byte | about 200 ms | about 18 ms |
The same change made the site ready for a larger library: the blog index is now paginated at 40 articles a page, so its size stays flat as articles are added, and listing pages no longer do work proportional to projects times articles.
How is a regression caught next time?
The browser check that runs in CI records time to first byte, largest contentful paint, layout shift and JavaScript weight for the main pages, and the testing write-up describes how. The page itself is the work index.
Related
Keep reading
Testing a Portfolio Like a Production Application
This site now runs two checks against a started production build on every push: an HTTP crawl of routes, links, metadata, structured data, the feed and private terms, and a real-browser pass over clicks, the mobile menu, ten screen widths and accessibility. What they found on the first run.
One Client Import Shipped Every Project to the Browser
A client component on this site imported one small value from a content module, and the bundler shipped the whole module, every project included, to every visitor. How it was found, why render-time hiding did not help, and the server-only boundary that now makes it a build error.
Google Sign-In Without a Client Library: Server-Side PKCE and Grants Read on Every Request
RPOMS added Google sign-in through its Supabase project's provider with no client library in the browser bundle. Google proves who someone is and grants nothing; a Super Admin grants access per service and capability, and revocation takes effect on the next request.