Webpack Build Issue - Static Images in public Folder (React Project) #187578
-
BodyIn my React project with a custom Webpack setup, images stored in the public folder render correctly when running How should I properly handle static images in the public folder so they are included and accessible in the production build? Should they be copied during the build process, or is there a recommended best practice for this scenario. Guidelines
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
|
Hey |
Beta Was this translation helpful? Give feedback.
-
|
This happens because You have two common solutions: ✅ Option 1 (Recommended for true static files): Use If the images should stay in Then configure Webpack to copy the This is the correct approach for:
✅ Option 2 (Recommended for app assets): Move images into Move images into This allows:
This is better for:
|
Beta Was this translation helpful? Give feedback.
This happens because
webpack-dev-serverserves thepublicfolder in development, but Webpack does not automatically copy it intodistduring a production build.You have two common solutions:
✅ Option 1 (Recommended for true static files): Use
copy-webpack-pluginIf the images should stay in
publicand not be processed by Webpack, install:npm install copy-webpack-plugin --save-devThen configure Webpack to copy the
publicfolder intodistduring build. This ensures the images exist in production exactly like they do in development.This is the correct approach for:
✅ Option 2 (Recommended for app assets): …