REFACTOR: Use the session for the `ServiceWorkerURL`

This commit is contained in:
Robin Ward 2020-08-06 15:24:43 -04:00
parent 036a79d273
commit db443d2289
2 changed files with 8 additions and 11 deletions

View File

@ -4,28 +4,25 @@ import getAbsoluteURL from "discourse-common/lib/get-url";
export default { export default {
name: "register-service-worker", name: "register-service-worker",
initialize() { initialize(container) {
const isSecured = const isSecured =
document.location.protocol === "https:" || document.location.protocol === "https:" ||
location.hostname === "localhost"; location.hostname === "localhost";
const isSupported = isSecured && "serviceWorker" in navigator; if (isSecured && "serviceWorker" in navigator) {
let { serviceWorkerURL } = container.lookup("session:main");
if (isSupported) { const caps = container.lookup("capabilities:main");
const caps = Discourse.__container__.lookup("capabilities:main");
const isAppleBrowser = const isAppleBrowser =
caps.isSafari || caps.isSafari ||
(caps.isIOS && (caps.isIOS &&
!window.matchMedia("(display-mode: standalone)").matches); !window.matchMedia("(display-mode: standalone)").matches);
if (Discourse.ServiceWorkerURL && !isAppleBrowser) { if (serviceWorkerURL && !isAppleBrowser) {
navigator.serviceWorker.getRegistrations().then(registrations => { navigator.serviceWorker.getRegistrations().then(registrations => {
for (let registration of registrations) { for (let registration of registrations) {
if ( if (
registration.active && registration.active &&
!registration.active.scriptURL.includes( !registration.active.scriptURL.includes(serviceWorkerURL)
Discourse.ServiceWorkerURL
)
) { ) {
this.unregister(registration); this.unregister(registration);
} }
@ -33,7 +30,7 @@ export default {
}); });
navigator.serviceWorker navigator.serviceWorker
.register(getAbsoluteURL(`/${Discourse.ServiceWorkerURL}`)) .register(getAbsoluteURL(`/${serviceWorkerURL}`))
.catch(error => { .catch(error => {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.info(`Failed to register Service Worker: ${error}`); console.info(`Failed to register Service Worker: ${error}`);

View File

@ -64,12 +64,12 @@ export default {
app.ThemeSettings = PreloadStore.get("themeSettings"); app.ThemeSettings = PreloadStore.get("themeSettings");
app.LetterAvatarVersion = setupData.letterAvatarVersion; app.LetterAvatarVersion = setupData.letterAvatarVersion;
app.MarkdownItURL = setupData.markdownItUrl; app.MarkdownItURL = setupData.markdownItUrl;
app.ServiceWorkerURL = setupData.serviceWorkerUrl;
I18n.defaultLocale = setupData.defaultLocale; I18n.defaultLocale = setupData.defaultLocale;
window.Logster = window.Logster || {}; window.Logster = window.Logster || {};
window.Logster.enabled = setupData.enableJsErrorReporting === "true"; window.Logster.enabled = setupData.enableJsErrorReporting === "true";
Session.currentProp("serviceWorkerURL", setupData.serviceWorkerUrl);
Session.currentProp("assetVersion", setupData.assetVersion); Session.currentProp("assetVersion", setupData.assetVersion);
Session.currentProp( Session.currentProp(