Skip to content

Commit 736c737

Browse files
committed
fix edgecases with cachedFetch
1 parent cf76178 commit 736c737

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/utils/cacheUtils.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ interface LocalStorageCacheResponse {
3838
timestamp: number
3939
}
4040

41-
export async function cachedFetch(input: string ): Promise<Response> {
41+
export async function cachedFetch(input: string): Promise<Response> {
4242
const now = Date.now();
4343
const cacheDuration = 7 * 24 * 60 * 60 * 1000; // 7 days
4444

@@ -51,13 +51,20 @@ export async function cachedFetch(input: string ): Promise<Response> {
5151
// If item exists in localStorage and is still valid, return it
5252
if ((now - timestamp < cacheDuration) && simpleResponse.status) {
5353
// create a new Response object from the cached data
54-
return new Response(simpleResponse.responseText, {
54+
const resp = new Response(simpleResponse.responseText, {
5555
status: simpleResponse.status,
5656
headers: { 'Content-Type': 'application/json' },
5757
});
58+
59+
// Set the URL property on the response object
60+
Object.defineProperty(resp, "url", { value: input });
61+
return resp;
5862
}
5963

6064
const response = await fetch(input);
65+
if (!response.ok) {
66+
throw new Error(`Fetch ${input} failed: ${response.status} ${response.statusText}`);
67+
}
6168
const responseText = await response.clone().text();
6269
localStorage.setItem(`${key}`, JSON.stringify(
6370
{

0 commit comments

Comments
 (0)