Skip to content

Commit b1ecae0

Browse files
committedJan 30, 2020
v4 to v5 migration
1 parent 923f995 commit b1ecae0

File tree

5 files changed

+61
-50
lines changed

5 files changed

+61
-50
lines changed
 

‎package.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@
4444
"rollup-plugin-babel": "^4.3.3",
4545
"rollup-plugin-commonjs": "^10.1.0",
4646
"rollup-plugin-node-resolve": "^5.2.0",
47+
"rollup-plugin-replace": "^2.2.0",
4748
"rollup-plugin-string": "^3.0.0",
48-
"workbox-cli": "^5.0.0"
49+
"workbox-cacheable-response": "^5.0.0",
Has a conversation. Original line has a conversation.
50+
"workbox-cli": "^5.0.0",
51+
"workbox-core": "^5.0.0",
52+
"workbox-expiration": "^5.0.0",
53+
"workbox-precaching": "^5.0.0",
54+
"workbox-routing": "^5.0.0",
55+
"workbox-strategies": "^5.0.0",
56+
"workbox-streams": "^5.0.0"
4957
}
5058
}

‎rollup.config.mjs

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
import {string} from 'rollup-plugin-string';
1818
import babel from 'rollup-plugin-babel';
1919
import compiler from '@ampproject/rollup-plugin-closure-compiler';
20-
import os from 'os';
21-
import path from 'path';
20+
import replace from 'rollup-plugin-replace';
2221
import resolve from 'rollup-plugin-node-resolve';
2322

