Skip to content

Commit

Permalink
Первая серия изменений PWA (#1222)
Browse files Browse the repository at this point in the history
* Добавляет манифест в кеш

* Убирает кеширование ServiceWorker'а

* Удаляет обход POST-запросов

* Удаляет обход запросов на внешние ресурсы

* Меняет порядок в стратегии кеширования
  • Loading branch information
igsekor committed Dec 25, 2023
1 parent 1462d76 commit 47d365d
Showing 1 changed file with 6 additions and 31 deletions.
37 changes: 6 additions & 31 deletions src/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,42 +354,11 @@ async function putPagesInCache(cacheKey, pages, loadRelated = true) {

// Стратегия кеширования
async function cacheStrategyImpl({ cacheKey, request, preloadResponsePromise, fallbackUrl }) {
// Игнорирует запросы на другие домены
if (!request.url.startsWith(self.location.origin)) {
return new Response()
}

// Игнорирует запросы browser-sync в режиме отладки
if (request.url.indexOf('browser-sync') > -1) {
return new Response()
}

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

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

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

// Игнорирует кеширование запросов методом POST
if (request.method === 'POST') {
return new Response()
}

// Пробует загрузить ресурс из кеша
const responseFromCache = await caches.match(request)
if (responseFromCache) {
return responseFromCache
}

let requestUrl = request.url

// Обрабатывает URL для кеширование страниц, если адрес заканчивается на 'index.html'
Expand All @@ -406,6 +375,12 @@ async function cacheStrategyImpl({ cacheKey, request, preloadResponsePromise, fa
return preloadResponse
}

// Пробует загрузить ресурс из кеша
const responseFromCache = await caches.match(request)
if (responseFromCache) {
return responseFromCache
}

// Запрашиваемый пользователем ресурс загружается и помещается в кеш
return putResInCache(cacheKey, requestUrl)
} catch (error) {
Expand Down

0 comments on commit 47d365d

Please sign in to comment.