Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a service worker using Workbox #191

Merged
merged 2 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Adds a service worker using Workbox.
  • Loading branch information
jeffposnick committed Jul 10, 2018
commit 4c9bf440d784e14348e25993f48ff880f99e7c23
4 changes: 2 additions & 2 deletions app/html/_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>Android Asset Studio{% if title %} - {{ title }}{% endif %}</title>

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:400,500|Roboto+Mono:400,500">
<link rel="stylesheet" href="//fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:400,500|Roboto+Mono:400,500">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="lib/spectrum/spectrum.css">
<link rel="stylesheet" href="styles/app.css">
<link href="favicon.ico" rel="icon">
Expand Down
6 changes: 6 additions & 0 deletions app/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ import {studio} from './studio';
import {imagelib} from './imagelib';

window.pages = pages;

window.addEventListener('load', function() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js');
}
});
21 changes: 21 additions & 0 deletions app/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
importScripts("https://storage.googleapis.com/workbox-cdn/releases/3.3.1/workbox-sw.js");

workbox.precaching.precacheAndRoute([]);

workbox.routing.registerRoute(
new RegExp('https://(?:fonts|www).(?:googleapis|gstatic).com/(.*)'),
workbox.strategies.cacheFirst({
cacheName: 'google-fonts',
plugins: [
new workbox.expiration.Plugin({
maxEntries: 20,
purgeOnQuotaError: true,
}),
new workbox.cacheableResponse.Plugin({
statuses: [0, 200]
}),
],
}),
);

workbox.googleAnalytics.initialize();
34 changes: 30 additions & 4 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const through = require('through2');
const url = require('url');
const yaml = require('js-yaml');
const path = require('path');

const workboxBuild = require('workbox-build');
const prettyBytes = require('pretty-bytes');

var AUTOPREFIXER_BROWSERS = [
'ie >= 10',
Expand Down Expand Up @@ -242,11 +243,36 @@ gulp.task('serve:dist', ['default'], function () {
});
});

gulp.task('service-worker', function() {
return workboxBuild.injectManifest({
swSrc: path.join('app', 'sw.js'),
swDest: path.join('dist', 'sw.js'),
globDirectory: 'dist',
globPatterns: [
'*.html',
'**/jquery.min.js',
'**/spectrum.{css,js}',
'res/**/*.svg',
'scripts/**/*.js',
'styles/**/*.css',
],
}).then(function(obj) {
obj.warnings.forEach(function(warning) {
console.warn(warning);
});
console.log('A service worker was generated to precache', obj.count,
'files, totalling', prettyBytes(obj.size));
});
});

// Build Production Files, the Default Task
gulp.task('default', ['clean'], function (cb) {
runSequence('styles',
['scripts', 'bower', 'html', 'res', 'copy'],
cb);
runSequence(
'styles',
['scripts', 'bower', 'html', 'res', 'copy'],
'service-worker',
cb
);
});

// Deploy to GitHub pages
Expand Down
Loading