Note: Always specify width and height attributes to prevent layout
shift (CLS). Use loading="eager" for above-the-fold images (first 2-3 images) and
loading="lazy" for below-the-fold content.
Load only when needed; reduces initial bundle size
Performance Impact: Default blocking scripts can delay First Contentful Paint (FCP) by 1-3
seconds. Always use async or defer unless script must execute before rendering.
Position scripts at end of <body> as fallback.
5. DOM Optimization and Minimal Markup
Technique
Description
Example
Impact
Reduce DOM Depth
Keep nesting to minimum (ideally <15 levels)
Flatten div soup; use CSS Grid/Flexbox
Faster rendering, layout recalculation
Minimize DOM Nodes
Target <1500 total nodes per page
Remove unnecessary wrappers, divs
Lower memory usage, faster querySelector
Semantic HTML
Use appropriate elements instead of generic divs/spans
Performance Metrics: Each additional 1000 DOM nodes increases memory by ~1MB and slows layout
by 10-15ms. Deep nesting (>20 levels) exponentially impacts CSS selector matching and style calculation.
6. Content Delivery and Caching Headers
Header/Technique
Syntax/Value
Description
Recommended Value
Cache-Control
Cache-Control: max-age=31536000
Define caching behavior and expiration time
Immutable assets: 1 year; Dynamic: no-cache
Immutable
Cache-Control: immutable
Resource will never change at this URL
Versioned/fingerprinted assets
ETag
ETag: "abc123"
Validate cached resource with server hash
Dynamic content, APIs
Last-Modified
Last-Modified: date
Timestamp of last resource modification
Static files without versioning
Compression
Content-Encoding: gzip|br
Compress text resources (HTML, CSS, JS)
Brotli (br) preferred; gzip fallback
CDN
Content Delivery Network
Distribute static assets globally
Cloudflare, CloudFront, Fastly
HTTP/2 Server Push
Link: </css/main.css>; rel=preload
Push critical resources before request
Critical CSS, fonts (use sparingly)
Service Worker Cache
JavaScript caching layer
Programmatic cache control, offline support
Progressive Web Apps (PWAs)
Example: HTML meta tags for caching and compression
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <!-- Prevent caching of HTML (always fetch fresh) --> <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"> <meta http-equiv="Pragma" content="no-cache"> <meta http-equiv="Expires" content="0"> <!-- Versioned assets can be cached long-term --> <link rel="stylesheet" href="/css/main.abc123.css"> <script src="/js/app.def456.js" defer></script> <!-- CDN-hosted resources --> <link rel="stylesheet" href="https://cdn.example.com/lib/v1.0.0/styles.css"></head><body> <!-- Register service worker for caching --> <script> if ('serviceWorker' in navigator) { navigator.serviceWorker.register('/sw.js'); } </script></body></html>
Asset Type
Cache Strategy
Cache-Control Value
Rationale
HTML Pages
No cache or short TTL
no-cache or max-age=300
Ensure users get latest content and metadata
Versioned CSS/JS
Long-term cache
max-age=31536000, immutable
Filename changes on update; safe to cache indefinitely
Images
Long cache with validation
max-age=86400 + ETag
Large files benefit from caching; validate occasionally
Fonts
Long-term cache
max-age=31536000
Rarely change; large files; cache indefinitely
API Responses
Conditional caching
no-cache + ETag
Validate on each request but avoid full download
Performance Optimization Checklist
Core Web Vitals: Focus on Largest Contentful Paint (LCP <2.5s), First Input Delay (FID
<100ms), and Cumulative Layout Shift (CLS <0.1). These metrics directly impact SEO rankings and user
experience.