devdocs/views/service-worker.js.erb

66 lines
2.2 KiB
Text
Raw Normal View History

2019-07-07 03:25:42 +02:00
<%# Use the hash of the application.js file as cache name or 'app' if not running in production %>
2019-07-07 00:55:58 +02:00
<%# This ensures that the cache is always updated if the hash of the application.js file changes %>
const cacheName = '<%= javascript_path('application', asset_host: false).scan(/application-([^\.]+)\.js/).last&.first || 'app' %>';
<%# Paths to cache when the service worker is installed %>
const cachePaths = [
'/',
'/favicon.ico',
'/manifest.json',
'/images/webapp-icon-32.png',
'/images/webapp-icon-60.png',
'/images/webapp-icon-80.png',
'/images/webapp-icon-128.png',
'/images/webapp-icon-256.png',
'/images/webapp-icon-512.png',
'<%= manifest_asset_urls.join "',\n '" %>',
'<%= doc_index_urls.join "',\n '" %>',<% unless App.production? %>
'<%= javascript_path('debug') %>',<% end %>
2019-07-07 00:55:58 +02:00
];
<%# Set-up the cache %>
self.addEventListener('install', event => {
self.skipWaiting();
event.waitUntil(
caches.open(cacheName).then(cache => cache.addAll(cachePaths)),
);
});
<%# Remove old caches %>
self.addEventListener('activate', event => {
2019-07-07 03:25:42 +02:00
event.waitUntil((async () => {
const keys = await caches.keys();
const jobs = keys.map(key => key !== cacheName ? caches.delete(key) : Promise.resolve());
return Promise.all(jobs);
2019-07-07 03:25:42 +02:00
})());
2019-07-07 00:55:58 +02:00
});
<%# Handle HTTP requests %>
self.addEventListener('fetch', event => {
2019-07-07 03:25:42 +02:00
event.respondWith((async () => {
<% if bypass_cache? %>
return fetch(event.request);
<% else %>
2019-07-07 03:25:42 +02:00
const cachedResponse = await caches.match(event.request);
if (cachedResponse) return cachedResponse;
try {
const response = await fetch(event.request);
return response;
} catch (err) {
const url = new URL(event.request.url);
<%# Attempt to return the index page from the cache if the user is visiting a url like devdocs.io/javascript/global_objects/array/find %>
<%# The index page will make sure the correct documentation or a proper offline page is shown %>
if (url.origin === location.origin && !url.pathname.includes('.')) {
const cachedIndex = await caches.match('/');
if (cachedIndex) return cachedIndex;
2019-07-07 00:55:58 +02:00
}
2019-07-07 03:25:42 +02:00
throw err;
}
<% end %>
2019-07-07 03:25:42 +02:00
})());
2019-07-07 00:55:58 +02:00
});