Commit f14ceb0f authored by Sebastien Rosset's avatar Sebastien Rosset

Service worker setup

parent a9a7ec4e
......@@ -17,6 +17,7 @@
/web/bundles/
/web/assets/css/*
/web/js/*
/web/sw.js
/node_modules/
/npm-debug.log
/TODO
......
......@@ -7,7 +7,8 @@ var gulp = require('gulp'),
notify = require('gulp-notify'),
gzip = require('gulp-gzip'),
del = require('del'),
notifier = require('node-notifier');
notifier = require('node-notifier'),
workboxBuild = require('workbox-build');
function handleError(err) {
console.log(err.toString());
......@@ -40,13 +41,31 @@ gulp.task('scriptsLibs', function() {
gulp.src(['node_modules/gogocarto-js/dist/gogocarto.js'])
.pipe(gulp.dest('web/js'));
return gulp.src(['src/Biopen/GeoDirectoryBundle/Resources/js/libs/**/*.js',
'src/Biopen/CoreBundle/Resources/js/libs/**/*.js',
'src/Biopen/CoreBundle/Resources/js/libs/**/*.js',
'src/Biopen/CoreBundle/Resources/js/init-sw.js',
'web/bundles/fosjsrouting/js/router.js',
])
.pipe(concat('libs.js'))
.pipe(gulp.dest('web/js'));
});
gulp.task('service-worker', () => {
return workboxBuild.injectManifest({
swSrc: 'src/Biopen/CoreBundle/Resources/js/sw.js',
swDest: 'web/sw.js',
globDirectory: 'web',
globPatterns: [
'+(assets|js|templates)\/**\/*.{js,css,html,png}',
'offline.html'
],
maximumFileSizeToCacheInBytes: 4 * 1024 * 1024
}).then(({count, size, warnings}) => {
// Optionally, log any warnings and details.
warnings.forEach(console.warn);
console.log(`${count} files will be precached, totaling ${size} bytes.`);
});
});
gulp.task('sass', function () {
return gulp.src(['src/Biopen/GeoDirectoryBundle/Resources/scss/**/*.scss',
'src/Biopen/CoreBundle/Resources/scss/**/*.scss',
......@@ -140,7 +159,7 @@ gulp.task('clean', function(cb) {
});
gulp.task('build', function() {
gulp.start('clean','sass', 'scriptsLibs', 'scriptsHome', 'scriptsExternalPages', 'scriptsElementForm', 'gogocarto_assets');
gulp.start('clean','sass', 'scriptsLibs', 'scriptsHome', 'scriptsExternalPages', 'scriptsElementForm', 'gogocarto_assets', 'service-worker');
});
gulp.task('production', function() {
......
This diff is collapsed.
......@@ -27,7 +27,8 @@
"gulp-sass": "^3.0.0",
"gulp-uglify": "^2.0.0",
"node-notifier": "^4.6.1",
"watchify": "^3.8.0"
"watchify": "^3.8.0",
"workbox-build": "^4.3.1"
},
"dependencies": {
"gogocarto-js": "^1.6.1",
......
if ('serviceWorker' in navigator) {
// Use the window load event to keep the page load performant
window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js');
});
}
importScripts('https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js');
console.log("Service-worker installing...");
workbox.core.skipWaiting();
workbox.core.clientsClaim();
self.addEventListener('fetch', event => {
event.respondWith(
caches.open(workbox.core.cacheNames.precache).then(precache => (
// Check if file is available in the cache
precache.match(event.request, { ignoreSearch: true }).then(response => {
// Cache hit - return response
if (response) {
return response;
} else {
// A request is a flux and can only be consumed once
// It is necessary to clone it to reuse it
const fetchRequest = event.request.clone();
return fetch(fetchRequest).catch(error => (
// If fetch fail, return
precache.match(new Request('/offline.html'), { ignoreSearch: true })
));
}
})
))
);
});
workbox.precaching.precacheAndRoute([]);
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="/assets/css/gogocarto.css">
</head>
<body style="text-align: center; background-color: lightgrey;">
<br/>
<br/>
<br/>
<img src="/assets/img/default-icon.png" style="max-width: 200px;" />
<br/>
Vous n'êtes pas connecté à Internet
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment