Skip to content

Commit 44496b4

Browse files
authored
Merge pull request #309 from sebastianbenz/network-first
Don't cache pages
2 parents 76fbf7e + 220a809 commit 44496b4

File tree

3 files changed

+52
-23
lines changed

3 files changed

+52
-23
lines changed

public/js/sw-page-transition-client.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class PageTransitionClient {
3030
* @private
3131
*/
3232
register() {
33-
this.log('page transition client registered for ' + this.urlWithoutFragment);
33+
this.log('registered for ' + this.urlWithoutFragment);
3434
// Register our serviceworker and let it know that we're waiting
3535
// for a fetch result
3636
if (this.serviceWorker && this.serviceWorker.controller) {
@@ -66,10 +66,14 @@ class PageTransitionClient {
6666
*/
6767
loadTargetPage() {
6868
if (this.debug) {
69+
// stay on the loading page
6970
return;
7071
}
71-
window.location.href = this.originalUrl;
72-
location.reload();
72+
const url = new URL(this.originalUrl);
73+
// force loading cached result
74+
url.searchParams.append('cache', 1);
75+
this.log('reloading ' + url);
76+
window.location.href = url.toString();
7377
}
7478

7579
/**
@@ -82,7 +86,7 @@ class PageTransitionClient {
8286
handleMessage(evt) {
8387
const message = evt.data;
8488
if (message.type === 'fetch' && message.url === this.urlWithoutFragment) {
85-
this.log('page transition client: received fetch signal for ' + message.url);
89+
this.log('received fetch signal for ' + message.url);
8690
this.serviceWorker.removeEventListener('message', this.handleMessage);
8791
this.loadTargetPage();
8892
}
@@ -106,7 +110,7 @@ class PageTransitionClient {
106110

107111
log(string) {
108112
if (this.debug) {
109-
console.log(string);
113+
console.log('[sw-page-transition-client] ' + string);
110114
}
111115
}
112116

public/js/sw-page-transition.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
const DEFAULT_CACHE_NAME = 'sw-page-transitions-cache';
2121

2222
/* eslint-disable no-unused-vars */
23+
2324
const transition = (function() {
2425
/* eslint-enable no-unused-vars */
2526

@@ -32,7 +33,7 @@ const transition = (function() {
3233
this._cacheName = cacheName;
3334
this._transitionPageMatchers = [];
3435
self.addEventListener('message', this.onMessageReceived.bind(this));
35-
this._debug = true;
36+
this._debug = false;
3637
}
3738

3839
/**
@@ -41,8 +42,8 @@ const transition = (function() {
4142
* @param {String} cacheName cache name for page transitions
4243
* @return {SwPageTransitionController} return controller
4344
*/
44-
cacheName(cacheName) {
45-
this._cacheName = cacheName;
45+
cacheName(name) {
46+
this._cacheName = name;
4647
return this;
4748
}
4849

@@ -72,19 +73,21 @@ const transition = (function() {
7273
if (request.mode !== 'navigate') {
7374
return Promise.resolve(null);
7475
}
75-
this.log('fetchWithPageTransition(' + request.url + ')');
76+
this.log('fetchWithPageTransition: fetch ' + request.url);
77+
const url = new URL(request.url);
78+
if (url.searchParams.get('cache') === '1') {
79+
url.searchParams.delete('cache');
80+
this.log('fetchWithPageTransition: returning cached page ' + url);
81+
return caches.match(new Request(url.toString()));
82+
}
7683
const transitionPage = this.findTransitionPage(request);
7784
// Return null if there is no transition page registered for this request.
7885
if (!transitionPage) {
86+
this.log('fetchWithPageTransition: no transition page found');
7987
return Promise.resolve(null);
8088
}
81-
// Returned requested page if in cache otherwise the transition page
82-
return caches.match(request).then(response => {
83-
if (response) {
84-
this.log('fetchWithPageTransition: returning cached page');
85-
}
86-
return response || this.fetchTransitionPage(request, transitionPage);
87-
});
89+
// Returned the transition page
90+
return this.fetchTransitionPage(request, transitionPage);
8891
}
8992

9093
/**
@@ -101,13 +104,20 @@ const transition = (function() {
101104
return caches.match(new Request(transitionPage))
102105
.then(response => {
103106
if (!response) {
104-
this.log('fetchWithPageTransition: loading page not found in cache');
107+
this.log('fetchTransitionPage: loading page not found in cache');
105108
return Promise.resolve(null);
106109
}
110+
this.log('fetchTransitionPage: return transition page');
107111
// Fetch the actual page and notify transition page when it's ready
108-
this.openCache().then(cache =>
109-
cache.add(request).then(() => this.notifyListeners(request.url))
110-
);
112+
this.openCache()
113+
.then(cache => cache.add(request)
114+
.then(() => this.notifyListeners(request.url))
115+
.catch(() => {
116+
// offline
117+
console.log('handling offline case');
118+
this.notifyListeners(request.url);
119+
})
120+
);
111121
return Promise.resolve(response);
112122
});
113123
}

public/sw.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ toolbox.options.debug = false;
1212
// Use page transitions
1313
importScripts('/js/sw-page-transition.js'); /* global transition */
1414

15+
const VERSION = '2';
16+
const PREFIX = 'gulliver';
17+
const CACHE_NAME = `${PREFIX}-v${VERSION}`;
18+
1519
// URL to return in place of the "offline dino" when client is
1620
// offline and requests a URL that's not in the cache.
1721
const OFFLINE_URL = '/.shell/offline';
@@ -36,6 +40,7 @@ const OFFLINE = [
3640
];
3741

3842
toolbox.precache(OFFLINE);
43+
toolbox.options.cache.name = CACHE_NAME;
3944

4045
// Cache the page registering the service worker. Without this, the
4146
// "first" page the user visits is only cached on the second visit,
@@ -46,6 +51,7 @@ toolbox.precache(
4651
})
4752
);
4853

54+
// Register and cache page transitions for list and detail page
4955
transition.cacheName(toolbox.options.cache.name)
5056
.registerPageTransition(/\/pwas\/.*/, TRANSITION_PWA_VIEW)
5157
.registerPageTransition(/\/.*/, TRANSITION_PWA_LIST);
@@ -59,7 +65,7 @@ toolbox.router.head(/^/, request => {
5965
// strategy, except that we cache.match(url) rather than cache.match(request) so
6066
// that the HEAD method is ignored on cache.match().
6167
function onlyIfCached(url) {
62-
return caches.open(toolbox.options.cache.name).then(cache => {
68+
return caches.open(CACHE_NAME).then(cache => {
6369
return cache.match(url).then(response => {
6470
return (response && response.status === 200) ? response : new Response('', {
6571
status: 504,
@@ -101,9 +107,18 @@ toolbox.router.default = (request, values, options) => {
101107
});
102108
};
103109

104-
// Claim clients so that the very first page load is controlled by a service
105-
// worker. (Important for responding correctly in offline state.)
106110
self.addEventListener('activate', () => self.clients.claim());
107111

112+
// Delete old caches and claim clients so that the very first page load is controlled by a service
113+
// worker. (Important for responding correctly in offline state.)
114+
self.addEventListener('activate', () =>
115+
caches.keys().then(cacheNames =>
116+
Promise.all(
117+
cacheNames.filter(cacheName => cacheName !== CACHE_NAME)
118+
.map(cacheName => caches.delete(cacheName))
119+
).then(self.clients.claim())
120+
)
121+
);
122+
108123
// Make sure the SW the page we register() is the service we use.
109124
self.addEventListener('install', () => self.skipWaiting());

0 commit comments

Comments
 (0)