π¨ React Asset Optimization Guide
Optimizing assets (images, fonts, and other static files) in a React app improves performance, reduces load time, and enhances the user experience. Below are best practices for optimizing assets in your React project.
πΉ Step 1: Optimize Imagesβ
Large images can significantly slow down your website. Use the following strategies:
β Use Modern Image Formats (WebP, AVIF)β
WebP and AVIF offer better compression and smaller file sizes compared to PNG/JPEG.
<img src="image.webp" alt="Optimized Image" />
β Enable Lazy Loadingβ
Lazy loading ensures that images load only when they are in the viewport.
<img src="large-image.jpg" loading="lazy" alt="Lazy Loaded Image" />
β Use Responsive Imagesβ
Use different image sizes for different screen resolutions.
<picture>
<source srcSet="image-small.webp" media="(max-width: 600px)" />
<source srcSet="image-large.webp" media="(min-width: 601px)" />
<img src="image-fallback.jpg" alt="Responsive Image" />
</picture>
β Compress Imagesβ
Use tools like:
- TinyPNG (https://tinypng.com)
- ImageOptim (https://imageoptim.com)
- Squoosh (https://squoosh.app)
πΉ Step 2: Optimize Fontsβ
Fonts can be a hidden performance bottleneck if not optimized.
β Use System Fonts (Avoid Large Font Files)β
System fonts donβt require downloads, improving load times.
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto;
β Use Font Subsettingβ
Only include the characters you need.
β Self-Host Fonts or Use Google Fonts with Display Swapβ
@font-face {
font-family: 'CustomFont';
src: url('/fonts/custom-font.woff2') format('woff2');
font-display: swap;
}
If using Google Fonts, add display=swap
to prevent render-blocking.
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" />
πΉ Step 3: Optimize CSS and JS Assetsβ
β Minify CSS and JavaScriptβ
Use tools like Terser and CSSNano.
npm install terser cssnano --save-dev
For Webpack, enable minification:
optimization: {
minimize: true,
minimizer: [new TerserPlugin()],
}
β Remove Unused CSSβ
If using Tailwind CSS, enable PurgeCSS to remove unused styles:
module.exports = {
purge: ["./src/**/*.js", "./public/index.html"],
};
πΉ Step 4: Use Content Delivery Network (CDN)β
A CDN caches assets across global servers for faster load times.
- Use Cloudflare, Netlify, or AWS S3 + CloudFront.
- Store static assets like images, fonts, and videos on a CDN.
<img src="https://cdn.example.com/images/optimized-image.webp" alt="CDN Image" />
πΉ Step 5: Enable Gzip or Brotli Compressionβ
Compressed assets load faster by reducing file size.
β Enable Brotli Compression in Viteβ
npm install vite-plugin-compression
Modify vite.config.js
:
import compression from "vite-plugin-compression";
export default {
plugins: [compression()],
};
β Summary: Key Steps for Asset Optimizationβ
Optimization | Method |
---|---|
Optimize Images | Use WebP/AVIF, Lazy Loading, Compression |
Optimize Fonts | Use System Fonts, Subset Fonts, Font Display Swap |
Minify CSS & JS | Terser, CSSNano, PurgeCSS for Tailwind |
Use a CDN | Store static assets on a CDN |
Enable Compression | Gzip/Brotli via Webpack or Vite |