What are ETag, Cache-Control, and Document Fragment
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.
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:
- Server sends
ETag: "abc123"with a response. - Next time, the browser sends
If-None-Match: "abc123". - If the content is unchanged, the server replies
304 Not Modifiedwith an empty body — the browser reuses its cached copy. - If changed, the server sends
200with 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:
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.