Where to Put a Favicon in a React Project
Both Vite and Create React App serve one static index.html, so a React favicon is set in that file, not inside a component. This guide covers where the icon files go in each setup, the link tags to add, subpath deployments, and how to check the built output.
Why the icon is declared in index.html
A React app mounts into a root element in the body, but a browser looks for icon tags in the head. In both tools that head is a plain HTML file you edit directly, so the favicon is a set of link tags rather than JSX. The framework renders the page; it does not build the icon links.
Files in a public folder are copied to the build output unchanged, so their URLs stay stable and the head can point at a fixed path instead of a hashed bundle filename.
| Detail | Where it lives |
|---|---|
| Vite entry file | index.html at the project root |
| Create React App entry file | public/index.html |
| Static assets in both | A public/ folder copied as-is to the build output |
| Icon links | Inside the head of that entry HTML file |
Vite: icons in public/, links in index.html
Vite copies everything under public/ to the root of dist/ without renaming it. Add the icon files there, then place the link tags in the head of the index.html at the project root. A leading slash resolves from the site root, which matches where the files land after a build.
Keep filenames identical in public/ and in the tags, or the browser logs a 404. The size guide explains each dimension.
<!-- index.html (Vite project root), inside <head> -->
<link rel="icon" type="image/svg+xml" href="/favicon.svg">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32.png">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="shortcut icon" href="/favicon.ico">
Create React App: use %PUBLIC_URL%
Create React App reads public/index.html and expects the %PUBLIC_URL% placeholder before each asset path. The build replaces it with the public URL, so icons still resolve when the app is served from a subdirectory rather than the domain root.
The starter template ships a favicon.ico, two PNG icons and a manifest.json in public/. Replacing those in place is simpler than adding a second set of tags. See ICO vs PNG for the format trade-off.
<!-- public/index.html (Create React App), inside <head> -->
<link rel="icon" href="%PUBLIC_URL%/favicon.ico">
<link rel="icon" type="image/png" sizes="32x32"
href="%PUBLIC_URL%/favicon-32.png">
<link rel="apple-touch-icon" sizes="180x180"
href="%PUBLIC_URL%/apple-touch-icon.png">
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
Why public/ and not an import from src/
Files imported from src/ are bundled, renamed with a content hash, and sometimes inlined when small. That suits images a component renders, but not a link tag that needs a predictable URL. The public folder leaves the filename untouched.
| Approach | What happens to the file |
|---|---|
| public/ plus a link tag | Copied unchanged; the URL stays /favicon.svg |
| import from src/ | Bundled and renamed with a content hash |
| Inline data URI | Embedded in HTML; no separate request, harder to edit |
Which files to include
A practical set covers modern tabs, older browsers and home screen shortcuts. Build it from one square image with the myfavicon generator, then drop the files into public/.
| File | Purpose |
|---|---|
| favicon.svg | Scales cleanly in browsers that support SVG icons |
| favicon-32.png | A small raster icon for tabs and bookmarks |
| apple-touch-icon.png | The iOS home screen image; avoid transparency |
| favicon.ico | A conventional fallback holding several sizes |
Serving the app under a subpath
If the site is deployed below a path such as /app/, a leading slash points at the domain root and the icons go missing. In Vite, set the base option so built URLs carry that prefix. Create React App handles it through %PUBLIC_URL% and the homepage field.
Rebuild and read the generated HTML to confirm each href carries the prefix. Why a favicon is not showing lists the usual path and cache mistakes.
// vite.config.js - deploy the build under /app/
import { defineConfig } from 'vite'
export default defineConfig({
base: '/app/',
})
Verify the result and fix a stale icon
Trust the served HTML over the tab. Inspect the head in DevTools and request each icon URL directly to confirm it returns the image. The favicon checker inspects a live URL the same way, without touching your project.
For home screen and installable apps, see Apple touch icon, the web manifest and PWA icons. The general add-a-favicon guide, the Next.js guide, the WordPress guide and the Shopify guide follow the same checks on other stacks.
FAQ
No. Both Vite and Create React App read a static index.html, so a link tag and icon files in public/ are enough.
Use public/. Files imported from src/ are renamed with a content hash, which makes the link URL hard to predict.
Browsers cache favicons for a long time. Try a private window, then change the URL with a new filename or a version query string.
You should not treat it as a ranking factor. Its job is recognition in tabs, bookmarks and home screen shortcuts.