Why is React Webpack production build showing Blank page?

后端 未结 7 1301
北荒
北荒 2020-12-14 02:04

I\'m building a react app, and at the moment webpack-dev-server works just fine( the hello world text shows up ), But webpack -p shows blank pa

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-14 02:30

    I figured it out, I was using browserHistory without setting up a local server. If i changed it to hashHistory it worked. To test webpack production locally with react-router browser history i needed to do this Configure a Server:

    Your server must be ready to handle real URLs. When the app first loads at / it will probably work, but as the user navigates around and then hits refresh at /accounts/23 your web server will get a request to /accounts/23. You will need it to handle that URL and include your JavaScript application in the response.

    An express app might look like this:

    const express = require('express')
    const path = require('path')
    const port = process.env.PORT || 8080
    const app = express()
    
    // serve static assets normally
    app.use(express.static(__dirname + '/public'))
    
    // handle every other route with index.html, which will contain
    // a script tag to your application's JavaScript file(s).
    app.get('*', function (request, response){
      response.sendFile(path.resolve(__dirname, 'public', 'index.html'))
    })
    
    app.listen(port)
    console.log("server started on port " + port)
    

    And just in case anyone is deploying to firebase using react-router with browser-history do this:

    {
      "firebase": "",
      "public": "",
      "ignore": [
        "firebase.json",
        "**/.*",
        "**/node_modules/**"
      ],
      "rewrites": [
        {
          "source": "**",
          "destination": "/index.html"
        }
      ]
    }
    

提交回复
热议问题