Skip to content

Commit

Permalink
chore(sw): only proxies HTML requests (#9070)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
4 people committed Sep 10, 2022
1 parent d4b146e commit e60a6cc
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/sw/src/sw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }))
Expand Down

0 comments on commit e60a6cc

Please sign in to comment.