У меня есть специальный маршрут /home
, который перенаправляет пользователей разных типов в разные места. Это также start_url
в моем манифесте PWA. Однако, если пользователь находится в автономном режиме (и поэтому не может получить маршрут сервера /home
), вместо отображения автономной страницы, я бы по умолчанию выбрал домашнюю страницу ('/') и перенаправил ее туда или на хотя бы показать, что содержимое (маршрут '/' кэшируется).
Вот упрощенная версия прослушивателя выборки:
var staticRoutes = ['/'];
self.addEventListener('fetch', function(event) {
event.respondWith(
// If the request is for a static route try to fetch it; if can't, return the cached page
staticRoutes.indexOf(new URL(event.request.url).pathname) > -1
? fetch(event.request).catch(function() {
return caches.match(event.request).then(function(response) {
return response;
})
: caches.match(event.request).then(function(response) {
return (
response ||
fetch(event.request).catch(function() {
if (event.request.mode === 'navigate') {
if (new URL(event.request.url).pathname === '/home') {
// What do I do hear to either:
// 1. redirect the user to the '/' route (letting it go through the service worker)
// 2. just display the content of the '/' route which is in the cache
}
}
})
)
})
)
})