Skip to content

Commit

Permalink
fix(app): Serve cached video on Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
hecsalazarf committed Feb 5, 2020
1 parent a203894 commit 92702ed
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/components/MainHero.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
muted
playsinline
class="absolute min-h-full min-w-full w-auto h-auto"
crossorigin="anonymous"
>
<source
:src="require('~/assets/video/main_hero.mp4')"
Expand Down
33 changes: 30 additions & 3 deletions static/service-worker.js
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))
})
)
})
}

0 comments on commit 92702ed

Please sign in to comment.