logo
Parslinks
Back to blog
Guides5 min read

High TTFB in Next.js: finding the bottleneck, layer by layer

When a page is slow to start loading, there's usually one big culprit, not ten small ones. Find it first.

Alireza Tadi

Co-founder, Parslinks · Team, marketing & infrastructure

The product page of a leather bag and shoe shop in Tabriz scores poorly in PageSpeed, and the first warning is "Reduce initial server response time": a TTFB of about 670 ms. The team goes for the familiar fixes first: compressing images and shrinking the font. The number doesn't budge, because none of that happens before the first byte.

TTFB is entirely server and network. To bring it down, you need to know exactly where those 670 ms are spent.

The pieces of TTFB

How network, middleware, data and rendering add up to a product page TTFB

Step one: measure, don't guess

The Server-Timing header is the simplest tool: the browser shows it in the Network tab under Timing, with no extra service needed.

lib/timing.ts
// lib/timing.ts — time each step and report it in Server-Timing
export async function timed<T>(marks: string[], name: string, fn: () => Promise<T>) {
  const start = performance.now();
  try {
    return await fn();
  } finally {
    marks.push(`${name};dur=${(performance.now() - start).toFixed(1)}`);
  }
}

// In a Route Handler or middleware:
// res.headers.set("Server-Timing", marks.join(", "));
ts12 lines

For pages (Server Components) you can log the same numbers. The only goal is to know which layer has the biggest share.

Culprit one: data fetching

At the Tabriz shop, 480 of the 670 ms was data. Three common problems:

Sequential queries that could run in parallel. The product, reviews and related products don't depend on each other, but were fetched with three awaits in a row:

app/products/[id]/page.tsx
// Before: 120 + 90 + 180 = 390 ms
const product = await getProduct(id);
const reviews = await getReviews(id);
const related = await getRelated(id);

// After: as long as the slowest, about 180 ms
const [product, reviews, related] = await Promise.all([
  getProduct(id),
  getReviews(id),
  getRelated(id),
]);
ts11 lines

A query without an index. "Related products" filtered on a category column with no index. EXPLAIN ANALYZE in PostgreSQL showed a full table scan; one index took it from 180 ms to 8 ms.

Data that could have been cached. Product details change a few times a day, not every second. With tagged caching, the page is served from cache and rebuilt only when the product is edited.

Culprit two: middleware

Middleware runs before every request, even static-file requests if the matcher isn't set right. One fetch to an auth service in middleware adds that latency to every page. Keep middleware to light work (redirects, language detection, checking a cookie exists) and write a precise matcher.

Culprit three: the network

If your users are in Iran and your server is in Europe, the TLS connection round trips alone can cost 200 ms before a single line of code runs. No code optimisation makes up for that. We cover it in hosting inside Iran or abroad.

Culprit four: rendering

The slow part of a page (reviews, say) shouldn't hold up the whole page. With Suspense, the page shell and core product info are sent immediately and reviews stream in afterwards. TTFB becomes as fast as the quickest essential part, not the slowest part.

The result

By running queries in parallel, adding one index and caching the product page, the Tabriz shop brought TTFB from 670 ms to about 150 ms. None of the changes was more than a few lines of code. The hard part was finding the right place.

Frequently asked questions

Join the discussion

No comments yet. Be the first to share your thoughts.