From 76a7d0698c414aad68c9151f196c70780f57325b Mon Sep 17 00:00:00 2001 From: Yuanle Song <sylecn@gmail.com> Date: Tue, 7 Feb 2017 21:11:28 +0800 Subject: [PATCH] make service worker work when put in any subdir This allow easier deployment --- sw.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/sw.js b/sw.js index dd9118d..3ab1547 100644 --- a/sw.js +++ b/sw.js @@ -1,3 +1,4 @@ +'use strict'; importScripts('vendor/localforage.min.js'); var CACHE = 'calc'; @@ -21,8 +22,11 @@ const cacheFileExtList = ['.js', '.css', '.png', '.jpg', '.gif']; */ const shouldCache = function (url) { const path = getUrlPath(url); - if (cachePathList.indexOf(path) !== -1) { - return true; + for (let cachePath of cachePathList) { + // since sw.js is scoped at ./, this should work. + if (path.endsWith(cachePath)) { + return true; + } } return cacheFileExtList.some(function (ext, index, ar) { return path.endsWith(ext); @@ -33,7 +37,7 @@ const shouldCache = function (url) { */ const isVendorAsset = function (url) { const path = getUrlPath(url); - return path.startsWith('/vendor/'); + return path.includes('/vendor/'); }; /** * return true if this URL is static asset. see cacheFileExtList. -- GitLab