Search code examples
reactjscreate-react-app

React serves default content for incorrect paths


I used create-react-app to play with react. I noticed that no matter what I type in after localhost:3000/ (which is the default url on dev machine) I always get the content from localhost:3000

This means I cannot access images so that sort of means I can't really do anything with it.

Is this a bug or is it some awesome new feature that I just don't understand? How can this be switched off?


Solution

  • Create React App uses historyApiFallback for the webpack-dev-server, which means that all requests that give a 404 response will fall back to load the index.html file.

    If you e.g. add an image to the public folder, you can see that the image takes precedence over the index.html file.

    You can also import the image with the help of Webpack and use the import as src in your code, and Webpack will make sure the image ends up in the final build folder for you.

    import img from './image.png';