简体   繁体   English

部署新的Webpack捆绑包会导致错误,直到页面刷新

[英]Deploying new Webpack bundle causes error until page refresh

We use webpack 1.x with React to bundle our application. 我们使用带有React的webpack 1.x捆绑我们的应用程序。 Furthermore, in order to bust caches when javascript files have changed we have our output set in webpack.config.js as: 此外,为了在javascript文件发生变化时破坏缓存,我们将webpack.config.js的输出设置为:

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. 突然之间,曾经存在的块没有,并且当index.html成功更新时,浏览器当前正在使用的一些块会对旧的,不存在的文件产生错误请求。

Is there a conventional way for webpack to handle the switch? webpack是否有传统的方式来处理交换机? Or within our React app to have it gracefully handle a bad component import. 或者在我们的React应用程序中,让它优雅地处理错误的组件导入。 We host on S3, which (like Meteor, I'm under the impression) falls back to index.html on a non-existent file request. 我们主持S3,它(就像Meteor,我的印象)在一个不存在的文件请求上回退到index.html。 In our app, this results in a Syntax error: Unexpected token < error because it is expecting javascript, not HTML. 在我们的应用程序中,这会导致Syntax error: Unexpected token <错误,因为它期待javascript,而不是HTML。

Edit : For averting the bad import in my React app, perhaps I could apply logic in the route's index.js file? 编辑 :为了避免我的React应用程序中的错误导入,也许我可以在路由的index.js文件中应用逻辑? Currently my getComponent calls look simply like this: 目前我的getComponent调用看起来像这样:

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

EDIT 2: Found an answer to my issue here. 编辑2: 在这里找到我的问题的答案。

What if you didn't delete the old files on S3? 如果您没有删除S3上的旧文件怎么办?

If you upload with a different chunkhash, presumably you could keep the old hash'ed files around for a while? 如果你使用不同的chunkhash上传,大概你可以将旧的哈希文件保留一段时间?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM