Angular - 部署后加载生产 block 文件失败

标签 angular http nginx jenkins angular-cli

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

由于@angular/cli 生产构建添加了 NO-CACHE 哈希,服务器无法加载 block 文件。

错误:错误:未捕获( promise ):错误:加载 block 11 失败。

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

这些是我的工具:

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

最佳答案

我管理路由的错误,替换它的错误处理程序并在发现加载 block 错误时重新加载。

// 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);
};

希望对你有帮助

关于Angular - 部署后加载生产 block 文件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48646216/

相关文章:

javascript - 是否可以在 Styles.scss 中动态导入 Variables.scss 文件?

AngularFirestore : Property 'id' does not exist on type 'QueryDocumentSnapshot<any>'

json - AngularJS 获取 JSON 的 HTTP 状态 0

rest - 如何在没有任何扩展的情况下从 Google Chrome 发送 POST HTTP 请求?

c - Nginx 模块获取客户端 ssl 证书

angular - 将 HTML 片段附加到 Angular 6 中的 View

Angular 10 : mat-elevation-z is not working for any component

rest - HTTP Rest 请求生成器脚本工具

nginx - 如何在 CDN 后面创建反向代理(另一个反向代理)

angularjs - 在angularJS前端出现CORS错误