Skip to content

Instantly share code, notes, and snippets.

@mordka
Forked from deanhume/service-worker-ghost-cms.js
Last active July 1, 2022 11:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mordka/c261fc28298f89e59c374281513878be to your computer and use it in GitHub Desktop.
Save mordka/c261fc28298f89e59c374281513878be to your computer and use it in GitHub Desktop.
Service Worker for Ghost CMS - ignore Ghost admin
const cacheName = 'blogCache';
const offlineUrl = '/offline/';
const adminPageSlug = '/ghost';
/**
* The event listener for the service worker installation
*/
self.addEventListener('install', event => {
event.waitUntil(
caches.open(cacheName)
.then(cache => cache.addAll([
offlineUrl
]))
);
});
/**
* Is the current request for an HTML page?
* @param {Object} event
*/
function isHtmlPage(event) {
return event.request.method === 'GET' && event.request.headers.get('accept').includes('text/html');
}
/**
* Is the current request for the admin portal resource
* @param {Object} event
*/
function isAdminPageResource(event) {
return event.request.url.includes(adminPageSlug);
}
/**
* Fetch and cache any results as we receive them.
*/
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request)
.then(response => {
// Only return cache if it's not an HTML page or admin resources
if (response && !isHtmlPage(event) && !isAdminPageResource(event)) {
return response;
}
return fetch(event.request).then(
function (response) {
// Dont cache if not a 200 response
if (!response || response.status !== 200) {
return response;
}
let responseToCache = response.clone();
caches.open(cacheName)
.then(function (cache) {
cache.put(event.request, responseToCache);
});
return response;
}
).catch(error => {
// Check if the user is offline first and is trying to navigate to a web page
if (isHtmlPage(event)
) {
return caches.match(offlineUrl);
}
});
})
)
;
});
@mordka
Copy link
Author

mordka commented Apr 15, 2018

This service worker caches all blog resources excluding ghost admin portal. Otherwise you might experience cached articles in editor and "Saving failed! Someone else is editing this post." messages.

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