{"version":3,"file":"sw-entry.mjs","names":["Serwist","isCurrentPageOutOfScope","serwist","self","__SERWIST_SW_ENTRY","window","navigator","caches","scope","swEntryWorker","Worker","location","origin","sw","register","cacheOnNavigation","url","onLine","postMessage","type","pushState","history","args","apply","replaceState","addEventListener","pathname","reloadOnOnline","reload"],"sources":["../src/sw-entry.ts"],"sourcesContent":["import { Serwist } from \"@serwist/window\";\nimport { isCurrentPageOutOfScope } from \"@serwist/window/internal\";\n\nimport type { SerwistNextOptions } from \"./internal-types.js\";\nimport type { MessageType } from \"./sw-entry-worker.js\";\n\ndeclare global {\n  interface Window {\n    serwist: Serwist;\n  }\n}\ndeclare const self: Window &\n  typeof globalThis & {\n    // Do not dereference this, use its attributes directly or assign them to other variables.\n    // You should do the latter if you use an attribute multiple times.\n    __SERWIST_SW_ENTRY: SerwistNextOptions;\n  };\n\nif (typeof window !== \"undefined\" && \"serviceWorker\" in navigator && typeof caches !== \"undefined\") {\n  const scope = self.__SERWIST_SW_ENTRY.scope;\n\n  let swEntryWorker: Worker | undefined;\n\n  if (self.__SERWIST_SW_ENTRY.swEntryWorker) {\n    swEntryWorker = new Worker(self.__SERWIST_SW_ENTRY.swEntryWorker);\n  }\n\n  window.serwist = new Serwist(window.location.origin + self.__SERWIST_SW_ENTRY.sw, { scope });\n\n  if (self.__SERWIST_SW_ENTRY.register && !isCurrentPageOutOfScope(scope)) {\n    window.serwist.register();\n  }\n\n  if (self.__SERWIST_SW_ENTRY.cacheOnNavigation) {\n    const cacheOnNavigation = async (url?: string | URL | null | undefined) => {\n      if (!window.navigator.onLine || !url) {\n        return;\n      }\n      swEntryWorker?.postMessage({\n        type: \"__FRONTEND_NAV_CACHE__\",\n        url,\n      } satisfies MessageType);\n    };\n\n    const pushState = history.pushState;\n    history.pushState = (...args) => {\n      pushState.apply(history, args);\n      cacheOnNavigation(args[2]);\n    };\n\n    const replaceState = history.replaceState;\n    history.replaceState = (...args) => {\n      replaceState.apply(history, args);\n      cacheOnNavigation(args[2]);\n    };\n\n    window.addEventListener(\"online\", () => {\n      cacheOnNavigation(window.location.pathname);\n    });\n  }\n\n  if (self.__SERWIST_SW_ENTRY.reloadOnOnline) {\n    window.addEventListener(\"online\", () => location.reload());\n  }\n}\n"],"mappings":";;;AAkBA,IAAI,OAAOK,WAAW,eAAe,mBAAmBC,aAAa,OAAOC,WAAW,aAAa;CAClG,MAAMC,QAAQL,KAAKC,mBAAmBI;CAEtC,IAAIC;CAEJ,IAAIN,KAAKC,mBAAmBK,eAC1BA,gBAAgB,IAAIC,OAAOP,KAAKC,mBAAmBK,aAAa;CAGlEJ,OAAOH,UAAU,IAAIF,QAAQK,OAAOM,SAASC,SAAST,KAAKC,mBAAmBS,IAAI,EAAEL,MAAM,CAAC;CAE3F,IAAIL,KAAKC,mBAAmBU,YAAY,CAACb,wBAAwBO,KAAK,GACpEH,OAAOH,QAAQY,SAAS;CAG1B,IAAIX,KAAKC,mBAAmBW,mBAAmB;EAC7C,MAAMA,oBAAoB,OAAOC,QAA0C;GACzE,IAAI,CAACX,OAAOC,UAAUW,UAAU,CAACD,KAC/B;GAEFP,eAAeS,YAAY;IACzBC,MAAM;IACNH;GACF,CAAuB;EACzB;EAEA,MAAMI,YAAYC,QAAQD;EAC1BC,QAAQD,aAAa,GAAGE,SAAS;GAC/BF,UAAUG,MAAMF,SAASC,IAAI;GAC7BP,kBAAkBO,KAAK,EAAE;EAC3B;EAEA,MAAME,eAAeH,QAAQG;EAC7BH,QAAQG,gBAAgB,GAAGF,SAAS;GAClCE,aAAaD,MAAMF,SAASC,IAAI;GAChCP,kBAAkBO,KAAK,EAAE;EAC3B;EAEAjB,OAAOoB,iBAAiB,gBAAgB;GACtCV,kBAAkBV,OAAOM,SAASe,QAAQ;EAC5C,CAAC;CACH;CAEA,IAAIvB,KAAKC,mBAAmBuB,gBAC1BtB,OAAOoB,iBAAiB,gBAAgBd,SAASiB,OAAO,CAAC;AAE7D"}