-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Expand file tree
/
Copy pathworker.js
More file actions
31 lines (31 loc) · 1.25 KB
/
Copy pathworker.js
File metadata and controls
31 lines (31 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
export default {
async fetch(request, env) {
const url = new URL(request.url);
if (url.pathname.endsWith('/search/search_index.json')) {
const headers = new Headers(request.headers);
headers.delete('Accept-Encoding');
const asset = await env.ASSETS.fetch(
new Request(new URL(`${url.pathname}.gz`, url.origin), {
method: request.method,
headers,
}),
);
if (asset.status === 304 || asset.ok) {
const out = new Headers(asset.headers);
out.set('Content-Type', 'application/json; charset=utf-8');
out.set('Cache-Control', 'public, max-age=3600');
out.delete('Content-Encoding');
out.delete('Content-Length');
const body =
asset.status === 304 || request.method === 'HEAD' || !asset.body
? null
: asset.body.pipeThrough(new DecompressionStream('gzip'));
return new Response(body, {
status: asset.status,
headers: out,
});
}
}
return env.ASSETS.fetch(request);
},
};