UNPKG

2.75 kBJavaScriptView Raw
1/* eslint strict: 0, no-var: 0 */
2"use strict";
3
4/*
5 * This is a separate file for two reasons:
6 * 1. CSP policy does not allow inline javascript
7 * 2. It has to be a small javascript executed before all other scripts,
8 * so that the timeout can be triggered while slow JS is loading
9 */
10
11(function() {
12 var msg = document.getElementById("loading-page-message");
13
14 if (msg) {
15 msg.textContent = "Loading the app…";
16
17 document.getElementById("loading-reload").addEventListener("click", function() {
18 location.reload(true);
19 });
20 }
21
22 var displayReload = function displayReload() {
23 var loadingReload = document.getElementById("loading-reload");
24
25 if (loadingReload) {
26 loadingReload.style.visibility = "visible";
27 }
28 };
29
30 var loadingSlowTimeout = setTimeout(function() {
31 var loadingSlow = document.getElementById("loading-slow");
32
33 // The parent element, #loading, is being removed when the app is loaded.
34 // Since the timer is not cancelled, `loadingSlow` can be not found after
35 // 5s. Wrap everything in this block to make sure nothing happens if the
36 // element does not exist (i.e. page has loaded).
37 if (loadingSlow) {
38 loadingSlow.style.visibility = "visible";
39 displayReload();
40 }
41 }, 5000);
42
43 window.g_LoungeErrorHandler = function LoungeErrorHandler(e) {
44 var message = document.getElementById("loading-page-message");
45 message.textContent =
46 "An error has occurred that prevented the client from loading correctly.";
47
48 var summary = document.createElement("summary");
49 summary.textContent = "More details";
50
51 var data = document.createElement("pre");
52 data.textContent = e.message; // e is an ErrorEvent
53
54 var info = document.createElement("p");
55 info.textContent = "Open the developer tools of your browser for more information.";
56
57 var details = document.createElement("details");
58 details.appendChild(summary);
59 details.appendChild(data);
60 details.appendChild(info);
61 message.parentNode.insertBefore(details, message.nextSibling);
62
63 window.clearTimeout(loadingSlowTimeout);
64 displayReload();
65 };
66
67 window.addEventListener("error", window.g_LoungeErrorHandler);
68
69 // Trigger early service worker registration
70 if ("serviceWorker" in navigator) {
71 navigator.serviceWorker.register("service-worker.js");
72
73 // Handler for messages coming from the service worker
74 var messageHandler = function ServiceWorkerMessageHandler(event) {
75 if (event.data.type === "fetch-error") {
76 window.g_LoungeErrorHandler({
77 message: `Service worker failed to fetch an url: ${event.data.message}`,
78 });
79
80 // Display only one fetch error
81 navigator.serviceWorker.removeEventListener("message", messageHandler);
82 }
83 };
84
85 navigator.serviceWorker.addEventListener("message", messageHandler);
86 }
87})();