2423
// The version of Chromium used by Samsung Internet 6.x.
@@ -58,6 +57,10 @@ export default [{
5857
}, {
5958
input: 'src/service-worker.mjs',
6059
plugins: [
60+
replace({
Has a conversation. Original line has a conversation.
61+
'process.env.NODE_ENV': JSON.stringify(
62+
process.env.NODE_ENV || 'development'),
63+
}),
6164
resolve(),
6265
babel({
6366
presets: [['@babel/preset-env', {
@@ -68,7 +71,7 @@ export default [{
6871
compiler(),
6972
],
7073
output: {
71-
file: path.join(os.tmpdir(), 'service-worker.js'),
74+
file: 'build/service-worker.js',
Has a conversation. Original line has a conversation.
7275
format: 'iife',
7376
},
7477
}, {

‎src/lib/partials.mjs

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
**/
1616

1717
export default {
18-
about: () => workbox.precaching.getCacheKeyForURL('partials/about.html'),
19-
foot: () => workbox.precaching.getCacheKeyForURL('partials/foot.html'),
20-
head: () => workbox.precaching.getCacheKeyForURL('partials/head.html'),
21-
navbar: () => workbox.precaching.getCacheKeyForURL('partials/navbar.html'),
18+
about: 'partials/about.html',
Has a conversation. Original line has a conversation.
19+
foot: 'partials/foot.html',
20+
head: 'partials/head.html',
21+
navbar: 'partials/navbar.html',
2222
};

‎src/service-worker.mjs

+41-40
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,50 @@
1414
* limitations under the License.
1515
**/
1616

17+
import {CacheableResponsePlugin} from 'workbox-cacheable-response';
Has a conversation. Original line has a conversation.
18+
import {CacheFirst, StaleWhileRevalidate} from 'workbox-strategies';
19+
import {cleanupOutdatedCaches, matchPrecache, precacheAndRoute}
20+
from 'workbox-precaching';
21+
import {clientsClaim, skipWaiting} from 'workbox-core';
22+
import {ExpirationPlugin} from 'workbox-expiration';
23+
import {registerRoute} from 'workbox-routing';
24+
import {strategy as streamsStrategy} from 'workbox-streams';
25+
1726
import {API_CACHE_NAME, DEFAULT_TAG} from './lib/constants.mjs';
1827
import * as templates from './lib/templates.mjs';
1928
import * as urls from './lib/urls.mjs';
2029
import partials from './lib/partials.mjs';
2130
import routeMatchers from './lib/route-matchers.mjs';
2231

23-
importScripts('https://storage.googleapis.com/workbox-cdn/releases/4.0.0-beta.1/workbox-sw.js');
24-
workbox.setConfig({
25-
debug: true,
26-
});
27-
workbox.precaching.precacheAndRoute([]);
28-
workbox.precaching.cleanupOutdatedCaches();
29-
30-
const cacheStrategy = new workbox.strategies.CacheFirst({
31-
cacheName: workbox.core.cacheNames.precache,
32-
});
32+
precacheAndRoute(self.__WB_MANIFEST);
Has a conversation. Original line has a conversation.
33+
cleanupOutdatedCaches();
3334

34-
const apiStrategy = new workbox.strategies.StaleWhileRevalidate({
35+
const apiStrategy = new StaleWhileRevalidate({
3536
cacheName: API_CACHE_NAME,
3637
plugins: [
37-
new workbox.expiration.Plugin({maxEntries: 50}),
38+
new ExpirationPlugin({maxEntries: 50}),
Has a conversation. Original line has a conversation.
3839
],
3940
});
4041

41-
workbox.routing.registerRoute(
42+
registerRoute(
4243
routeMatchers.get('about'),
43-
workbox.streams.strategy([
44-
() => cacheStrategy.makeRequest({request: partials.head()}),
45-
() => cacheStrategy.makeRequest({request: partials.navbar()}),
46-
() => cacheStrategy.makeRequest({request: partials.about()}),
47-
() => cacheStrategy.makeRequest({request: partials.foot()}),
44+
streamsStrategy([
45+
() => matchPrecache(partials.head),
Has a conversation. Original line has a conversation.
46+
() => matchPrecache(partials.navbar),
47+
() => matchPrecache(partials.about),
48+
() => matchPrecache(partials.foot),
4849
])
4950
);
5051

51-
workbox.routing.registerRoute(
52+
registerRoute(
5253
routeMatchers.get('questions'),
53-
workbox.streams.strategy([
54-
() => cacheStrategy.makeRequest({request: partials.head()}),
55-
() => cacheStrategy.makeRequest({request: partials.navbar()}),
56-
async ({event, url, params}) => {
54+
streamsStrategy([
55+
() => matchPrecache(partials.head),
56+
() => matchPrecache(partials.navbar),
57+
async ({event, params}) => {
5758
try {
5859
const questionId = params[1];
59-
const questionResponse = await apiStrategy.makeRequest({
60+
const questionResponse = await apiStrategy.handle({
Has a conversation. Original line has a conversation.
6061
event,
6162
request: urls.getQuestion(questionId),
6263
});
@@ -66,19 +67,19 @@ workbox.routing.registerRoute(
6667
return templates.error(error.message);
6768
}
6869
},
69-
() => cacheStrategy.makeRequest({request: partials.foot()}),
70+
() => matchPrecache(partials.foot),
7071
])
7172
);
7273

73-
workbox.routing.registerRoute(
74+
registerRoute(
7475
routeMatchers.get('index'),
75-
workbox.streams.strategy([
76-
() => cacheStrategy.makeRequest({request: partials.head()}),
77-
() => cacheStrategy.makeRequest({request: partials.navbar()}),
76+
streamsStrategy([
77+
() => matchPrecache(partials.head),
78+
() => matchPrecache(partials.navbar),
7879
async ({event, url}) => {
7980
try {
8081
const tag = url.searchParams.get('tag') || DEFAULT_TAG;
81-
const listResponse = await apiStrategy.makeRequest({
82+
const listResponse = await apiStrategy.handle({
8283
event,
8384
request: urls.listQuestionsForTag(tag),
8485
});
@@ -88,37 +89,37 @@ workbox.routing.registerRoute(
8889
return templates.error(error.message);
8990
}
9091
},
91-
() => cacheStrategy.makeRequest({request: partials.foot()}),
92+
() => matchPrecache(partials.foot),
9293
])
9394
);
9495

9596
// Gravatar images support CORS, so we won't be storing opaque responses.
96-
workbox.routing.registerRoute(
97+
registerRoute(
9798
new RegExp('https://www\\.gravatar\\.com/'),
98-
new workbox.strategies.CacheFirst({
99+
new CacheFirst({
99100
cacheName: 'profile-images',
100101
plugins: [
101-
new workbox.expiration.Plugin({
102+
new ExpirationPlugin({
102103
maxEntries: 50,
103104
purgeOnQuotaError: true,
104105
}),
105106
],
106107
})
107108
);
108109

109-
workbox.routing.registerRoute(
110+
registerRoute(
110111
new RegExp('^https://.*(?:\\.jpg|\\.png)'),
111-
new workbox.strategies.CacheFirst({
112+
new CacheFirst({
112113
cacheName: 'other-images',
113114
plugins: [
114-
new workbox.cacheableResponse.Plugin({statuses: [0, 200]}),
115-
new workbox.expiration.Plugin({
115+
new CacheableResponsePlugin({statuses: [0, 200]}),
116+
new ExpirationPlugin({
116117
maxEntries: 10,
117118
purgeOnQuotaError: true,
118119
}),
119120
],
120121
})
121122
);
122123

123-
workbox.core.skipWaiting();
124-
workbox.core.clientsClaim();
124+
skipWaiting();
125+
clientsClaim();

‎workbox-config.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414
* limitations under the License.
1515
**/
1616

17-
const os = require('os');
1817
const path = require('path');
1918

2019
module.exports = {
2120
globDirectory: 'build',
2221
globPatterns: [
2322
'**/*.{html,js,svg,png,json}',
2423
],
25-
swSrc: path.join(os.tmpdir(), 'service-worker.js'),
24+
swSrc: path.join('build', 'service-worker.js'),
Has a conversation. Original line has a conversation.
2625
swDest: path.join('build', 'service-worker.js'),
2726
};

0 commit comments

Comments
 (0)
Please sign in to comment.