Skip to content

Commit

Permalink
lint and doc fix
Browse files Browse the repository at this point in the history
birm committed Jan 24, 2025
1 parent f4be78d commit 1cf0df3
Showing 1 changed file with 27 additions and 36 deletions.
63 changes: 27 additions & 36 deletions source/hooks/useFetch.js
Original file line number Diff line number Diff line change
@@ -45,16 +45,16 @@ const useFetch = (url, type = 'json') => {
const cachedResponse = await cache.match(url);
const cachedLastModified = await cache.match(`${url}-last-modified`);

// If cached data exists, check if it's up to date using ETag or Last-Modified
// If cached data exists, check if it's up to date using Last-Modified
if (cachedResponse && cachedLastModified) {
const lastModified = cachedLastModified.headers.get('Last-Modified');

// Fetch headers only using the HEAD request
const headResponse = await fetch(url, { ...config, method: 'HEAD' });
const newLastModified = headResponse.headers.get('Last-Modified');

// Compare if the ETag or Last-Modified is different
console.log("cache" ,lastModified, newLastModified)
// Compare if the Last-Modified is different
console.log('cache', lastModified, newLastModified);
if (lastModified === newLastModified) {
const cachedData = await cachedResponse.json();
setData(cachedData);
@@ -63,33 +63,25 @@ const useFetch = (url, type = 'json') => {
return;
}
}
console.log("cache fail", cachedLastModified)
console.log('cache fail', cachedLastModified);

// Fetch fresh data if it's not cached or is outdated
const csvData = await d3.csv(url, covertRaw);
setData(csvData);
setIsPending(false);
setError(null);

// Cache the fresh data along with ETag and Last-Modified headers
// Cache the fresh data along with the Last-Modified header
const responseToCache = new Response(JSON.stringify(csvData));
await cache.put(url, responseToCache);

// Now use HEAD request to get only the headers
const headResponse = await fetch(url, { ...config, method: 'HEAD' });
const etag = headResponse.headers.get('ETag');
const lastModified = headResponse.headers.get('Last-Modified');

// Only cache headers if they exist
if (etag) {
const etagResponse = new Response(null, { headers: { ETag: etag } });
await cache.put(`${url}-etag`, etagResponse);
}
if (lastModified) {
const lastModifiedResponse = new Response(null, { headers: { 'Last-Modified': lastModified } });
await cache.put(`${url}-last-modified`, lastModifiedResponse);
}

} catch (err) {
if (err.name !== 'AbortError') {
setIsPending(false);
@@ -98,30 +90,29 @@ const useFetch = (url, type = 'json') => {
}

return () => abortCont.abort();
} else {

// For non-CSV data (JSON or other types)
fetch(url, config)
.then((x) => x.json())
.then((res) => {
if (!res.error) {
setData(res);
setIsPending(false);
setError(null);
} else {
throw Error(res.error);
}
})
.catch((err) => {
if (err.name !== 'AbortError') {
setIsPending(false);
setError(err);
}
});
}

// For non-CSV data (JSON or other types)
fetch(url, config)
.then((x) => x.json())
.then((res) => {
if (!res.error) {
setData(res);
setIsPending(false);
setError(null);
} else {
throw Error(res.error);
}
})
.catch((err) => {
if (err.name !== 'AbortError') {
setIsPending(false);
setError(err);
}
});

return () => abortCont.abort();
};
}
return () => abortCont.abort();
};

fetchData();

0 comments on commit 1cf0df3

Please sign in to comment.