Rukon
Full-Stack Development2 min read

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.

By

Next.jsPerformanceReact Server ComponentsTypeScript

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?

  1. Count in one pass. A new getPostCountsByProject(slugs) takes the published list once and counts for every project from it.
  2. 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.
  3. 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];
}
tsThe published list, computed once per day per server instance.

What was the result?

MeasureBeforeAfter
/work time to first byte (local production build)2.3–3.2 sabout 20 ms
/work HTML document (uncompressed)416 KB294 KB
Article page time to first byteabout 200 msabout 18 ms
Repeated requests against the same local production build.

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.