Back to results for “Performance Optimization & Profiling”
Node.js › HTTP & Web Servers

How would you implement HTTP/2 server push in Node.js, and why has server push become less commonly recommended over time?

Advanced

Answer

HTTP/2 server push lets a server proactively send resources to a client before the client explicitly requests them (like pushing a CSS file alongside the HTML page that references it), aiming to save a round trip -- however, server push has largely fallen out of favor and was removed from Chrome, since it's genuinely difficult to predict correctly what a client already has cached, and incorrectly pushing resources a client already possesses wastes bandwidth; modern approaches favor alternatives like the 103 Early Hints status code, which suggests resources to preload without forcing them onto the client.
// HTTP/2 push (increasingly discouraged given browser support has been removed)
stream.pushStream({ ':path': '/style.css' }, (err, pushStream) => {
  pushStream.respond({ ':status': 200 });
  pushStream.end(cssContent);
});

// Modern alternative: 103 Early Hints, a lightweight preload suggestion
res.writeEarlyHints({ link: '</style.css>; rel=preload; as=style' });
Real-world example A team that previously implemented HTTP/2 server push for their CSS and JS bundles removed it after realizing browser cache-awareness issues were actually making performance worse for repeat visitors, migrating instead to 103 Early Hints as a lighter-weight, less risky preloading mechanism.

Common follow-ups: Why specifically does server push perform poorly for a client that already has the resource cached from a previous visit?;How does the 103 Early Hints status code avoid the cache-awareness problem that made server push risky?