In a web build, why does a path string to an image under `src/` 404 in production, and how does a public pass-through folder differ?
answer
- what the build can see
- a string is not a dependency
- only emitted files are deployed
- copied verbatim, never hashed
- missing file: no build error
basics
~20 sA path string is not an import, so the build never emits the file and the source folder is not deployed. A public folder is copied verbatim to the site root, but nothing checks that referenced files exist.
solid answer
~50 sA build only ships files it reaches through the module graph: imports, `url()` in imported CSS, recognised `new URL()` calls and references in the entry HTML. A string such as `'/src/images/hero-fern.jpg'` is opaque text. It can work in development because a dev server may serve the source tree, but the production output contains only emitted files, so the request 404s. Importing the image fixes it: the build resolves the file, fails if it is missing, emits it and hands you the final URL. A **public pass-through folder** is the other route: its files are copied to the output root as they are, never renamed or transformed, and referenced by a root-absolute path such as `/favicon.ico`. That suits files that need a fixed name, but the build never resolves those strings, so a typo is a runtime 404, not a build error.
code
javascript · 10 lines// can work in development, 404s in production: the build never sees this string
heroImg.src = '/src/images/hero-fern.jpg'
// resolved, emitted and rewritten by the build; a missing file fails the build
import heroUrl from './images/hero-fern.jpg'
heroImg.src = heroUrl
// public/favicon.ico is copied to the output root as-is;
// nothing checks this path until a browser requests it
iconLink.href = '/favicon.ico'go deeper
Recall that a build ships only what its graph reaches, so a path string to a source file breaks after deployment. Import the file, or use the public folder with a root path.
Explain the difference: imports are resolved, verified and renamed, while public files are copied verbatim and referenced by absolute path without any check.
Diagnose dev-only working paths quickly, decide which files truly need fixed names, and add a check for public-folder references because the build will not catch them.
Set the convention across apps: import by default, keep the public folder for contract files, and decide how unverified references are caught before release.
## What a build can see A production build does not copy your source folder to the server. It starts from the entry points, follows every **dependency** it can recognise, and writes out only what that walk reached. In a typical web build the recognised dependencies are: - `import` statements and `import()` calls, including imports of images, fonts and other assets; - `url()` references inside stylesheets that are themselves imported; - the standard `new URL('./file', import.meta.url)` pattern with a literal path; - `src` and `href` references in the entry HTML file, which many app builds process too. Anything else is just a string. The build has no way to know that `'/src/images/hero-fern.jpg'` names a file, so it does not resolve it, does not emit it and does not rewrite it. ## Why the string works in development During development, many dev servers serve files straight from the project, so a request for `/src/images/hero-fern.jpg` happens to find the source file. In production the server only has the build output: emitted files under names the build chose, usually with content hashes, in an output folder. The source path does not exist there, and the browser gets a **404**. The bug is invisible until the first deploy because the dev server was, in effect, covering for it. ## The two correct routes **Import the file.** The build resolves it at build time, which means: 1. a missing or misspelt file **fails the build**, before anything ships; 2. the file is emitted (or inlined if it is tiny) and gets the build's naming; 3. your code receives the final URL, whatever it turns out to be. **Put it in the public pass-through folder.** Most application build setups have one folder whose contents are copied to the root of the output **verbatim**: no hashing, no transformation, no inlining, no analysis. You reference those files by a **root-absolute path** (`/robots.txt`, `/favicon.ico`), because their location in the output mirrors their location in the folder. | | Imported asset | Public pass-through file | Bare path string to source | |---|---|---|---| | Seen by the build | Yes | Copied, never resolved | No | | Missing file | Build error | Runtime 404 | Runtime 404 | | Output name | Chosen by the build, often hashed | Exactly as in the folder | Not emitted at all | | Transformed or inlined | Can be | Never | Never | | How code refers to it | The URL the import returns | Root-absolute path | Breaks in production | ## When the public folder is the right home The public folder exists for files whose **name or location is part of a contract** with something outside your code: - files crawlers or browsers request by a fixed path, such as `robots.txt` or the favicon; - files a third party verifies at an exact URL; - files referenced from outside the bundle entirely, such as a document linked from an email. For anything your own code references, an import is the better default: it is checked at build time, gets content-based names and is processed like the rest of the graph. ## What the public folder costs - **No verification.** Because the build never resolves those strings, a renamed or deleted file is discovered by a user, not by CI. Some teams add a link-check step for exactly this reason. - **No content-based names.** A file that keeps its name across releases cannot be cached as aggressively as a hashed one; what cache policy to apply is a separate decision. - **Sub-path deployments.** A root-absolute path assumes the site lives at `/`. When the collection viewer is deployed under `/collection/`, those paths also need the site's base prefix; how a build applies that prefix is its own subject. ## Diagnosing the 404 When an image loads in development and 404s after deployment, check in this order: 1. Does the code reference the file through an import or a recognised pattern, or through a string? 2. If it is a string, is the file in the public folder, and does the path match its position there? 3. If it is in the public folder, is the site served from the root, or does the path need the base prefix?
- Why does a typo in an imported asset's path fail the build, while the same typo in a public-folder path does not?An import is a dependency the build must resolve to a real file before it can emit anything, so an unresolvable path is an error. A public-folder path is only a string in your code; the build copies the folder without reading your strings, so nothing compares the two until a browser requests the URL and gets a 404.
- Which files belong in the public folder rather than being imported?Files whose exact name or location is a contract with something outside your code: `robots.txt`, the favicon, a verification file a third party fetches by fixed URL, or a document linked from outside the site. Files your own code references are better imported, so the build checks them and names them.
- If a path works in the dev server, it works the same in production.
- Files in the public folder get content-hashed names like imported assets.
- The build fails when a public-folder file referenced in code is missing.
- The public folder is the best default home for every image.
- A misspelt path in an image import only shows up as a runtime 404.