2022-06-04 21:34:33 +00:00
|
|
|
import { json } from "@remix-run/server-runtime";
|
2022-06-04 13:48:37 +00:00
|
|
|
|
2022-06-11 13:13:28 +00:00
|
|
|
declare const ASSET_CACHE: string;
|
|
|
|
declare const DATA_CACHE: string;
|
|
|
|
declare const DOCUMENT_CACHE: string;
|
2022-06-04 13:48:37 +00:00
|
|
|
|
2022-06-04 21:34:33 +00:00
|
|
|
export function isAssetRequest(request: Request) {
|
|
|
|
return ["font", "image", "script", "style"].includes(request.destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isLoaderRequest(request: Request) {
|
|
|
|
const url = new URL(request.url);
|
|
|
|
return request.method.toLowerCase() === "get" && url.searchParams.get("_data");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isDocumentGetRequest(request: Request) {
|
|
|
|
return request.method.toLowerCase() === "get" && request.mode === "navigate";
|
|
|
|
}
|
|
|
|
|
2022-06-11 14:13:00 +00:00
|
|
|
export function isMutationRequest(request: Request) {
|
|
|
|
return ["POST", "DELETE"].includes(request.method);
|
|
|
|
}
|
|
|
|
|
2022-06-11 13:13:28 +00:00
|
|
|
export function fetchAsset(event: FetchEvent): Promise<Response> {
|
2022-06-04 21:34:33 +00:00
|
|
|
// stale-while-revalidate
|
2022-06-04 13:48:37 +00:00
|
|
|
const url = new URL(event.request.url);
|
2022-06-04 21:34:33 +00:00
|
|
|
return caches
|
|
|
|
.match(event.request, {
|
|
|
|
cacheName: ASSET_CACHE,
|
|
|
|
ignoreVary: true,
|
|
|
|
ignoreSearch: true,
|
|
|
|
})
|
|
|
|
.then((cachedResponse) => {
|
2022-06-05 22:34:50 +00:00
|
|
|
console.debug(`Serving asset from ${cachedResponse ? "cache" : "network"}`, url.pathname);
|
2022-06-04 21:34:33 +00:00
|
|
|
|
|
|
|
const fetchPromise = event.preloadResponse
|
|
|
|
.then((preloadedResponse?: Response) => preloadedResponse || fetch(event.request.clone()))
|
|
|
|
.then((response) =>
|
|
|
|
caches.open(ASSET_CACHE).then((cache) => {
|
|
|
|
switch (response.status) {
|
|
|
|
case 200:
|
|
|
|
cache.put(event.request, response.clone());
|
|
|
|
break;
|
|
|
|
case 404:
|
|
|
|
cache.delete(event.request);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}),
|
|
|
|
);
|
2022-06-04 13:48:37 +00:00
|
|
|
|
2022-06-04 21:34:33 +00:00
|
|
|
return cachedResponse || fetchPromise;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-05 21:11:21 +00:00
|
|
|
// stores the timestamp for when each URL's cached response has been revalidated
|
|
|
|
const lastTimeRevalidated: Record<string, number> = {};
|
|
|
|
|
2022-06-11 13:13:28 +00:00
|
|
|
export function fetchLoaderData(event: FetchEvent): Promise<Response> {
|
2022-06-05 21:11:21 +00:00
|
|
|
const url = new URL(event.request.url);
|
2022-06-19 15:57:51 +00:00
|
|
|
const doNotCacheList = ["/outgoing-call/twilio-token"];
|
|
|
|
if (doNotCacheList.includes(url.pathname)) {
|
2022-06-11 17:29:58 +00:00
|
|
|
return fetch(event.request);
|
|
|
|
}
|
|
|
|
|
2022-06-05 21:11:21 +00:00
|
|
|
const path = url.pathname + url.search;
|
|
|
|
|
|
|
|
return caches.match(event.request, { cacheName: DATA_CACHE }).then((cachedResponse) => {
|
|
|
|
console.debug(`Serving data from ${cachedResponse ? "cache" : "network"}`, path);
|
|
|
|
|
|
|
|
const timestamp = lastTimeRevalidated[path] ?? 0;
|
|
|
|
const diff = Date.now() - timestamp;
|
|
|
|
const TEN_SECONDS = 10 * 1000;
|
|
|
|
if (cachedResponse && diff < TEN_SECONDS) {
|
|
|
|
console.debug("Returned response from cache after a revalidation no older than 10s");
|
|
|
|
// TODO: see if we can check a header or something to see if the requests comes from the revalidation thing
|
|
|
|
return cachedResponse;
|
|
|
|
}
|
|
|
|
|
2022-06-08 22:33:38 +00:00
|
|
|
const request = cachedResponse
|
|
|
|
? new Request(event.request.clone(), { cache: "reload" })
|
|
|
|
: event.request.clone();
|
2022-06-05 21:11:21 +00:00
|
|
|
const fetchPromise = event.preloadResponse
|
2022-06-08 22:33:38 +00:00
|
|
|
.then((preloadedResponse?: Response) => preloadedResponse || fetch(request))
|
2022-06-05 21:11:21 +00:00
|
|
|
.then((response) =>
|
|
|
|
caches.open(DATA_CACHE).then((cache) => {
|
|
|
|
if (!response.ok) {
|
|
|
|
return json(
|
|
|
|
{ message: "Network Error" },
|
|
|
|
{
|
|
|
|
status: 500,
|
|
|
|
headers: { "X-Remix-Catch": "yes", "X-Remix-Worker": "yes" },
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const clonedResponse = response.clone();
|
|
|
|
cache.match(event.request).then(async (cached) => {
|
|
|
|
if (!cached) {
|
|
|
|
// we had nothing cached, simply cache what we got
|
|
|
|
await cache.put(event.request, clonedResponse.clone());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-08 22:33:38 +00:00
|
|
|
// otherwise, cache the new response
|
|
|
|
await cache.put(event.request, clonedResponse.clone());
|
|
|
|
|
2022-06-05 21:11:21 +00:00
|
|
|
if (await areResponsesEqual(cached.clone(), clonedResponse.clone())) {
|
|
|
|
// if what we have in the cache is up-to-date, we don't have to do anything
|
|
|
|
console.debug("Responses are the same, no need to revalidate", path);
|
2022-06-08 22:33:38 +00:00
|
|
|
lastTimeRevalidated[path] = Date.now();
|
2022-06-05 21:11:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cachedResponse) {
|
|
|
|
// and if we had returned a cached response
|
|
|
|
// tell the UI to fetch the latest data
|
|
|
|
console.debug("Revalidate loader data", path);
|
2022-06-12 21:32:57 +00:00
|
|
|
const channel = new BroadcastChannel("revalidate");
|
2022-06-05 21:11:21 +00:00
|
|
|
channel.postMessage("revalidateLoaderData");
|
|
|
|
lastTimeRevalidated[path] = Date.now();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
return cachedResponse || fetchPromise;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function areResponsesEqual(a: Response, b: Response): Promise<boolean> {
|
|
|
|
const viewA = new DataView(await a.arrayBuffer());
|
|
|
|
const viewB = new DataView(await b.arrayBuffer());
|
|
|
|
|
|
|
|
if (viewA === viewB) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (viewA.byteLength !== viewB.byteLength) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let i = viewA.byteLength;
|
|
|
|
while (i--) {
|
|
|
|
if (viewA.getUint8(i) !== viewB.getUint8(i)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2022-06-04 21:34:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-11 13:13:28 +00:00
|
|
|
export function fetchDocument(event: FetchEvent): Promise<Response> {
|
2022-06-04 21:34:33 +00:00
|
|
|
// network-first
|
|
|
|
const url = new URL(event.request.url);
|
|
|
|
console.debug("Serving document from network", url.pathname);
|
|
|
|
return caches.open(DOCUMENT_CACHE).then((cache) =>
|
|
|
|
fetch(event.request.clone())
|
|
|
|
.then((response) => {
|
|
|
|
cache.put(event.request, response.clone());
|
|
|
|
return response;
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.debug("Serving document from network failed, falling back to cache", url.pathname);
|
|
|
|
return caches.match(event.request).then((response) => {
|
|
|
|
if (!response) {
|
|
|
|
throw error;
|
|
|
|
}
|
2022-06-04 13:48:37 +00:00
|
|
|
|
2022-06-04 21:34:33 +00:00
|
|
|
return response;
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
);
|
2022-06-04 13:48:37 +00:00
|
|
|
}
|
2022-06-05 21:11:21 +00:00
|
|
|
|
|
|
|
export async function deleteCaches() {
|
|
|
|
const allCaches = await caches.keys();
|
2022-06-11 13:13:28 +00:00
|
|
|
const cachesToDelete = allCaches.filter((cacheName) => cacheName !== ASSET_CACHE);
|
|
|
|
await Promise.all(cachesToDelete.map((cacheName) => caches.delete(cacheName)));
|
2022-06-11 14:13:00 +00:00
|
|
|
console.debug("Old caches deleted");
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function purgeMutatedLoaders(event: FetchEvent) {
|
|
|
|
const url = new URL(event.request.url);
|
|
|
|
const rootPathname = "/" + url.pathname.split("/")[1];
|
|
|
|
const cache = await caches.open(DATA_CACHE);
|
|
|
|
const cachedLoaders = await cache.keys();
|
|
|
|
|
|
|
|
const loadersToDelete = cachedLoaders.filter((loader) => {
|
|
|
|
const cachedPathname = new URL(loader.url).pathname;
|
|
|
|
const shouldPurge = cachedPathname.startsWith(rootPathname);
|
|
|
|
|
2022-06-12 21:32:57 +00:00
|
|
|
if (["/dev/null", "/settings/phone"].includes(url.pathname)) {
|
2022-06-11 14:13:00 +00:00
|
|
|
// changes phone number or twilio account credentials
|
|
|
|
// so purge messages and phone calls from cache
|
|
|
|
return (
|
|
|
|
shouldPurge ||
|
|
|
|
["/messages", "/calls", "/keypad"].some((pathname) => cachedPathname.startsWith(pathname))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return shouldPurge;
|
|
|
|
});
|
|
|
|
await Promise.all(loadersToDelete.map((loader) => cache.delete(loader)));
|
|
|
|
console.debug("Purged loaders data starting with", rootPathname);
|
2022-06-05 21:11:21 +00:00
|
|
|
}
|