Imports and public/ · Local · No plugin

Optimize React Images — Before They Ever Reach Your Bundle

Compress the images behind your React imports and your public/ folder before you build. Nothing to configure in Vite or Webpack, and nothing re-encoded on every CI run.

Quick answer

In a React app an image reaches users one of two ways: through an import in src/, where the bundler hashes it (and inlines very small files as base64), or from public/, where it is copied byte for byte. Neither path re-encodes the pixels, so compress both folders with TinyPixels before you commit.

How to compress images before a React build

1

Download and open TinyPixels

Free to install on Mac or Windows — no account needed to start.

2

Resize oversized exports

Hero art exported at 2400 px for a 600 px slot is the usual culprit. Export near 2x the largest display size.

3

Compress, converting heroes if useful

Drop the folders into TinyPixels. For large banners, also generate WebP or AVIF to serve through <picture>.

4

Commit the compressed files

Every environment, from local dev to production, now serves the smaller file.

Where React images actually come from

A React component can reference an image three ways: import hero from './hero.png' (the bundler resolves it to a hashed URL), a plain string path like /logo.png that points into public/, or a url() inside a stylesheet. All three end up as ordinary files in your build output, at whatever size you committed.

That is why hero art and screenshots are the usual culprit. A designer exports a 2400 px illustration for a 600 px slot, someone imports it, and every visitor downloads it. React's rendering model does not help: reconciling components is cheap, but a 4 MB <img> is network and decode cost the framework never sees.

Compressing once in the repo means local dev, preview deploys and production all serve the same smaller file, and there is no per-bundler plugin to configure or keep upgrading.

Imports and public/ covered

Both folders get the same pass, even though the bundler treats them differently.

Resize first, then compress

Dimensions drive most of the bytes. Fix them before touching quality.

Pairs with <picture>

Generate WebP or AVIF next to the original and keep a JPEG or PNG fallback.

Hints still matter

Explicit width and height prevent layout shift. They do not shrink bytes, so use both.

What each React setup does with an image

SetupWhat it doesYour move
Vite, imported from src/Hashes and copies the file. Inlines it as base64 only under assetsInlineLimit (4 KiB by default).Compress before commit
Vite, file in public/Copied as-is and served from the root path.Compress before commit
Create React App (now deprecated)Inlines imports under about 10,000 bytes, otherwise emits a hashed file.Compress before commit
CSS url() in a stylesheetResolved by the bundler like an import.Compress the referenced file
Webpack image-minimizer pluginRe-compresses on every build, which adds CI time.Optional once files are pre-compressed

Defaults like these change between tool versions, so check your bundler's docs. The common thread: none of them re-encode the image content on their own.

Common mistakes with React project images

Importing the design export directly

The bundler will happily hash a 5 MB export without complaint. Resize and compress before the import, because nothing downstream will.

Assuming base64 inlining is a win

Inlined images become roughly a third larger as text and cannot be cached separately. Compress small icons anyway, and don't rely on inlining to save them.

Forgetting CSS backgrounds

Hero sections often use background-image in a stylesheet, which sits outside your JSX and is easy to miss in an image audit.

Lazy-loading the hero image

loading="lazy" on the above-the-fold image delays Largest Contentful Paint. Keep that one eager, lazy-load the rest, and compress both.

Frequently asked questions

Do I need a Webpack plugin to compress images in React?

No. An image-minimizer plugin works, but it re-compresses on every build. Compressing images once in the repo with TinyPixels keeps CI fast and the result identical everywhere.

Should I compress images in src/assets or public/?

Both. Imported files are bundled and hashed, while public/ files are served untouched. Neither is re-encoded, so each folder needs its own pass.

Does React optimize images automatically?

No. React is a UI library and has no image pipeline. Frameworks built on it, such as Next.js with its image component, add one; see the Next.js page for that case.

Does lazy loading replace compression?

No. Lazy loading changes when an image downloads; compression changes how many bytes it is. Visitors who scroll to a lazy image still download the full file.

Can I batch compress my whole React project at once?

Yes. Drop the assets and public image folders into TinyPixels and every file compresses in one pass, locally.

Shrink your React bundle today

Free to start. No credit card, no account, no cloud. See Pro pricing →