Skip to content

Commit 3db13a1

Browse files
committed
test sw
1 parent e561c07 commit 3db13a1

6 files changed

Lines changed: 92 additions & 3 deletions

File tree

‎cache.manifest‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CACHE MANIFEST
2-
# Build: 20170520024022
2+
# Build: 20170522080022
33

44
CACHE:
55
resources/fonts/Dressedless_Three.svg
@@ -15,6 +15,7 @@ resources/images/paypal.png
1515
resources/images/twohardtings.jpg
1616
resources/images/wechatpay.jpg
1717
resources/images/zhifubao.png
18+
src/sw.js
1819
src/lib/all.207da233.js
1920

2021
NETWORK:

‎gulpfile.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ gulp.task("dist:appjs", function() {
8888
}))
8989
.pipe(gulp.dest('./static/app/src'));
9090
});
91+
gulp.task("dist:swjs", function() {
92+
return gulp.src(['./static/app/src/sw.js'])
93+
.pipe(cachebust.references())
94+
.pipe(gulp.dest('./src/'));
95+
});
9196
gulp.task('dist:html', function () {
9297
return gulp.src(['./static/app/*.html'])
9398
.pipe($.fileInclude({
@@ -150,7 +155,7 @@ gulp.task('prepare', function (cb) {
150155
runSequence('build_version', cb);
151156
});
152157
gulp.task('compile', function (cb) {
153-
runSequence('prepare', 'sass', 'dist:libjs', 'dist:appjs', 'dist:html', 'manifest', cb);
158+
runSequence('prepare', 'sass', 'dist:libjs', 'dist:appjs', 'dist:swjs', 'dist:html', 'manifest', cb);
154159
});
155160
gulp.task('default', function (cb) {
156161
runSequence('clean:dist', 'compile', 'watch', 'serve', cb);

‎index.html‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎src/sw.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Here comes the install event!
2+
// This only happens once, when the browser sees this
3+
// version of the ServiceWorker for the first time.
4+
self.addEventListener('install', function(event) {
5+
// We pass a promise to event.waitUntil to signal how
6+
// long install takes, and if it failed
7+
event.waitUntil(
8+
// We open a cache…
9+
caches.open('simple-sw-v1').then(function(cache) {
10+
// And add resources to it
11+
return cache.addAll([
12+
'src/lib/all.207da233.js'
13+
]);
14+
})
15+
);
16+
});
17+
18+
// The fetch event happens for the page request with the
19+
// ServiceWorker's scope, and any request made within that
20+
// page
21+
self.addEventListener('fetch', function(event) {
22+
// Calling event.respondWith means we're in charge
23+
// of providing the response. We pass in a promise
24+
// that resolves with a response object
25+
event.respondWith(
26+
// First we look for something in the caches that
27+
// matches the request
28+
caches.match(event.request).then(function(response) {
29+
// If we get something, we return it, otherwise
30+
// it's null, and we'll pass the request to
31+
// fetch, which will use the network.
32+
return response || fetch(event.request);
33+
})
34+
);
35+
});

‎static/app/html/scripts.html‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,16 @@
99
<script>
1010
@@include("src/AppBundle.js")
1111
</script>
12+
<script>
13+
if ('serviceWorker' in navigator) {
14+
window.addEventListener('load', function() {
15+
navigator.serviceWorker.register('src/sw.js').then(function(registration) {
16+
// Registration was successful
17+
console.log('ServiceWorker registration successful with scope: ', registration.scope);
18+
}, function(err) {
19+
// registration failed :(
20+
console.log('ServiceWorker registration failed: ', err);
21+
});
22+
});
23+
}
24+
</script>

‎static/app/src/sw.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Here comes the install event!
2+
// This only happens once, when the browser sees this
3+
// version of the ServiceWorker for the first time.
4+
self.addEventListener('install', function(event) {
5+
// We pass a promise to event.waitUntil to signal how
6+
// long install takes, and if it failed
7+
event.waitUntil(
8+
// We open a cache…
9+
caches.open('simple-sw-v1').then(function(cache) {
10+
// And add resources to it
11+
return cache.addAll([
12+
'src/lib/all.js'
13+
]);
14+
})
15+
);
16+
});
17+
18+
// The fetch event happens for the page request with the
19+
// ServiceWorker's scope, and any request made within that
20+
// page
21+
self.addEventListener('fetch', function(event) {
22+
// Calling event.respondWith means we're in charge
23+
// of providing the response. We pass in a promise
24+
// that resolves with a response object
25+
event.respondWith(
26+
// First we look for something in the caches that
27+
// matches the request
28+
caches.match(event.request).then(function(response) {
29+
// If we get something, we return it, otherwise
30+
// it's null, and we'll pass the request to
31+
// fetch, which will use the network.
32+
return response || fetch(event.request);
33+
})
34+
);
35+
});

0 commit comments

Comments
 (0)