Skip to main content

🎨 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:


πŸ”Ή 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​

OptimizationMethod
Optimize ImagesUse WebP/AVIF, Lazy Loading, Compression
Optimize FontsUse System Fonts, Subset Fonts, Font Display Swap
Minify CSS & JSTerser, CSSNano, PurgeCSS for Tailwind
Use a CDNStore static assets on a CDN
Enable CompressionGzip/Brotli via Webpack or Vite