Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Service-worker registration helper used by `root.tsx`. Kept in its own
|
||||
* module so it can be unit-tested without rendering a React tree.
|
||||
*
|
||||
* The function is a no-op when:
|
||||
* - `navigator.serviceWorker` is unavailable (SSR, older browsers).
|
||||
* - `window.location.protocol` is not http(s) — file:// previews would
|
||||
* otherwise throw SecurityError.
|
||||
* - `import.meta.env.DEV` is true — the dev server hot-reloads, and
|
||||
* a cached SW would shadow new bundles.
|
||||
*
|
||||
* Refs: docs/improvement-plan/10-compliance-polish/lighthouse.md (Step (d)).
|
||||
*/
|
||||
export type RegisterServiceWorkerOptions = {
|
||||
scriptUrl?: string;
|
||||
scope?: string;
|
||||
/** Inject an override container so tests can supply a fake navigator. */
|
||||
navigatorRef?: Navigator;
|
||||
/** Inject an override container so tests can disable DEV gating. */
|
||||
isDev?: boolean;
|
||||
};
|
||||
|
||||
export async function registerServiceWorker(options: RegisterServiceWorkerOptions = {}): Promise<ServiceWorkerRegistration | null> {
|
||||
const navigatorRef = options.navigatorRef ?? (typeof navigator === "undefined" ? undefined : navigator);
|
||||
if (!navigatorRef || !("serviceWorker" in navigatorRef)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (options.isDev) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return await navigatorRef.serviceWorker.register(options.scriptUrl ?? "/sw.js", {
|
||||
scope: options.scope ?? "/",
|
||||
});
|
||||
} catch (err) {
|
||||
// Swallow but log — a failed SW must never break the app render.
|
||||
if (typeof console !== "undefined") {
|
||||
console.warn("[pwa] service worker registration failed", err);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user