Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/playground/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { defineGlobals } from "./config";
import Monitor from "./Monitor.vue";
import router from "./router";
import { normalizeError } from "./utils/helpers";
import { setupMapCache } from "./utils/mapCache";

setupMapCache();

const app = createApp(Monitor);

app.config.errorHandler = error => {
Expand Down
64 changes: 64 additions & 0 deletions packages/playground/src/utils/mapCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Cache configuration for map-related resources
*/
const CACHE_CONFIG = [
{
pattern: (url: string) => url.endsWith("/all.csv") && url.includes("ISO-3166"),
contentType: "text/csv",
},
{
pattern: (url: string) => url.includes("countries-110m.json") || url.endsWith("/110m.json"),
contentType: "application/json",
},
] as const;

const cache = new Map<string, string>();
const promises = new Map<string, Promise<string>>();
const originalFetch = window.fetch;

async function getCached(url: string): Promise<string> {
if (cache.has(url)) {
return cache.get(url)!;
}

if (promises.has(url)) {
return promises.get(url)!;
}

const promise = originalFetch(url)
.then(res => res.text())
.then(text => {
cache.set(url, text);
return text;
})
.finally(() => {
promises.delete(url);
});

promises.set(url, promise);
return promise;
}

function getUrlString(input: RequestInfo | URL): string {
if (typeof input === "string") return input;
if (input instanceof URL) return input.href;
return input.url;
}

export function setupMapCache(): void {
window.fetch = async function (input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
const url = getUrlString(input);

for (const config of CACHE_CONFIG) {
if (config.pattern(url)) {
const content = await getCached(url);
return new Response(content, {
status: 200,
headers: { "Content-Type": config.contentType },
});
}
}

return originalFetch(input, init);
};
}
Loading