@dr.pogodin/react-utils
Version:
Collection of generic ReactJS components and utils
47 lines (41 loc) • 1.46 kB
JavaScript
/**
* Initialization of client-side environment.
*/
/* global window */
import { getBuildInfo } from "../shared/utils/isomorphy/buildInfo";
const buildInfo = getBuildInfo();
// TODO: Should be moved into buildInfo module?
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line no-console
console.warn('Dev mode: "BUILD_INFO" attached to the global "window"');
// eslint-disable-next-line no-underscore-dangle
window.__DEV_BUILD_INFO__ = BUILD_INFO;
}
/* TODO: A proper logger should be moved to `@dr.pogodin/react-utils`. */
const {
useServiceWorker
} = buildInfo;
if (useServiceWorker) {
const {
navigator
} = window;
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
void (async () => {
try {
// Note: no matter the "publicPath", we want to serve the service worker
// from the web app root, to allow it control any and all pages of the
// web app (otherwise, it will be restricted to the scope of its path).
// The server takes it into account.
const reg = await navigator.serviceWorker.register('/__service-worker.js');
// eslint-disable-next-line no-console
console.log('SW registered:', reg);
} catch (err) {
// eslint-disable-next-line no-console
console.log('SW registration failed:', err);
}
})();
});
}
}
//# sourceMappingURL=init.js.map