Angular - Load生成块文件在部署后失败

时间:2018-02-06 15:13:57

标签: angular http nginx jenkins angular-cli

我的客户端整天都在打开浏览器,所以在我进行部署后,他们会看到应用程序坏了,他们需要重新加载页面来修复它。

由于@ angular / cli生成版本添加了NO-CACHE哈希,服务器无法加载块文件。

错误:error: Uncaught (in promise): Error: Loading chunk 11 failed.

我想在部署后重新加载页面。

这些是我的工具:

  • 我可以访问我的NGINX配置。
  • 我可以访问Jenkins(蓝海)
  • 我在项目中实现了HttpClient拦截器。

2 个答案:

答案 0 :(得分:2)

我管理错误的路由替换它的错误处理程序并重新加载,如果它发现加载块的错误。

// Keep the original error handler
const oldHandler = this.router.errorHandler;
// Replace route error handler
this.router.errorHandler =  (err: any) => {
  // Check if there is an error loading the chunk
  if (err.originalStack && err.originalStack.indexOf('Error: Loading chunk') >= 0) {
    // Check if is the first time the error happend
    if (localStorage.getItem('lastChunkError') !== err.originalStack) {
      // Save the last error to avoid an infinite reload loop if the chunk really does not exists after reload
      localStorage.setItem('lastChunkError', err.originalStack);
      location.reload(true);
    } else {
      // The chunk really does not exists after reload
      console.error('We really don\'t find the chunk...');
    }
  }
  // Run original handler
  oldHandler(err);
};

我希望它可以帮到你

答案 1 :(得分:1)

我解决了这个问题,将我的应用程序移到AWS并在S3中保存文件!

相关问题