2022-06-05 21:11:21 +00:00
|
|
|
import { useEffect } from "react";
|
2022-06-12 21:32:57 +00:00
|
|
|
|
|
|
|
import useRevalidate from "./use-revalidate";
|
2022-06-05 21:11:21 +00:00
|
|
|
|
|
|
|
export default function useServiceWorkerRevalidate() {
|
2022-06-12 21:32:57 +00:00
|
|
|
const revalidate = useRevalidate();
|
2022-06-05 21:11:21 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-06-12 21:32:57 +00:00
|
|
|
const channel = new BroadcastChannel("revalidate");
|
2022-06-05 21:11:21 +00:00
|
|
|
function onMessage(event: MessageEvent) {
|
|
|
|
const isRefresh = event.data === "revalidateLoaderData";
|
|
|
|
if (isRefresh) {
|
|
|
|
console.debug("Revalidating loaders data");
|
2022-06-12 21:32:57 +00:00
|
|
|
revalidate();
|
2022-06-05 21:11:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
channel.addEventListener("message", onMessage);
|
|
|
|
return () => {
|
|
|
|
channel.removeEventListener("message", onMessage);
|
|
|
|
channel.close();
|
|
|
|
};
|
2022-06-12 21:32:57 +00:00
|
|
|
}, [revalidate]);
|
2022-06-05 21:11:21 +00:00
|
|
|
}
|