serve loader data from the cache first, update the UI when there is something new

This commit is contained in:
m5r
2022-06-05 23:11:21 +02:00
parent 5943044509
commit 724348cff4
6 changed files with 216 additions and 29 deletions

View File

@ -0,0 +1,23 @@
import { useEffect } from "react";
import { useFetcher } from "@remix-run/react";
export default function useServiceWorkerRevalidate() {
const fetcher = useFetcher();
useEffect(() => {
const channel = new BroadcastChannel("sw-messages");
function onMessage(event: MessageEvent) {
const isRefresh = event.data === "revalidateLoaderData";
if (isRefresh) {
console.debug("Revalidating loaders data");
fetcher.submit({}, { method: "post", action: "/dev/null" });
}
}
channel.addEventListener("message", onMessage);
return () => {
channel.removeEventListener("message", onMessage);
channel.close();
};
}, [fetcher]);
}