Web Performance Optimization
Page speed directly impacts user experience, search rankings, and conversion rates. Studies show that a one-second delay in page load can reduce conversions by up to 7%. This guide covers practical techniques to make your websites faster.
Core Web Vitals
Google's Core Web Vitals measure three key aspects of user experience:
- LCP (Largest Contentful Paint): Measures loading performance. Should occur within 2.5 seconds.
- FID (First Input Delay) / INP (Interaction to Next Paint): Measures interactivity. INP should be under 200ms.
- CLS (Cumulative Layout Shift): Measures visual stability. Should be less than 0.1.
Optimize these metrics first, as they directly affect your SEO ranking.
Image Optimization
Images are often the largest resources on a page. Proper optimization yields quick wins.
<!-- Modern formats with fallbacks -->
<picture>
<source srcset="hero.avif" type="image/avif">
<source srcset="hero.webp" type="image/webp">
<img src="hero.jpg" alt="Hero image" loading="lazy" width="1200" height="630">
</picture>
<!-- Native lazy loading for below-the-fold images -->
<img src="blog-post.jpg" alt="Blog content" loading="lazy">
/* Prevent layout shift by reserving space */
img {
max-width: 100%;
height: auto;
aspect-ratio: 1200 / 630; /* Match your image dimensions */
}
Critical Rendering Path
Minimize the number of resources required to render the first paint:
<!-- Inline critical CSS -->
<style>
/* Above-the-fold styles only */
.hero { display: flex; align-items: center; min-height: 100vh; }
.nav { position: fixed; top: 0; width: 100%; }
</style>
<!-- Defer non-critical CSS -->
<link rel="preload" href="/styles/non-critical.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="/styles/non-critical.css"></noscript>
<!-- Defer non-critical JavaScript -->
<script src="/app.js" defer></script>
<script src="/analytics.js" defer></script>
Caching Strategies
Effective caching reduces repeat load times dramatically:
# Server-side caching headers
location ~* \.(js|css|png|jpg|svg)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
# Cache API responses appropriately
location /api/ {
proxy_cache_bypass $http_cache_control;
add_header Cache-Control "public, max-age=60";
}
On the client side, use Service Workers for offline caching:
// Service Worker cache strategy
self.addEventListener('fetch', (event) => {
if (event.request.url.includes('/api/')) {
// Network first, cache fallback for API calls
event.respondWith(
fetch(event.request).catch(() => caches.match(event.request))
);
} else {
// Cache first for static assets
event.respondWith(
caches.match(event.request).then(cached => cached || fetch(event.request))
);
}
});
Code Splitting
Split your JavaScript bundle so users only download what they need:
// React lazy loading with suspense
const Dashboard = lazy(() => import('./pages/Dashboard'));
const Settings = lazy(() => import('./pages/Settings'));
function App() {
return (
<Suspense fallback={<Loader />}>
<Routes>
<Route path="/dashboard" element={<Dashboard />} />
<Route path="/settings" element={<Settings />} />
</Routes>
</Suspense>
);
}
Conclusion
Performance optimization is an ongoing process, not a one-time task. Start by measuring your current performance with tools like Lighthouse, WebPageTest, or Chrome DevTools. Identify the biggest bottlenecks — often images and unoptimized JavaScript — and tackle them first. Set up performance budgets in your CI pipeline to catch regressions before they reach production. Small improvements compound over time, and even a 20% speed boost can make a noticeable difference in user satisfaction.