Back to Browser Internals
Browser Internals
easy
mid

What are ETag, Cache Control, and DocumentFragment, and where are each used?

Three unrelated terms. Cache-Control: HTTP header dictating if/how long a response is cached. ETag: a content fingerprint for conditional revalidation (304 Not Modified). DocumentFragment: a lightweight, off-DOM container for batching DOM nodes so insertion triggers one reflow.

4 min read·~6 min to think through

These three are bundled in the question but are separate concepts — two HTTP caching, one DOM.

Cache-Control

An HTTP response header that tells the browser (and proxies/CDNs) whether and how long to cache a response:

  • max-age=3600 — fresh for 3600s; serve from cache without asking the server.
  • no-cache — may cache, but must revalidate with the server before using.
  • no-store — don't cache at all (sensitive data).
  • public / private — cacheable by shared caches (CDN) or only the browser.
  • immutable — never revalidate (great for content-hashed assets).

It's the primary caching control — it decides freshness.

ETag

An HTTP header that's a fingerprint of the response content (a hash or version token). It powers conditional requests / revalidation:

  1. Server sends ETag: "abc123" with a response.
  2. Next time, the browser sends If-None-Match: "abc123".
  3. If the content is unchanged, the server replies 304 Not Modified with an empty body — the browser reuses its cached copy.
  4. If changed, the server sends 200 with the new body and a new ETag.

So ETag saves bandwidth (no re-downloading unchanged content) when the cache needs revalidation. Cache-Control says when to revalidate; ETag makes that revalidation cheap. (Last-Modified/If-Modified-Since is the timestamp-based equivalent.)

DocumentFragment

A completely different thing — a DOM concept. DocumentFragment is a lightweight, minimal container that lives outside the main DOM tree. You build up nodes inside it, then insert it once:

js
const frag = document.createDocumentFragment();
for (const item of items) {
  const li = document.createElement("li");
  li.textContent = item;
  frag.appendChild(li);        // off-DOM — no reflow per append
}
list.appendChild(frag);        // ONE insertion → ONE reflow; the fragment "dissolves"

Appending to the live DOM 1000 times can trigger reflow each time. Building in a fragment and inserting once triggers a single reflow/repaint. When inserted, the fragment itself isn't added — only its children. It's a batch-DOM-updates performance tool.

The framing

"They're unrelated. Cache-Control is the HTTP header that decides if and how long a response is cached — max-age, no-cache, no-store, immutable. ETag is a content fingerprint for cheap revalidation: the browser sends If-None-Match, and the server can reply 304 Not Modified with no body if nothing changed — saving bandwidth. DocumentFragment is a DOM thing entirely — an off-tree container you batch nodes into and insert once, so 1000 appends cause one reflow instead of 1000."

Follow-up questions

  • How do Cache-Control and ETag work together?
  • What's the difference between no-cache and no-store?
  • Why does building in a DocumentFragment improve performance?
  • ETag vs Last-Modified — what's the difference?

Common mistakes

  • Confusing no-cache (revalidate) with no-store (never cache).
  • Thinking ETag prevents requests — it makes the request cheap (304), it still happens.
  • Forgetting the DocumentFragment dissolves on insertion (only children are added).
  • Treating these as one related topic.

Performance considerations

  • Cache-Control + ETag minimize network: skip requests entirely, or make revalidation a tiny 304. DocumentFragment minimizes layout work by collapsing many DOM mutations into one reflow.

Edge cases

  • ETag changing on every request (e.g. dynamic content) — revalidation never saves anything.
  • no-store for sensitive pages.
  • Content-hashed assets with immutable + long max-age.
  • Modern frameworks make DocumentFragment less necessary (virtual DOM batches for you).

Real-world examples

  • Content-hashed JS/CSS served with immutable + max-age=1y; HTML with no-cache + ETag.
  • Vanilla-JS list rendering using a fragment to insert many rows at once.

Senior engineer discussion

Seniors cleanly separate the three, explain the Cache-Control/ETag interplay (freshness vs cheap revalidation, the 304 flow), and describe DocumentFragment as a reflow-batching DOM tool — noting virtual-DOM frameworks largely subsume it.

Related questions