Skip to content

Commit

Permalink
Bump workbox-window from 4.3.1 to 6.0.2 (#198)
Browse files Browse the repository at this point in the history
* Bumps [workbox-window](https://github.com/googlechrome/workbox) from 4.3.1 to 6.0.2.
  - [Release notes](https://github.com/googlechrome/workbox/releases)
  - [Commits](GoogleChrome/workbox@v4.3.1...v6.0.2)
* Use rollup to build service worker
* Fix offline work
  • Loading branch information
dependabot[bot] authored Jan 10, 2021
1 parent 94ccaaa commit 2e42c9c
Show file tree
Hide file tree
Showing 6 changed files with 397 additions and 76 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ pids/
public/css/main.css*
uploads/
public/*_stats.json
public/sw.js
2 changes: 1 addition & 1 deletion client/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ app.run(/* @ngInject */function ($rootScope) {
// noinspection JSUnnecessarySemicolon
; // eslint-disable-line semi
// include all js files
bulk(__dirname, ['./**/!(app|*.spec).js'])
bulk(__dirname, ['./**/!(app|*.spec|sw).js'])
115 changes: 66 additions & 49 deletions public/sw.js → client/scripts/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,100 @@
/* global self, caches, fetch, importScripts, workbox, Response */
/* eslint-disable indent */

importScripts('https://storage.googleapis.com/workbox-cdn/releases/4.2.0/workbox-sw.js')
import { CacheableResponsePlugin } from 'workbox-cacheable-response'
import { cacheNames, clientsClaim } from 'workbox-core'
import { ExpirationPlugin } from 'workbox-expiration'
import { initialize as googleAnalyticsInitialize } from 'workbox-google-analytics'
import {
cleanupOutdatedCaches,
createHandlerBoundToURL,
matchPrecache,
precache
} from 'workbox-precaching'
import { NavigationRoute, registerRoute, setCatchHandler } from 'workbox-routing'
import { CacheFirst, NetworkFirst, NetworkOnly, StaleWhileRevalidate } from 'workbox-strategies'

// network timeout for network first strategies - if not response in this time, serve from cache
var NETWORK_TIMEOUT_SECONDS = 0.5
const NETWORK_TIMEOUT_SECONDS = 0.5
// default html document to serve
var SINGLE_PAGE_URL = '/index.html'
const SINGLE_PAGE_URL = '/index.html'

workbox.precaching.precache([
precache([
SINGLE_PAGE_URL,
'/js/scripts.js',
'/css/main.css'
], {
ignoreURLParametersMatching: true
})

workbox.precaching.cleanupOutdatedCaches()
cleanupOutdatedCaches()

workbox.routing.registerNavigationRoute(
// Assuming '/' has been precached,
// look up its corresponding cache key.
workbox.precaching.getCacheKeyForURL(SINGLE_PAGE_URL), {
blacklist: [
new RegExp('/api/')
]
const handler = createHandlerBoundToURL(SINGLE_PAGE_URL)
const navigationRoute = new NavigationRoute(handler, {
denylist: [/\/api\//]
})
registerRoute(navigationRoute)

// Catch routing errors, like if the user is offline
setCatchHandler(async ({ event }) => {
// Return the precached offline page if a document is being requested
if (event.request.destination === 'document') {
return matchPrecache(SINGLE_PAGE_URL)
}
)

// the primary resources that are precashed
workbox.routing.registerRoute(
return Response.error()
})

// the primary resources that are precached
registerRoute(
/\/(js\/scripts\.js|css\/main\.css)$/,
new workbox.strategies.NetworkFirst({
cacheName: workbox.core.cacheNames.precache,
new NetworkFirst({
cacheName: cacheNames.precache,
networkTimeoutSeconds: NETWORK_TIMEOUT_SECONDS
})
)

// nomenclature data
workbox.routing.registerRoute(
/^.*\/api\/(i18n|locations|nomenclature|species|user|zone|visit)/,
new workbox.strategies.StaleWhileRevalidate({
registerRoute(
/^.*\/api\/(staging\/)?(i18n|locations|nomenclature|organization|species|user|zone|visit)/,
new StaleWhileRevalidate({
cacheName: 'api-cache'
})
)

// every other api request
workbox.routing.registerRoute(
registerRoute(
/^.*\/api\//,
new workbox.strategies.NetworkOnly()
new NetworkOnly()
)

workbox.routing.registerRoute(
registerRoute(
/\.json$/,
new workbox.strategies.NetworkFirst({
new NetworkFirst({
cacheName: 'stats-cache',
networkTimeoutSeconds: NETWORK_TIMEOUT_SECONDS
})
)

workbox.routing.registerRoute(
registerRoute(
// Cache HTML views files.
/\/views\/.*\.html$/,
// Use cache but update in the background.
new workbox.strategies.StaleWhileRevalidate({
new StaleWhileRevalidate({
// Use a custom cache name.
cacheName: 'view-cache'
})
)

workbox.routing.registerRoute(
registerRoute(
// Cache image files.
/\/img\/.*\.(?:png|jpg|jpeg|svg|gif)$/,
// Use the cache if it's available.
new workbox.strategies.CacheFirst({
new CacheFirst({
// Use a custom cache name.
cacheName: 'image-cache',
plugins: [
new workbox.expiration.Plugin({
new ExpirationPlugin({
// Cache only 20 images.
maxEntries: 20,
// Cache for a maximum of a week.
Expand All @@ -90,25 +107,25 @@ workbox.routing.registerRoute(
})
)

workbox.routing.registerRoute(
registerRoute(
// Cache CSS files.
/\.css$/,
// Use cache but update in the background.
new workbox.strategies.StaleWhileRevalidate({
new StaleWhileRevalidate({
// Use a custom cache name.
cacheName: 'css-cache'
})
)

workbox.routing.registerRoute(
registerRoute(
// Cache fonts.
/\/fonts\//,
// Use cache but update in the background.
new workbox.strategies.StaleWhileRevalidate({
new StaleWhileRevalidate({
// Use a custom cache name.
cacheName: 'font-cache',
plugins: [
new workbox.expiration.Plugin({
new ExpirationPlugin({
// Cache for a maximum of a year.
maxAgeSeconds: 365 * 24 * 60 * 60
})
Expand All @@ -117,23 +134,23 @@ workbox.routing.registerRoute(
)

// Cache the Google Fonts stylesheets with a stale-while-revalidate strategy.
workbox.routing.registerRoute(
registerRoute(
/^https:\/\/fonts\.googleapis\.com/,
new workbox.strategies.StaleWhileRevalidate({
new StaleWhileRevalidate({
cacheName: 'google-fonts-stylesheets'
})
)

// Cache the underlying font files with a cache-first strategy for 1 year.
workbox.routing.registerRoute(
registerRoute(
/^https:\/\/fonts\.gstatic\.com/,
new workbox.strategies.CacheFirst({
new CacheFirst({
cacheName: 'google-fonts-webfonts',
plugins: [
new workbox.cacheableResponse.Plugin({
new CacheableResponsePlugin({
statuses: [0, 200]
}),
new workbox.expiration.Plugin({
new ExpirationPlugin({
maxAgeSeconds: 60 * 60 * 24 * 365,
maxEntries: 30,
purgeOnQuotaError: true
Expand All @@ -142,24 +159,24 @@ workbox.routing.registerRoute(
})
)

workbox.googleAnalytics.initialize()
googleAnalyticsInitialize()

workbox.routing.registerRoute(
registerRoute(
/^https:\/\/maps\.googleapis\.com/,
new workbox.strategies.StaleWhileRevalidate({
new StaleWhileRevalidate({
cacheName: 'gmaps-cache'
})
)

workbox.routing.registerRoute(
registerRoute(
/^https:\/\/maps\.gstatic\.com/,
new workbox.strategies.CacheFirst({
new CacheFirst({
cacheName: 'gmaps-static-cache',
plugins: [
new workbox.cacheableResponse.Plugin({
new CacheableResponsePlugin({
statuses: [0, 200]
}),
new workbox.expiration.Plugin({
new ExpirationPlugin({
maxAgeSeconds: 60 * 60 * 24 * 365,
maxEntries: 30,
purgeOnQuotaError: true
Expand All @@ -168,5 +185,5 @@ workbox.routing.registerRoute(
})
)

workbox.core.skipWaiting()
workbox.core.clientsClaim()
self.skipWaiting()
clientsClaim()
Loading

0 comments on commit 2e42c9c

Please sign in to comment.