Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,24 +434,31 @@ self.addEventListener('message', async (event) => {
})

self.addEventListener('fetch', async (event) => {
const url = new URL(event.request.url)

// Игнорирует запросы на другие домены
if (!event.request.startsWith(self.location.origin) && event.request.method === 'POST') {
return new Response(fetch(event.request))
if (url.origin !== self.location.origin) {
return

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Чтобы Service Worker не обрабатывал запрос, достаточно досрочно вернуть undefined.

}

// Игнорирует обработку не GET-запросов
if (event.request.method !== 'GET') {
return
}

// Игнорирует кеширование Service Worker
if (event.request.endsWith('sw.js')) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вообще, такая ситуация возможна только если кто-то сделает внутри основного кода fetсh('/sw.js').

return new Response(fetch(event.request))
return
}

// Игнорирует кеширование манифеста
if (event.request.endsWith('manifest.json')) {
return new Response(fetch(event.request))
return
}

// Игнорирует кеширование страниц с параметрами GET запроса
if (event.request.indexOf('.html?') > -1 || event.request.indexOf('.js?') > -1) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут можно было бы заменить такую проверку на что-то типа [...url.searchParams.keys()].legnth > 0 или url.searchParams.toString() !== ''

return new Response(fetch(event.request))
return
}

event.respondWith(
Expand Down
Loading