-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(app): Serve cached video on Safari
Following these recommendations to make cache videos work on Safari https://developers.google.com/web/tools/workbox/guides/advanced-recipes GoogleChrome/workbox#1663 (comment)
- Loading branch information
1 parent
a203894
commit 92702ed
Showing
2 changed files
with
31 additions
and
3 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
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 |
---|---|---|
@@ -1,11 +1,38 @@ | ||
importScripts('/assets/js/workbox-v5.0.0/workbox-sw.js') | ||
workbox.setConfig({ modulePathPrefix: '/assets/js/workbox-v5.0.0/' }) | ||
|
||
workbox.core.setCacheNameDetails({ prefix: 'assets' }) | ||
workbox.core.setCacheNameDetails({ prefix: '', suffix: '', precache: 'precache' }) | ||
workbox.core.skipWaiting() | ||
workbox.core.clientsClaim() | ||
|
||
workbox.precaching.precacheAndRoute(self.__WB_MANIFEST) | ||
const precache = self.__WB_MANIFEST | ||
precacheMedia(precache) | ||
workbox.precaching.precacheAndRoute(precache) | ||
|
||
workbox.precaching.cleanupOutdatedCaches() | ||
workbox.routing.registerRoute(/\.(?:png|jpg|jpeg|svg|mp4)$/, new workbox.strategies.StaleWhileRevalidate({ cacheName: 'media', plugins: [new workbox.expiration.ExpirationPlugin({ maxEntries: 30, purgeOnQuotaError: false })] }), 'GET') | ||
workbox.routing.registerRoute(/\.(?:png|jpg|jpeg|svg)$/, new workbox.strategies.CacheFirst({ cacheName: 'media', plugins: [new workbox.expiration.ExpirationPlugin({ maxEntries: 30, purgeOnQuotaError: false })] }), 'GET') | ||
workbox.routing.registerRoute(/\/assets\/media\/.*\.mp4$/, new workbox.strategies.CacheFirst({ | ||
cacheName: 'precachedMedia', | ||
plugins: [ | ||
new workbox.expiration.ExpirationPlugin({ maxEntries: 5, purgeOnQuotaError: false }), | ||
new workbox.rangeRequests.RangeRequestsPlugin(), | ||
new workbox.cacheableResponse.CacheableResponsePlugin({ statuses: [200] }) | ||
] | ||
})) | ||
|
||
function precacheMedia (precache) { | ||
const media = precache.filter((p, i, arr) => { | ||
if (/.*\.mp4$/.test(p.url)) { | ||
arr.splice(i, 1) | ||
return true | ||
} | ||
}) | ||
self.addEventListener('install', event => { | ||
event.waitUntil( | ||
caches.open('precachedMedia') | ||
.then(cache => { | ||
media.forEach(e => cache.add(e.url)) | ||
}) | ||
) | ||
}) | ||
} |