UNPKG

@serwist/turbopack

Version:

A module that integrates Serwist into your Next.js / Turbopack application.

137 lines (136 loc) 3.71 kB
"use client"; import { c } from "react/compiler-runtime"; import { Serwist } from "@serwist/window"; import { isCurrentPageOutOfScope } from "@serwist/window/internal"; import { createContext, useContext, useEffect, useState } from "react"; import { jsx } from "react/jsx-runtime"; //#region src/lib/context.ts const SerwistContext = createContext(null); const useSerwist = () => { const context = useContext(SerwistContext); if (!context) throw new Error("[useSerwist]: 'SerwistContext' is not available."); return context; }; //#endregion //#region src/index.react.tsx /** * `@serwist/window` provider for Next.js apps. * @param options * @returns */ function SerwistProvider(t0) { const $ = c(17); const { swUrl, disable: t1, register: t2, cacheOnNavigation: t3, reloadOnOnline: t4, options, children } = t0; const disable = t1 === void 0 ? false : t1; const register = t2 === void 0 ? true : t2; const cacheOnNavigation = t3 === void 0 ? true : t3; const reloadOnOnline = t4 === void 0 ? true : t4; let t5; if ($[0] !== disable || $[1] !== options || $[2] !== register || $[3] !== swUrl) { t5 = () => { if (typeof window === "undefined") return null; if (disable) return null; const scope = options?.scope || "/"; if (!(window.serwist && window.serwist instanceof Serwist) && "serviceWorker" in navigator) { window.serwist = new Serwist(swUrl, { ...options, scope, type: options?.type || "module" }); if (register && !isCurrentPageOutOfScope(scope)) window.serwist.register(); } return window.serwist ?? null; }; $[0] = disable; $[1] = options; $[2] = register; $[3] = swUrl; $[4] = t5; } else t5 = $[4]; const [serwist] = useState(t5); let t6; let t7; if ($[5] !== cacheOnNavigation || $[6] !== serwist) { t6 = () => { const cacheUrls = async (url) => { if (!window.navigator.onLine || !url) return; serwist?.messageSW({ type: "CACHE_URLS", payload: { urlsToCache: [url] } }); }; const cacheCurrentPathname = () => cacheUrls(window.location.pathname); const pushState = history.pushState; const replaceState = history.replaceState; if (cacheOnNavigation) { history.pushState = (...t8) => { const args = t8; pushState.apply(history, args); cacheUrls(args[2]); }; history.replaceState = (...t9) => { const args_0 = t9; replaceState.apply(history, args_0); cacheUrls(args_0[2]); }; window.addEventListener("online", cacheCurrentPathname); } return () => { history.pushState = pushState; history.replaceState = replaceState; window.removeEventListener("online", cacheCurrentPathname); }; }; t7 = [serwist, cacheOnNavigation]; $[5] = cacheOnNavigation; $[6] = serwist; $[7] = t6; $[8] = t7; } else { t6 = $[7]; t7 = $[8]; } useEffect(t6, t7); let t8; let t9; if ($[9] !== reloadOnOnline) { t8 = () => { const reload = _temp; if (reloadOnOnline) window.addEventListener("online", reload); return () => { window.removeEventListener("online", reload); }; }; t9 = [reloadOnOnline]; $[9] = reloadOnOnline; $[10] = t8; $[11] = t9; } else { t8 = $[10]; t9 = $[11]; } useEffect(t8, t9); let t10; if ($[12] !== serwist) { t10 = { serwist }; $[12] = serwist; $[13] = t10; } else t10 = $[13]; let t11; if ($[14] !== children || $[15] !== t10) { t11 = /* @__PURE__ */ jsx(SerwistContext.Provider, { value: t10, children }); $[14] = children; $[15] = t10; $[16] = t11; } else t11 = $[16]; return t11; } function _temp() { return location.reload(); } //#endregion export { SerwistProvider, useSerwist }; //# sourceMappingURL=index.react.mjs.map