remix-utils
Version:
This package contains simple utility functions to use with [React Router](https://reactrouter.com/).
73 lines • 2.77 kB
JavaScript
/**
* Caches all JS files built by Remix in a browser cache.
* This will use the Remix manifest to determine which files to cache.
* It will get every JS file, get all the already cached URLs, remove any
* old file, and finally add the new files to the cache.
*
* **This can only be run inside entry.client**
*/
export async function cacheAssets({ cacheName = "assets", buildPath = "/build/", } = {}) {
let paths = getFilePaths();
let cache = await caches.open(cacheName);
let urls = await getCachedUrls(cache, buildPath);
await removeOldAssets(cache, paths, urls);
await addNewAssets(cache, paths, urls);
}
function getFilePaths() {
try {
return unique([
// biome-ignore lint/suspicious/noExplicitAny: global window type is missing proper typing
...Object.values(window.__reactRouterManifest.routes).flatMap(
// biome-ignore lint/suspicious/noExplicitAny: global window type is missing proper typing
(route) => {
return [route.module, ...(route.imports ?? [])];
}),
// biome-ignore lint/suspicious/noExplicitAny: global window type is missing proper typing
window.__reactRouterManifest.url,
// biome-ignore lint/suspicious/noExplicitAny: global window type is missing proper typing
window.__reactRouterManifest.entry.module,
// biome-ignore lint/suspicious/noExplicitAny: global window type is missing proper typing
...window.__reactRouterManifest.entry.imports,
]);
}
catch {
throw new Error("Failed to get file paths from Remix manifest");
}
}
async function getCachedUrls(cache, buildPath = "/build/") {
try {
let keys = await cache.keys();
return keys
.map((key) => {
return new URL(key.url);
})
.filter((url) => url.hostname === window.location.hostname)
.map((url) => url.pathname)
.filter((pathname) => pathname.startsWith(buildPath));
}
catch {
throw new Error("Failed to retrieve cached URLs");
}
}
async function removeOldAssets(cache, paths, urls) {
try {
await Promise.all(urls
.filter((pathname) => !paths.includes(pathname))
.map((pathname) => cache.delete(pathname)));
}
catch {
throw new Error("Failed to remove old assets from the cache");
}
}
async function addNewAssets(cache, paths, urls) {
try {
await cache.addAll(paths.filter((path) => !urls.includes(path)));
}
catch {
throw new Error("Failed to add new assets to the cache");
}
}
function unique(array) {
return [...new Set(array)];
}
//# sourceMappingURL=cache-assets.js.map