Performance: Conditional requests handling
This affects server response time and network load by reducing unnecessary data transfer when content is unchanged.
app.get('/data', (req, res) => { const data = getData(); const etag = generateETag(data); res.set('ETag', etag); if (req.headers['if-none-match'] === etag) { res.status(304).end(); } else { res.send(data); } });
app.get('/data', (req, res) => {
const data = getData();
res.send(data);
});| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Always send full response | N/A (server-side) | N/A | High network payload delays paint | [X] Bad |
| Use ETag with 304 Not Modified | N/A (server-side) | N/A | Minimal network payload speeds paint | [OK] Good |
app.get('/data', (req, res) => {
res.set('ETag', '12345');
if (req.fresh) {
res.status(304).end();
} else {
res.send('Full data response');
}
});app.get('/info', (req, res) => {
res.set('Last-Modified', new Date().toUTCString());
if (req.fresh()) {
res.status(304).end();
} else {
res.send('Info data');
}
});Pass the test and this pattern moves unknown