Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is React Webpack production build showing Blank page?

Tags:

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 page. For the Production build The network tab under chrome dev tools, shows index.html and index_bundle.js to have size 0 B(see picture)enter image description here But That is clearly not the case HTML file size is 227 B & index_bundle.js file size is 195Kb(see picture)

Also Chrome Devtools Elements Tab shows the following(see picture) enter image description here

My webpack config file looks like this:enter image description here

like image 919
jasan Avatar asked May 31 '16 16:05

jasan


People also ask

Why use webpack instead of create React app?

Configuring webpack provides complete control over the development environment, while initializing with Create React App prevents any custom configuration by default.

Is webpack necessary for React?

Well, we don't necessarily need webpack to work with React, other alternatives could be Browserify, Parsel, Brunch, etc, but honestly, I don't know how well they fit in with React. js. Webpack is the most widely used and an accepted module bundler and task runner throughout React. js community.


2 Answers

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": "<YOUR-FIREBASE-APP>",   "public": "<YOUR-PUBLIC-DIRECTORY>",   "ignore": [     "firebase.json",     "**/.*",     "**/node_modules/**"   ],   "rewrites": [     {       "source": "**",       "destination": "/index.html"     }   ] } 
like image 178
jasan Avatar answered Oct 27 '22 07:10

jasan


use

import { HashRouter } from 'react-router-dom'; 

instead of

import { BrowserRouter} from 'react-router-dom'; 

and don't forget to replace it in your routes code

<HashRouter>   ... </HashRouter> 
like image 39
Yecodeo Avatar answered Oct 27 '22 06:10

Yecodeo