Performance: ETag and conditional requests
MEDIUM IMPACT
This concept affects page load speed by reducing unnecessary data transfer and server processing for unchanged resources.
app.get('/file', (req, res) => { const fileContent = fs.readFileSync('file.txt'); const etag = generateETag(fileContent); if (req.headers['if-none-match'] === etag) { res.status(304).end(); } else { res.setHeader('ETag', etag); res.setHeader('Content-Type', 'text/plain'); res.send(fileContent); } }); function generateETag(content) { return 'W/"' + require('crypto').createHash('md5').update(content).digest('hex') + '"'; }
app.get('/file', (req, res) => { const fileContent = fs.readFileSync('file.txt'); res.setHeader('Content-Type', 'text/plain'); res.send(fileContent); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No ETag, always full response | N/A | N/A | High network and paint cost due to full reload | [X] Bad |
| ETag with conditional requests | N/A | N/A | Low network cost, faster paint due to cached content | [OK] Good |