chore(sw): only proxies HTML requests (#9070)

* chore(sw): only proxies HTML requests

もはやHTMLじゃなさそうなリクエストにはSWで関与しないようにする

こうするといろいろな面倒事が解決するはず…たぶん

Resolve #9037
Resolve #9038

* align code style

* Update packages/sw/src/sw.ts

Co-authored-by: Acid Chicken (硫酸鶏) <root@acid-chicken.com>

Co-authored-by: syuilo <Syuilotan@yahoo.co.jp>
Co-authored-by: tamaina <tamaina@hotmail.co.jp>
Co-authored-by: Acid Chicken (硫酸鶏) <root@acid-chicken.com>
This commit is contained in:
rinsuki 2022-09-10 19:25:32 +09:00 committed by GitHub
parent fe048ab2e2
commit 16537d8c92

View File

@ -24,6 +24,16 @@ self.addEventListener('activate', ev => {
}); });
self.addEventListener('fetch', ev => { self.addEventListener('fetch', ev => {
let isHTMLRequest = false;
if (ev.request.headers.get('sec-fetch-dest') === 'document') {
isHTMLRequest = true;
} else if (ev.request.headers.get('accept')?.includes('/html')) {
isHTMLRequest = true;
} else if (ev.request.url.endsWith('/')) {
isHTMLRequest = true;
}
if (!isHTMLRequest) return;
ev.respondWith( ev.respondWith(
fetch(ev.request) fetch(ev.request)
.catch(() => new Response(`Offline. Service Worker @${_VERSION_}`, { status: 200 })) .catch(() => new Response(`Offline. Service Worker @${_VERSION_}`, { status: 200 }))