-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
pwa.cache.*
option to precisely control caching
- Loading branch information
Showing
18 changed files
with
272 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,6 @@ package-lock.json | |
.idea | ||
|
||
# Misc | ||
*.map | ||
sw.min.js | ||
assets/js/dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* PWA loader */ | ||
|
||
if ('serviceWorker' in navigator) { | ||
const meta = document.querySelector('meta[name="pwa-cache"]'); | ||
const isEnabled = meta.content === 'true'; | ||
|
||
if (isEnabled) { | ||
let swUrl = '/sw.min.js'; | ||
const baseUrl = meta.getAttribute('data-baseurl'); | ||
|
||
if (baseUrl !== null) { | ||
swUrl = `${baseUrl}${swUrl}?baseurl=${encodeURIComponent(baseUrl)}`; | ||
} | ||
|
||
const $notification = $('#notification'); | ||
const $btnRefresh = $('#notification .toast-body>button'); | ||
|
||
navigator.serviceWorker.register(swUrl).then((registration) => { | ||
// In case the user ignores the notification | ||
if (registration.waiting) { | ||
$notification.toast('show'); | ||
} | ||
|
||
registration.addEventListener('updatefound', () => { | ||
registration.installing.addEventListener('statechange', () => { | ||
if (registration.waiting) { | ||
if (navigator.serviceWorker.controller) { | ||
$notification.toast('show'); | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
$btnRefresh.on('click', () => { | ||
if (registration.waiting) { | ||
registration.waiting.postMessage('SKIP_WAITING'); | ||
} | ||
$notification.toast('hide'); | ||
}); | ||
}); | ||
|
||
let refreshing = false; | ||
|
||
// Detect controller change and refresh all the opened tabs | ||
navigator.serviceWorker.addEventListener('controllerchange', () => { | ||
if (!refreshing) { | ||
window.location.reload(); | ||
refreshing = true; | ||
} | ||
}); | ||
} else { | ||
navigator.serviceWorker.getRegistrations().then(function (registrations) { | ||
for (let registration of registrations) { | ||
registration.unregister(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* PWA service worker */ | ||
|
||
const swconfPath = '/assets/js/data/swconf.js'; | ||
const params = new URL(location).searchParams; | ||
const swconfUrl = params.has('baseurl') | ||
? `${params.get('baseurl')}${swconfPath}` | ||
: swconfPath; | ||
|
||
importScripts(swconfUrl); | ||
const purge = swconf.purge; | ||
|
||
function verifyHost(url) { | ||
for (const host of swconf.allowHosts) { | ||
const regex = RegExp(`^http(s)?://${host}/`); | ||
if (regex.test(url)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
function verifyUrl(url) { | ||
if (!verifyHost(url)) { | ||
return false; | ||
} | ||
|
||
const requestPath = new URL(url).pathname; | ||
|
||
for (const path of swconf.denyPaths) { | ||
if (requestPath.startsWith(path)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
if (!purge) { | ||
swconf.allowHosts.push(location.host); | ||
} | ||
|
||
self.addEventListener('install', (event) => { | ||
if (purge) { | ||
return; | ||
} | ||
|
||
event.waitUntil( | ||
caches.open(swconf.cacheName).then((cache) => { | ||
return cache.addAll(swconf.resources); | ||
}) | ||
); | ||
}); | ||
|
||
self.addEventListener('activate', (event) => { | ||
event.waitUntil( | ||
caches.keys().then((keyList) => { | ||
return Promise.all( | ||
keyList.map((key) => { | ||
if (purge) { | ||
return caches.delete(key); | ||
} else { | ||
if (key !== swconf.cacheName) { | ||
return caches.delete(key); | ||
} | ||
} | ||
}) | ||
); | ||
}) | ||
); | ||
}); | ||
|
||
self.addEventListener('message', (event) => { | ||
if (event.data === 'SKIP_WAITING') { | ||
self.skipWaiting(); | ||
} | ||
}); | ||
|
||
self.addEventListener('fetch', (event) => { | ||
event.respondWith( | ||
caches.match(event.request).then((response) => { | ||
if (response) { | ||
return response; | ||
} | ||
|
||
return fetch(event.request).then((response) => { | ||
const url = event.request.url; | ||
|
||
if (purge || event.request.method !== 'GET' || !verifyUrl(url)) { | ||
return response; | ||
} | ||
|
||
// See : <https://developers.google.com/web/fundamentals/primers/service-workers#cache_and_return_requests> | ||
let responseToCache = response.clone(); | ||
|
||
caches.open(swconf.cacheName).then((cache) => { | ||
cache.put(event.request, responseToCache); | ||
}); | ||
return response; | ||
}); | ||
}) | ||
); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
--- | ||
layout: compress | ||
permalink: '/:path/swconf.js' | ||
# Note that this file will be fetched by the ServiceWorker, so it will not be cached. | ||
--- | ||
|
||
const swconf = { | ||
{% if site.pwa.cache.enabled %} | ||
cacheName: 'chirpy-{{ "now" | date: "%s" }}', | ||
|
||
{%- comment -%} Resources added to the cache during PWA installation. {%- endcomment -%} | ||
resources: [ | ||
'{{ "/assets/css/:THEME.css" | replace: ':THEME', site.theme | relative_url }}', | ||
'{{ "/" | relative_url }}', | ||
{% for tab in site.tabs %} | ||
'{{- tab.url | relative_url -}}', | ||
{% endfor %} | ||
|
||
{% assign cache_list = site.static_files | where: 'swcache', true %} | ||
{% for file in cache_list %} | ||
'{{ file.path | relative_url }}'{%- unless forloop.last -%},{%- endunless -%} | ||
{% endfor %} | ||
], | ||
|
||
{%- comment -%} The request url with below domain will be cached. {%- endcomment -%} | ||
allowHosts: [ | ||
{% if site.img_cdn and site.img_cdn contains '//' %} | ||
'{{ site.img_cdn | split: '//' | last | split: '/' | first }}', | ||
{% endif %} | ||
|
||
{%- unless site.assets.self_host.enabled -%} | ||
{% for cdn in site.data.origin["cors"].cdns %} | ||
'{{ cdn.url | split: "//" | last }}' | ||
{%- unless forloop.last -%},{%- endunless -%} | ||
{% endfor %} | ||
{% endunless %} | ||
], | ||
|
||
{%- comment -%} The request url with below path will not be cached. {%- endcomment -%} | ||
denyPaths: [ | ||
{% for path in site.pwa.cache.deny_paths %} | ||
{% unless path == empty %} | ||
'{{ path | relative_url }}'{%- unless forloop.last -%},{%- endunless -%} | ||
{% endunless %} | ||
{% endfor %} | ||
], | ||
purge: false | ||
{% else %} | ||
purge: true | ||
{% endif %} | ||
}; |
Oops, something went wrong.