How to fix the white screen after build with create-react-app?

前端 未结 5 1587
心在旅途
心在旅途 2021-01-12 14:05

I used react-router-dom and I build my react-app. When I deploy it on the server, I get a blank page and the console is empty.

My App.js is :



        
5条回答
  •  被撕碎了的回忆
    2021-01-12 14:45

    Try changing basename="/" on your BrowserRouter to basename="/React". react-router needs this if used in a sub-directory.

    From the react-router docs:

    basename: The base URL for all locations. If your app is served from a sub-directory on your server, you'll want to set this to the sub-directory. A properly formatted basename should have a leading slash, but no trailing slash.

    Also change homepage in package.json to the url of your production target. homepage="." means it will work on every domain where it is located in the server root (and is also the default behaviour).

    From the React docs regarding deployment:

    By default, Create React App produces a build assuming your app is hosted at the server root. To override this, specify the homepage in your package.json, for example:

    "homepage": "http://mywebsite.com/relativepath",
    

    This will let Create React App correctly infer the root path to use in the generated HTML file.

提交回复
热议问题