git ssb

0+

Rômulo Alves / website



Commit 1bfb0d193cbe211d8658796595d6f4020592add5

Including service-workers cache

Rômulo Alves committed on 10/3/2016, 7:52:54 PM
Parent: 54088e85e5901d156b239ff0c750eecc099490de

Files changed

gulpfile.jschanged
src/index.htmlchanged
src/index.jsadded
src/service-worker.jsadded
package.jsonadded
gulpfile.jsView
@@ -1,18 +1,25 @@
11 const gulp = require('gulp');
22 const minifyHtml = require('gulp-minify-html');
33 const cleanCSS = require('gulp-clean-css');
44 const usemin = require('gulp-usemin');
5+const uglify = require('gulp-uglify');
56 const rev = require('gulp-rev');
67
78 gulp.task('copy', () =>
8- gulp.src('src/favicon.png')
9+ gulp.src(['src/favicon.png'])
910 .pipe(gulp.dest('dist/')));
1011
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 () {
1218 return gulp.src('src/index.html')
1319 .pipe(usemin({
1420 css: [ rev() ],
21+ js: [ uglify(), rev() ],
1522 html: [ minifyHtml({ empty: true }) ],
1623 inlinecss: [ cleanCSS, 'concat' ]
1724 }))
1825 .pipe(gulp.dest('dist/'));
src/index.htmlView
@@ -38,6 +38,9 @@
3838 </p>
3939 </section>
4040 </section>
4141 </div>
42+ <!-- build:js index.js -->
43+ <script src="index.js"></script>
44+ <!-- build:js index.js -->
4245 </body>
4346 </html>
src/index.jsView
@@ -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.jsView
@@ -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.jsonView
@@ -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