UNPKG

next-pwa-pack

Version:

PWA cache provider for Next.js/React apps (service worker, manifest, offline page, SPA cache, offline)

1 lines 927 B
import{useEffect,useRef}from"react";import{updateSWCache}from"./pwaActions";export default function SSERevalidateListener({sseEndpoint:e="/api/pwa/cache-events",enabled:o=!0}){const n=useRef(null);return useEffect((()=>{if(!o||"undefined"==typeof window)return void console.log("[PWA-SSE] SSE disabled or not in browser");console.log("[PWA-SSE] Starting SSE listener for:",e);const r=new EventSource(e);return n.current=r,r.onopen=()=>{console.log("[PWA-SSE] Connected to server")},r.onmessage=e=>{try{const o=JSON.parse(e.data);console.log("[PWA-SSE] Received event:",o),"revalidate"===o.type&&o.urls&&Array.isArray(o.urls)&&(console.log("[PWA-SSE] Revalidating URLs:",o.urls),updateSWCache(o.urls))}catch(e){console.warn("[PWA-SSE] Failed to parse message:",e)}},r.onerror=e=>{console.warn("[PWA-SSE] Connection error:",e)},()=>{console.log("[PWA-SSE] Cleaning up SSE connection"),n.current&&n.current.close()}}),[e,o]),null}