Commit 1bfb0d193cbe211d8658796595d6f4020592add5
Including service-workers cache
Rômulo Alves committed on 10/3/2016, 7:52:54 PMParent: 54088e85e5901d156b239ff0c750eecc099490de
Files changed
gulpfile.js | changed |
src/index.html | changed |
src/index.js | added |
src/service-worker.js | added |
package.json | added |
gulpfile.js | ||
---|---|---|
@@ -1,18 +1,25 @@ | ||
1 | 1 | const gulp = require('gulp'); |
2 | 2 | const minifyHtml = require('gulp-minify-html'); |
3 | 3 | const cleanCSS = require('gulp-clean-css'); |
4 | 4 | const usemin = require('gulp-usemin'); |
5 | +const uglify = require('gulp-uglify'); | |
5 | 6 | const rev = require('gulp-rev'); |
6 | 7 | |
7 | 8 | gulp.task('copy', () => |
8 | - gulp.src('src/favicon.png') | |
9 | + gulp.src(['src/favicon.png']) | |
9 | 10 | .pipe(gulp.dest('dist/'))); |
10 | 11 | |
11 | -gulp.task('build', ['copy'], function () { | |
12 | +gulp.task('sw', () => | |
13 | + gulp.src(['src/service-worker.js']) | |
14 | + .pipe(uglify()) | |
15 | + .pipe(gulp.dest('dist/'))); | |
16 | + | |
17 | +gulp.task('build', ['copy', 'sw'], function () { | |
12 | 18 | return gulp.src('src/index.html') |
13 | 19 | .pipe(usemin({ |
14 | 20 | css: [ rev() ], |
21 | + js: [ uglify(), rev() ], | |
15 | 22 | html: [ minifyHtml({ empty: true }) ], |
16 | 23 | inlinecss: [ cleanCSS, 'concat' ] |
17 | 24 | })) |
18 | 25 | .pipe(gulp.dest('dist/')); |
src/index.html | ||
---|---|---|
@@ -38,6 +38,9 @@ | ||
38 | 38 | </p> |
39 | 39 | </section> |
40 | 40 | </section> |
41 | 41 | </div> |
42 | + <!-- build:js index.js --> | |
43 | + <script src="index.js"></script> | |
44 | + <!-- build:js index.js --> | |
42 | 45 | </body> |
43 | 46 | </html> |
src/index.js | ||
---|---|---|
@@ -1,0 +1,5 @@ | ||
1 | +if ('serviceWorker' in navigator) { | |
2 | + navigator.serviceWorker.register('/service-worker.js').then(function(registration) { | |
3 | + console.log('ServiceWorker for offline registered!', registration.scope); | |
4 | + }); | |
5 | +} |
src/service-worker.js | ||
---|---|---|
@@ -1,0 +1,42 @@ | ||
1 | +var CACHE_NAME = '20161003'; | |
2 | +var urlsToCache = [ | |
3 | + '/', | |
4 | + '/favicon.png' | |
5 | +]; | |
6 | + | |
7 | +self.addEventListener('install', function(event) { | |
8 | + event.waitUntil( | |
9 | + caches.open(CACHE_NAME) | |
10 | + .then(function(cache) { | |
11 | + return cache.addAll(urlsToCache); | |
12 | + }) | |
13 | + ); | |
14 | +}); | |
15 | + | |
16 | +self.addEventListener('fetch', function(event) { | |
17 | + event.respondWith( | |
18 | + caches.match(event.request) | |
19 | + .then(function(response) { | |
20 | + if (response) { | |
21 | + return response; | |
22 | + } | |
23 | + | |
24 | + var fetchRequest = event.request.clone(); | |
25 | + return fetch(fetchRequest).then( | |
26 | + function(response) { | |
27 | + if(!response || response.status !== 200 || response.type !== 'basic') { | |
28 | + return response; | |
29 | + } | |
30 | + | |
31 | + var responseToCache = response.clone(); | |
32 | + caches.open(CACHE_NAME) | |
33 | + .then(function(cache) { | |
34 | + cache.put(event.request, responseToCache); | |
35 | + }); | |
36 | + | |
37 | + return response; | |
38 | + } | |
39 | + ); | |
40 | + }) | |
41 | + ); | |
42 | +}); |
package.json | ||
---|---|---|
@@ -1,0 +1,28 @@ | ||
1 | +{ | |
2 | + "name": "romuloalves.github.io", | |
3 | + "version": "1.0.0", | |
4 | + "description": "", | |
5 | + "main": "gulpfile.js", | |
6 | + "dependencies": {}, | |
7 | + "devDependencies": { | |
8 | + "gulp": "^3.9.1", | |
9 | + "gulp-clean-css": "^2.0.13", | |
10 | + "gulp-minify-html": "^1.0.6", | |
11 | + "gulp-rev": "^7.1.2", | |
12 | + "gulp-uglify": "^2.0.0", | |
13 | + "gulp-usemin": "^0.3.24" | |
14 | + }, | |
15 | + "scripts": { | |
16 | + "test": "echo \"Error: no test specified\" && exit 1" | |
17 | + }, | |
18 | + "repository": { | |
19 | + "type": "git", | |
20 | + "url": "git+https://github.com/romuloalves/romuloalves.github.io.git" | |
21 | + }, | |
22 | + "author": "romuloalves", | |
23 | + "license": "MIT", | |
24 | + "bugs": { | |
25 | + "url": "https://github.com/romuloalves/romuloalves.github.io/issues" | |
26 | + }, | |
27 | + "homepage": "https://github.com/romuloalves/romuloalves.github.io#readme" | |
28 | +} |
Built with git-ssb-web