Skip to content

Commit 68e4f47

Browse files
committed
use if-modified-since and last-modified headers
1 parent ac9caed commit 68e4f47

4 files changed

Lines changed: 15 additions & 1 deletion

File tree

.changeset/strange-lamps-jump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ryanatkn/orc': patch
3+
---
4+
5+
use `if-modified-since` and `last-modified` headers

src/lib/fetch_cache.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface Fetch_Cache_Item<TData = any, TParams = any> {
2929
params: TParams;
3030
key: Fetch_Cache_Key;
3131
etag: string | null;
32+
last_modified: string | null;
3233
data: TData;
3334
}
3435

src/lib/fetch_packages.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,12 @@ const fetch_package_json = async (
8888
if (etag) {
8989
headers['if-none-match'] = etag;
9090
}
91+
const last_modified = cached?.last_modified;
92+
if (last_modified) {
93+
headers['if-modified-since'] = last_modified;
94+
}
9195
try {
92-
const res = await fetch(url, {headers});
96+
const res = await fetch(url, {headers}); // TODO handle `retry-after` @see https://docs.github.com/en/rest/guides/best-practices-for-using-the-rest-api
9397
if (res.status === 304) {
9498
log?.info('cached', key);
9599
return cached!;
@@ -103,6 +107,7 @@ const fetch_package_json = async (
103107
params: null,
104108
key,
105109
etag: res.headers.get('etag'),
110+
last_modified: res.headers.get('last-modified'),
106111
data: package_json,
107112
};
108113
cache?.set(result.key, result);
@@ -113,6 +118,7 @@ const fetch_package_json = async (
113118
params: null,
114119
key,
115120
etag: null,
121+
last_modified: null,
116122
data: null,
117123
}; // TODO better error
118124
return result;

src/lib/github.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export const fetch_github_pull_requests = async (
6565
params,
6666
key,
6767
etag: res.headers.etag ?? null,
68+
last_modified: res.headers['last-modified'] ?? null,
6869
data: res.data.map((i) => Github_Pull_Request.parse(i)).sort((a, b) => b.number - a.number),
6970
};
7071
cache?.set(result.key, result);
@@ -80,6 +81,7 @@ export const fetch_github_pull_requests = async (
8081
params,
8182
key,
8283
etag: null,
84+
last_modified: null,
8385
data: null,
8486
};
8587
return result;

0 commit comments

Comments
 (0)