From e60a6cc621faafa1f59bd1407d92a955d5ae5bae Mon Sep 17 00:00:00 2001 From: rinsuki <428rinsuki+git@gmail.com> Date: Sat, 10 Sep 2022 19:25:32 +0900 Subject: [PATCH] chore(sw): only proxies HTML requests (#9070) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 (硫酸鶏) Co-authored-by: syuilo Co-authored-by: tamaina Co-authored-by: Acid Chicken (硫酸鶏) --- packages/sw/src/sw.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/sw/src/sw.ts b/packages/sw/src/sw.ts index 872692f90..92391f1fb 100644 --- a/packages/sw/src/sw.ts +++ b/packages/sw/src/sw.ts @@ -24,6 +24,16 @@ self.addEventListener('activate', 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( fetch(ev.request) .catch(() => new Response(`Offline. Service Worker @${_VERSION_}`, { status: 200 }))