Skip to content

Commit d77fbcf

Browse files
committed
fix: adding etag, last-modified info to github releases
1 parent d3f3717 commit d77fbcf

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

site/functions/gh/[username]/[repo]/[version]/[name].css.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import { fetchGithubRelease, type Params } from '@/fetchGithubRelease';
22

33
export async function onRequest(context: any) {
44
// typlog/theme-nezu/0.6.1/base.css
5-
const params = context.params as Params
6-
const content = await fetchGithubRelease(params, 'css')
7-
if (!content) {
5+
const params = context.params as Params;
6+
const result = await fetchGithubRelease(params, 'css');
7+
if (!result) {
88
return new Response('Not Found', { status: 404 });
99
} else {
10-
return new Response(content, { headers: { 'Content-Type': 'text/css' } });
10+
const headers = {
11+
'content-type': 'text/css',
12+
...result.headers,
13+
};
14+
return new Response(result.content, { headers });
1115
}
1216
}

site/functions/gh/[username]/[repo]/[version]/[name].js.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import { fetchGithubRelease, type Params } from '@/fetchGithubRelease';
22

33
export async function onRequest(context: any) {
44
// typlog/theme-nezu/0.6.1/app.js
5-
const params = context.params as Params
6-
const content = await fetchGithubRelease(params, 'js')
7-
if (!content) {
5+
const params = context.params as Params;
6+
const result = await fetchGithubRelease(params, 'js');
7+
if (!result) {
88
return new Response('Not Found', { status: 404 });
99
} else {
10-
return new Response(content, { headers: { 'Content-Type': 'text/javascript' } });
10+
const headers = {
11+
'content-type': 'text/javascript',
12+
...result.headers,
13+
};
14+
return new Response(result.content, { headers });
1115
}
1216
}

site/lib/fetchGithubRelease.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@ export interface Params {
55
name: string
66
}
77

8+
const RESERVED_HEADERS = ['date', 'etag', 'last-modified']
9+
810
export async function fetchGithubRelease(params: Params, suffix: 'css' | 'js') {
911
const url = `https://github.com/${params.username}/${params.repo}/releases/download/${params.version}/${params.name}.${suffix}`
1012
const resp = await fetch(url)
1113
if (resp.status === 200) {
12-
return resp.text()
14+
const content = await resp.text()
15+
const headers: Record<string, string> = {}
16+
RESERVED_HEADERS.forEach(key => {
17+
const value = resp.headers.get(key)
18+
if (value) {
19+
headers[key] = value
20+
}
21+
})
22+
return { content, headers }
1323
} else {
1424
return null
1525
}

0 commit comments

Comments
 (0)