2020const DEFAULT_CACHE_NAME = 'sw-page-transitions-cache' ;
2121
2222/* eslint-disable no-unused-vars */
23+
2324const 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 }
0 commit comments