forked from Rosetta/landing
35 lines
940 B
JavaScript
35 lines
940 B
JavaScript
function withFreshHtml(response) {
|
|
if (!response.headers.get('content-type')?.includes('text/html')) {
|
|
return response;
|
|
}
|
|
|
|
const headers = new Headers(response.headers);
|
|
headers.set('Cache-Control', 'no-cache');
|
|
|
|
return new Response(response.body, {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
headers,
|
|
});
|
|
}
|
|
|
|
const worker = {
|
|
async fetch(request, env) {
|
|
const response = await env.ASSETS.fetch(request);
|
|
|
|
if (
|
|
response.status !== 404 ||
|
|
request.method !== 'GET' ||
|
|
!request.headers.get('accept')?.includes('text/html')
|
|
) {
|
|
return withFreshHtml(response);
|
|
}
|
|
|
|
const indexUrl = new URL('/index.html', request.url);
|
|
const indexResponse = await env.ASSETS.fetch(new Request(indexUrl, request));
|
|
return withFreshHtml(indexResponse);
|
|
},
|
|
};
|
|
|
|
export default worker;
|