简体   繁体   English

在 Angular 渐进式 web 应用程序中强制重新加载远程索引。

[英]Enforcing reload of remote index.html in an Angular progressive web app

I have an Angular 8 web app that uses Angular PWA and service worker.我有一个使用 Angular PWA 和 service worker 的 Angular 8 web 应用程序。

The customer wants to add maintenance mode to the system to be able to cut off the users and display a simple HTML page with the downtime information.客户希望在系统中添加维护模式,以便能够切断用户并显示带有停机信息的简单 HTML 页面。

On the server side it works simply by returning 503 and a maintenance mode HTML page.在服务器端,它只需返回 503 和维护模式 HTML 页面即可工作。 While the page is based on a template, I cannot include it in my SPA webapp because customer's admins might want to adjust it for the situation (eg to explain how long the maintenance will last).虽然该页面基于模板,但我无法将其包含在我的 SPA webapp 中,因为客户的管理员可能希望根据情况对其进行调整(例如,解释维护将持续多长时间)。

The question boils down to this: how to enforce the web browser to show the remote index.html with maintenance text instead of the cached index.html?问题归结为:如何强制 web 浏览器显示远程 index.html 与维护文本而不是缓存的 index.html? How is it usually done in a typical Angular SPA application, that was created using a Visual Studio Angular SPA template?在使用 Visual Studio Angular SPA 模板创建的典型 Angular SPA 应用程序中,它通常是如何完成的?

The longer explanations:较长的解释:

The problem is the client side.问题出在客户端。 Since this is a PWA that mostly communicates with the back-end through API, there are three potential points when the user might receive 503 response:由于这是一个主要通过 API 与后端通信的 PWA,因此用户可能会收到 503 响应的三个潜在点:

  • when loading the site for the very first time.第一次加载网站时。 This is not a problem, users get the maintenance page immediately.这不是问题,用户立即获得维护页面。

  • when the site has been already loaded and the user is doing a normal page refresh (not full reload).当站点已经加载并且用户正在进行正常的页面刷新(不是完全重新加载)时。 This is a problem because the cached SPA root index.html is returned by the service worker instead of the modified index.html on the server.这是一个问题,因为缓存的 SPA 根 index.html 是由 service worker 返回的,而不是服务器上修改后的 index.html。 Fortunately, my SPA is doing some web requests immediately after launching, so I can catch 503 there, and thus it all boils down to the third point:幸运的是,我的 SPA 在启动后立即执行了一些 web 请求,所以我可以在那里捕获 503,因此这一切都归结为第三点:

  • the most often 503 status will be caught while the user is working on some task and the app calls my server API.当用户正在处理某些任务并且应用程序调用我的服务器 API 时,最常见的 503 状态将被捕获。 I have a global error handler (based on ErrorHandler) added to my HTTP service calls in my Angular code, so I can catch 503 response.我在 Angular 代码中的 HTTP 服务调用中添加了一个全局错误处理程序(基于 ErrorHandler),因此我可以捕获 503 响应。 But what should I do next?但是接下来我该怎么办? How do I force full reload of website, ignoring the service worker cache to enforce reloading of the remote index.html page?如何强制完全重新加载网站,忽略服务工作者缓存以强制重新加载远程 index.html 页面?

What have I already attempted:我已经尝试过什么:

As a quick&dirty workaround, I have added the following code in my error handler:作为一种快速而简单的解决方法,我在错误处理程序中添加了以下代码:

    if (error.status === 503) {
        // the server should have offline page. If not, the request will lead to 404.
        document.location.href = "offline.html?_=" + Math.floor(Math.random() * 1000000000);
    }

This assumes that the remote server has offline.html file instead of newly modified index.html.这里假设远程服务器有离线的.html文件而不是新修改的index.html。 The problem with this approach is that the user won't be able to simply continue refreshing the page because they'll get the offline.html and finally 404 instead of the revived index.html after the server has been restored from the maintenance mode.这种方法的问题在于,用户将无法简单地继续刷新页面,因为他们将在服务器从维护模式恢复后获得离线。html 最后是 404 而不是恢复的索引。html。 It would be nicer to force the maintenance text in the same index.html, but unfortunately, the browser keeps displaying the cached SPA index.html instead.最好在同一索引中强制维护文本。html,但不幸的是,浏览器一直显示缓存的 SPA 索引。html。

In case if someone else stumbles upon the same question: I ended up with such a bit ugly solution:万一其他人偶然发现了同样的问题:我最终得到了一个有点丑陋的解决方案:

    else if (error.status === 503) {
        // we assume the server has sent us entire offline html page contents
        // and we rewrite current page with it entirely
        document.open();
        // error is the response body
        document.write(error.error);
        document.close();
    }

This way:这边走:

  • if the user loads the website for the first time, they will receive the server's 503 response immediately如果用户第一次加载网站,他们会立即收到服务器的 503 响应
  • if the user loads the website from browser's cache, the website will receive 503 sometime later, but, most likely, very soon because of some immediate API requests, and then we come to the last:如果用户从浏览器的缓存中加载网站,网站将在稍后的某个时间收到 503,但很可能很快,因为一些即时的 API 请求,然后我们来到最后一个:
  • if the user receives 503 status when calling API, the entire visible document will get replaced with the text from the server.如果用户在调用 API 时收到 503 状态,则整个可见文档将被来自服务器的文本替换。

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

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