react production build 404 not found

前端 未结 2 1447
暗喜
暗喜 2021-02-10 19:53

Hello i need to deploy react app.

To achieve that i run : \"npm run build\"

after that in my vhost.conf i\'ve added vhost



        
2条回答
  •  悲哀的现实
    2021-02-10 20:42

    This is a common issue that comes up for SPA. In SPA, mostly the routing happens on client side. In your case, mostly react-router should be doing the job. Since the whole js is bundled as a single file and is served in index.html, you need to serve index.html for all paths that is non-existing in your server.

    You have to add a config like this

    RewriteEngine On  
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
    RewriteRule ^ - [L]
    
    RewriteRule ^ /index.html [L]
    

    So, if there is no matching path in your server, the index.html would get served. Then the javascript would execute and react-router(client side routing) will take over and display the correct component for the route.

    This is true for most SPA, where the routing happens on client side.

提交回复
热议问题