Skip to content

Commit ce06aa6

Browse files
committed
added swprecache
1 parent 0076243 commit ce06aa6

File tree

11 files changed

+153
-127
lines changed

11 files changed

+153
-127
lines changed

Diff for: README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ my-app/
5151
.babelrc
5252
.gitignore
5353
manifest.json
54-
my-service-worker.js
5554
next.config.js
5655
postcss.config.js
5756
routes.js
@@ -174,5 +173,5 @@ Any time that you want to "age" the redux store to make sure that these values w
174173
I don't like using relative paths if I don't have to (I hate trying to remember ../../..)! So I set up in the .babelrc file at the root all the aliases for different folders. If you add a folder to your project, add it in there too.
175174

176175
## Service Worker
177-
I set up a service worker at root for PWA support. Add any url you want to cache locally in that file. Be sure to customize the manifest.json file at the root with your project specifics.
176+
Using webpack's SWPrecache Plugin, I set up a service worker at root for PWA support. It is automatically registered on every page load through the Layout component. Be sure to customize the manifest.json file at the root with your project specifics.
178177

Diff for: actions/storageActions.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function syncStorage() {
3131
STORAGE.setStore(store)
3232
.then(() => {
3333
dispatch(setUpdated(ts)); //so times will match
34-
console.log('storage set');
34+
console.log('storage updated from store');
3535
return resolve();
3636
})
3737
.catch(err => {
@@ -44,7 +44,7 @@ export function syncStorage() {
4444
store.storage.updated = store.app.updated;
4545
STORAGE.setStore(store)
4646
.then(() => {
47-
console.log('storage set');
47+
console.log('storage updated from store');
4848
resolve();
4949
})
5050
.catch(err => {
@@ -56,13 +56,19 @@ export function syncStorage() {
5656
storage.app.updated = storage.storage.updated;
5757
console.log('storage later than store');
5858
dispatch(setWholeStore(storage));
59+
console.log('store updated from storage');
5960
resolve();
6061
}
61-
else {
62-
reject('There was a storage error');
62+
else if (store.app.updated === storage.storage.updated) {
63+
console.log('storage is in sync');
64+
resolve();
6365
}
6466

6567
})
68+
.catch(err => {
69+
console.error('storage err: ', err);
70+
reject(err.message);
71+
});
6672
});
6773
}
6874
}

Diff for: components/Layout/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ class Page extends Component {
2020
}
2121

2222
componentWillMount() {
23+
}
24+
25+
componentDidMount() {
2326
if (process.env.NODE_ENV ==='production') {
2427
registerSW();
2528
}
2629
this.props.initStorage();
27-
}
28-
29-
componentDidMount() {
3030
this.props.syncStorage();
3131
this.bound_onResize();
3232
window.addEventListener('resize', this.bound_onResize);

Diff for: my-service-worker.js

-19
This file was deleted.

Diff for: next.config.js

+14-45
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const path = require('path')
22
const glob = require('glob');
3-
3+
const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin');
44

55
module.exports = {
66
webpack: (config, { dev }) => {
@@ -33,53 +33,22 @@ module.exports = {
3333
}
3434
);
3535

36-
// config.plugins.push(
37-
// new SWPrecacheWebpackPlugin({
38-
// cacheId: 'my-project-name',
39-
// filename: 'my-project-service-worker.js',
40-
// verbose: true,
41-
// mergeStaticsConfig: true,
42-
// staticFileGlobsIgnorePatterns: [/\.next\//],
43-
// runtimeCaching: [
44-
// {
45-
// handler: 'networkFirst',
46-
// urlPattern: /^https?.*/
47-
// }
48-
// ]
49-
// })
50-
// );
51-
52-
/**
53-
* Install and Update our Service worker
54-
* on our main entry file :)
55-
* Reason: https://github.com/ooade/NextSimpleStarter/issues/32
56-
*/
57-
// const oldEntry = config.entry
58-
59-
// config.entry = () =>
60-
// oldEntry().then(entry => {
61-
// entry['main.js'].push(path.resolve('service-worker'))
62-
// return entry
63-
// })
36+
config.plugins.push(
37+
new SWPrecacheWebpackPlugin({
38+
verbose: true,
39+
staticFileGlobsIgnorePatterns: [/\.next\//],
40+
runtimeCaching: [
41+
{
42+
handler: 'networkFirst',
43+
urlPattern: /^https?.*/
44+
}
45+
]
46+
})
47+
)
6448

65-
// /* Enable only in Production */
66-
// if (!dev) {
67-
// // Service Worker
68-
// config.plugins.push(
69-
// new SWPrecacheWebpackPlugin({
70-
// verbose: true,
71-
// staticFileGlobsIgnorePatterns: [/\.next\//],
72-
// runtimeCaching: [
73-
// {
74-
// handler: 'nextworkFirst',
75-
// urlPattern: /^https?.*/
76-
// }
7749

78-
// ]
79-
// })
80-
// )
81-
// }
8250

51+
8352
return config
8453
}
8554
}

Diff for: offline/registerSW.js

+20-14
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1+
12
export default async function registerSW() {
2-
if ('serviceWorker' in navigator) {
3-
try {
4-
const registration = await navigator.serviceWorker.getRegistration('/');
5-
if (!registration) {
6-
await navigator.serviceWorker.register('/my-service-worker.js', {
7-
scope: '/'
8-
});
9-
10-
console.log(`Registration successful`);
11-
}
123

13-
} catch (e) {
14-
console.warn(`Registration failed: ${e.message}`);
4+
if (navigator && 'serviceWorker' in navigator) {
5+
try {
6+
const registration = await navigator.serviceWorker.getRegistration('/');
7+
if (!registration) {
8+
await navigator.serviceWorker.register('/service-worker.js', {
9+
scope: '/'
10+
});
11+
12+
console.log(`Registration successful`);
13+
}
14+
15+
} catch (e) {
16+
console.warn(`Registration failed: ${e.message}`);
17+
}
1518
}
16-
}
17-
}
19+
20+
}
21+
22+
23+

0 commit comments

Comments
 (0)