{"version":3,"file":"index.react.mjs","names":["createContext","useContext","SerwistContext","useSerwist","context","Error","_c","Serwist","isCurrentPageOutOfScope","useEffect","useState","swUrl","disable","register","cacheOnNavigation","reloadOnOnline","options","children","serwist","SerwistProvider","t0","$","t1","t2","t3","t4","undefined","t5","window","scope","navigator","type","t6","cacheUrls","url","onLine","messageSW","payload","urlsToCache","cacheCurrentPathname","location","pathname","pushState","history","replaceState","t7","args","apply","t8","args_0","addEventListener","removeEventListener","t10","t9","reload","_temp","t11","t12"],"sources":["../src/lib/context.ts","../src/index.react.tsx"],"sourcesContent":["import type { Serwist } from \"@serwist/window\";\nimport { createContext, useContext } from \"react\";\n\nexport interface SerwistContextValues {\n  serwist: Serwist | null;\n}\n\nexport const SerwistContext = createContext<SerwistContextValues>(null!);\n\nexport const useSerwist = () => {\n  const context = useContext(SerwistContext);\n  if (!context) {\n    throw new Error(\"[useSerwist]: 'SerwistContext' is not available.\");\n  }\n  return context;\n};\n","\"use client\";\nimport { Serwist } from \"@serwist/window\";\nimport { isCurrentPageOutOfScope } from \"@serwist/window/internal\";\nimport { type JSX, type ReactNode, useEffect, useState } from \"react\";\nimport { SerwistContext, useSerwist } from \"./lib/context.js\";\n\nexport interface SerwistProviderProps {\n  swUrl: string;\n  disable?: boolean;\n  register?: boolean;\n  cacheOnNavigation?: boolean;\n  reloadOnOnline?: boolean;\n  options?: RegistrationOptions;\n  children?: ReactNode;\n}\n\ndeclare global {\n  interface Window {\n    serwist: Serwist;\n  }\n}\n\n/**\n * `@serwist/window` provider for Next.js apps.\n * @param options\n * @returns\n */\nexport function SerwistProvider({\n  swUrl,\n  disable = false,\n  register = true,\n  cacheOnNavigation = true,\n  reloadOnOnline = true,\n  options,\n  children,\n}: SerwistProviderProps): JSX.Element {\n  const [serwist] = useState(() => {\n    if (typeof window === \"undefined\") return null;\n    if (disable) return null;\n    const scope = options?.scope || \"/\";\n    if (!(window.serwist && window.serwist instanceof Serwist) && \"serviceWorker\" in navigator) {\n      window.serwist = new Serwist(swUrl, { ...options, scope, type: options?.type || \"module\" });\n      if (register && !isCurrentPageOutOfScope(scope)) {\n        void window.serwist.register();\n      }\n    }\n    return window.serwist ?? null;\n  });\n  useEffect(() => {\n    const cacheUrls = async (url?: string | URL | null | undefined) => {\n      if (!window.navigator.onLine || !url) {\n        return;\n      }\n      serwist?.messageSW({\n        type: \"CACHE_URLS\",\n        payload: { urlsToCache: [url] },\n      });\n    };\n    const cacheCurrentPathname = () => cacheUrls(window.location.pathname);\n    const pushState = history.pushState;\n    const replaceState = history.replaceState;\n\n    if (cacheOnNavigation) {\n      history.pushState = (...args) => {\n        pushState.apply(history, args);\n        cacheUrls(args[2]);\n      };\n      history.replaceState = (...args) => {\n        replaceState.apply(history, args);\n        cacheUrls(args[2]);\n      };\n      window.addEventListener(\"online\", cacheCurrentPathname);\n    }\n\n    return () => {\n      history.pushState = pushState;\n      history.replaceState = replaceState;\n      window.removeEventListener(\"online\", cacheCurrentPathname);\n    };\n  }, [serwist?.messageSW, cacheOnNavigation]);\n  useEffect(() => {\n    const reload = () => location.reload();\n    if (reloadOnOnline) {\n      window.addEventListener(\"online\", reload);\n    }\n    return () => {\n      window.removeEventListener(\"online\", reload);\n    };\n  }, [reloadOnOnline]);\n  return <SerwistContext.Provider value={{ serwist }}>{children}</SerwistContext.Provider>;\n}\n\nexport { useSerwist };\n"],"mappings":";;;;;;;AAOA,MAAaE,iBAAiBF,cAAoC,IAAK;AAEvE,MAAaG,mBAAa;CACxB,MAAAC,UAAgBH,WAAWC,cAAc;CACzC,IAAI,CAACE,SACH,MAAM,IAAIC,MAAM,kDAAkD;CACnE,OACMD;AAAO;;;;;;;ACahB,SAAOe,gBAAAC,IAAA;CAAA,MAAAC,IAAAf,EAAA,EAAA;CAAyB,MAAA,EAAAK,OAAAC,SAAAU,IAAAT,UAAAU,IAAAT,mBAAAU,IAAAT,gBAAAU,IAAAT,SAAAC,aAAAG;CAE9B,MAAAR,UAAAU,OAAAI,KAAAA,IAAA,QAAA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