Improved caching to exclude server side only resources
This commit is contained in:
@@ -16,6 +16,12 @@ const base = "/";
|
||||
const baseUrl = new URL(base, self.origin);
|
||||
const manifestUrlList = self.assetsManifest.assets.map(asset => new URL(asset.url, baseUrl).href);
|
||||
|
||||
// Add assets which are only available on the server and should not be cached here
|
||||
const serverOnlyResources = [
|
||||
"/api",
|
||||
"/oauth2"
|
||||
];
|
||||
|
||||
async function onInstall(event) {
|
||||
console.info('Service worker: Install');
|
||||
|
||||
@@ -39,12 +45,17 @@ async function onActivate(event) {
|
||||
|
||||
async function onFetch(event) {
|
||||
let cachedResponse = null;
|
||||
|
||||
if (event.request.method === 'GET') {
|
||||
// For all navigation requests, try to serve index.html from cache,
|
||||
// unless that request is for an offline resource.
|
||||
// If you need some URLs to be server-rendered, edit the following check to exclude those URLs
|
||||
const shouldServeIndexHtml = event.request.mode === 'navigate'
|
||||
&& !manifestUrlList.some(url => url === event.request.url);
|
||||
|
||||
const path = new URL(event.request.url).pathname;
|
||||
|
||||
const shouldServeIndexHtml =
|
||||
event.request.mode === 'navigate' &&
|
||||
!manifestUrlList.some(url => url === event.request.url) &&
|
||||
!serverOnlyResources.some(url => path.startsWith(url)); // Check for server side assets
|
||||
|
||||
const request = shouldServeIndexHtml ? 'index.html' : event.request;
|
||||
const cache = await caches.open(cacheName);
|
||||
|
||||
Reference in New Issue
Block a user