Skip to content

Commit

Permalink
update(all): update sw to use Workbox
Browse files Browse the repository at this point in the history
This commit updates the app so that it uses workbox-sw to write the service worker and workbox-build to inject a manifest into the service worker.
  • Loading branch information
nasearle committed Apr 12, 2018
1 parent c128216 commit 377eb2b
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# Skip dependencies and builds
node_modules/
dist/

.vscode/
2 changes: 1 addition & 1 deletion solution/app/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {hasPrerequisites} from 'features';

if ('serviceWorker' in navigator &&
(window.location.protocol === 'https:' || isLocalhost)) {
navigator.serviceWorker.register('service-worker.js')
navigator.serviceWorker.register('sw.js')
.then(function(registration) {
// updatefound is fired if service-worker.js changes.
registration.onupdatefound = function() {
Expand Down
103 changes: 0 additions & 103 deletions solution/app/service-worker.js

This file was deleted.

38 changes: 38 additions & 0 deletions solution/app/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2016 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Copyright 2016 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.0.0/workbox-sw.js');

workbox.precaching.precacheAndRoute([]);

workbox.routing.registerRoute(/(.*)/,
workbox.strategies.cacheFirst({cacheName: 'furniture-store'})
);
21 changes: 20 additions & 1 deletion solution/gulpfile.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,29 @@ import source from 'vinyl-source-stream';
import browserSync from 'browser-sync';
import nodemon from 'gulp-nodemon';
import gulpLoadPlugins from 'gulp-load-plugins';
import workboxBuild from 'workbox-build';

const $ = gulpLoadPlugins();
const bs = browserSync.create();

// Inject a precache manifest into the service worker
gulp.task('build-sw', () => {
return workboxBuild.injectManifest({
swSrc: 'app/sw.js',
swDest: 'dist/sw.js',
globDirectory: 'dist',
globPatterns: [
'/',
'index.html',
'scripts/main.min.js',
'styles/main.css',
'images/**/*'
]
}).catch(err => {
console.log('Uh oh 😬', err);
});
});

// Optimize images
gulp.task('images', () => {
gulp.src('app/images/**/*')
Expand Down Expand Up @@ -173,7 +192,7 @@ gulp.task('default', ['clean'], cb => {
runSequence(
'styles',
['html', 'scripts', 'images', 'copy'],
// ['lint', 'html', 'scripts', 'images', 'copy'],
'build-sw',
cb
);
});
Expand Down
Loading

0 comments on commit 377eb2b

Please sign in to comment.