Deploying new Webpack bundle causes error until page refresh
Asked Answered
F

1

9

We use webpack 1.x with React to bundle our application. Furthermore, in order to bust caches when javascript files have changed we have our output set in webpack.config.js as:

output: {
    path: __dirname + '/dist',
    filename: 'index_bundle.[chunkhash:10].js',
    publicPath: '/'
},

This successfully accomplishes our goal of changing file names when new versions of the app are deployed. And on a successful deploy, one can see that the new files are there.

The wrinkle comes in when a user is on the app during a deploy. Suddenly, chunks that were once there are not, and while the index.html successfully updates, some chunks that are currently in use by the browser make bad requests for old, non-existent files.

Is there a conventional way for webpack to handle the switch? Or within our React app to have it gracefully handle a bad component import. We host on S3, which (like Meteor, I'm under the impression) falls back to index.html on a non-existent file request. In our app, this results in a Syntax error: Unexpected token < error because it is expecting javascript, not HTML.

Edit: For averting the bad import in my React app, perhaps I could apply logic in the route's index.js file? Currently my getComponent calls look simply like this:

getComponent(nextState, callback) {
  require.ensure([], (require) => {
    callback(null, require('./components/HomePage').default);
  });
}

EDIT 2: Found an answer to my issue here.

Francois answered 4/5, 2017 at 18:55 Comment(2)
Something is not right. If you are being served a html (because of a non-existent file) you should answer it with HTTP 404 which forces the browser to ignore the file and skip parsing.Subito
The default for static website hosting on S3 is to have a file return on error (incl. not being able to find said file). That being said, even so, is there a way for my React app to see the 404 (or wrong file type) and avert an error of any kind? the getComponent of my route index.js files has been added aboveFrancois
B
1

What if you didn't delete the old files on S3?

If you upload with a different chunkhash, presumably you could keep the old hash'ed files around for a while?

Bullheaded answered 4/5, 2017 at 19:35 Comment(1)
This is a potential option. Ideally we would like a way to not have to do that as we currently delete old files during deployment.Francois

© 2022 - 2024 — McMap. All rights reserved.