Skip to content

Правит ошибки в обработке события fetch в Service Worker#1346

Draft
monochromer wants to merge 3 commits intodoka-guide:mainfrom
monochromer:fix-service-worker
Draft

Правит ошибки в обработке события fetch в Service Worker#1346
monochromer wants to merge 3 commits intodoka-guide:mainfrom
monochromer:fix-service-worker

Conversation

@monochromer
Copy link
Copy Markdown
Contributor

@monochromer monochromer commented Apr 6, 2026

В консоли есть ошибки типа:

Uncaught (in promise) TypeError: s.request.startsWith is not a function
    at sw.js:1:11550
    at Generator.next (<anonymous>)
    at sw.js:1:196
    at new Promise (<anonymous>)
    at o (sw.js:1:16)
    at sw.js:1:11512

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.

src/sw.js Outdated
}

// Игнорирует кеширование 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').

src/sw.js Outdated
}

// Игнорирует кеширование страниц с параметрами 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() !== ''

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 7, 2026

Превью контента из 1acdf59 опубликовано.

@monochromer monochromer marked this pull request as draft April 7, 2026 19:03
src/sw.js Outdated
// Игнорирует кеширование Service Worker
if (event.request.endsWith('sw.js')) {
return new Response(fetch(event.request))
if (url.endsWith('sw.js')) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

А раз уж у нас есть urlObject, то может лучше проверять urlObject.pathname === '/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.

Да, выглядит чище. Сделал так

src/sw.js Outdated
// Игнорирует кеширование страниц с параметрами GET запроса
if (event.request.indexOf('.html?') > -1 || event.request.indexOf('.js?') > -1) {
return new Response(fetch(event.request))
if (url.indexOf('.html?') > -1 || url.indexOf('.js?') > -1) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

И тут, соответственно urlObject.search !== ''

@monochromer
Copy link
Copy Markdown
Contributor Author

Из-за ошибки обработки события fetch Service Worker фактически не перехватывает запросы. Эта ошибка судя по всему наблюдается довольно давно. Попробуйте в main-ветке в devtools на вкладке network включить режим "offline" (или вообще отключиться от сети) как на сайте, так и на установленном PWA. По крайней мере, у меня не работает offline-режим.

После правки появляются дополнительные ошибки. Например, не подгружаются стили.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants