File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html >
3+
4+ < head >
5+ < meta charset ="utf-8 " />
6+ < meta http-equiv ="X-UA-Compatible " content ="IE=edge ">
7+ < title > network-idle-callback demo</ title >
8+ < meta name ="viewport " content ="width=device-width, initial-scale=1 ">
9+ < link rel ="stylesheet " type ="text/css " media ="screen " href ="../test/main.css " />
10+ < script src ="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver "> </ script >
11+ < script >
12+ if ( 'serviceWorker' in navigator ) {
13+ window . addEventListener ( 'load' , function ( ) {
14+ navigator . serviceWorker . register ( './sw.js' ) . then ( function ( registration ) {
15+ console . log ( 'ServiceWorker registration successful with scope: ' , registration . scope ) ;
16+ } , function ( err ) {
17+ console . log ( 'ServiceWorker registration failed: ' , err ) ;
18+ } ) ;
19+ } ) ;
20+ }
21+ </ script >
22+ </ head >
23+
24+ < body >
25+ < p > This demo uses [network-idle-callback](https://github.com/pastelsky/network-idle-callback) to only prefetch when network activity goes idle in the current tab.</ p >
26+ < a href ="../test/1.html "> Link 1</ a >
27+ < a href ="../test/2.html "> Link 2</ a >
28+ < a href ="../test/3.html "> Link 3</ a >
29+ < section id ="stuff ">
30+ < a href ="../test/main.css "> CSS</ a >
31+ </ section >
32+ < a href ="../test/4.html " style ="position:absolute;margin-top:900px; "> Link 4</ a >
33+ < script src ="https://unpkg.com/network-idle-callback@1.0.1/lib/index.js "> </ script >
34+ < script type ="module ">
35+ import quicklink from "../dist/quicklink.mjs" ;
36+ quicklink ( { timeoutFn : networkIdleCallback } ) ;
37+ </ script >
38+ </ body >
39+
40+ </ html >
Original file line number Diff line number Diff line change 1+ importScripts ( 'https://unpkg.com/network-idle-callback@1.0.1/lib/request-monitor.js' ) ;
2+
3+ self . addEventListener ( 'install' , function ( e ) {
4+ console . log ( '[ServiceWorker] Installed' ) ;
5+ } ) ;
6+
7+ self . addEventListener ( 'activate' , function ( e ) {
8+ console . log ( '[ServiceWorker] Activated' ) ;
9+ } ) ;
10+
11+ self . addEventListener ( 'fetch' , function ( e ) {
12+ console . log ( '[ServiceWorker] Fetch' , e . request . url ) ;
13+ self . requestMonitor . listen ( e ) ;
14+
15+ const promise = fetch ( e . request )
16+ . then ( response => {
17+ console . log ( 'done' , e . clientId ) ;
18+ self . requestMonitor . unlisten ( e ) ;
19+ return response ;
20+ } )
21+ . catch ( e => {
22+ console . log ( 'error' ) ;
23+ self . requestMonitor . unlisten ( e ) ;
24+ } ) ;
25+
26+ e . respondWith ( promise ) ;
27+ } ) ;
You can’t perform that action at this time.
0 commit comments