6
6
*/
7
7
/* eslint-disable no-restricted-globals */
8
8
9
- import { PrecacheController } from 'workbox-precaching' ;
9
+ import { PrecacheController , type PrecacheEntry } from 'workbox-precaching' ;
10
10
11
11
function parseSwParams ( ) {
12
12
const params = JSON . parse (
13
- new URLSearchParams ( self . location . search ) . get ( 'params' ) ,
13
+ new URLSearchParams ( self . location . search ) . get ( 'params' ) ! ,
14
14
) ;
15
15
if ( params . debug ) {
16
16
console . log ( '[Docusaurus-PWA][SW]: Service Worker params:' , params ) ;
@@ -22,7 +22,7 @@ function parseSwParams() {
22
22
// https://developers.google.com/web/tools/workbox/guides/using-bundlers#code_splitting_and_dynamic_imports
23
23
// https://twitter.com/sebastienlorber/status/1280155204575518720
24
24
// but looks it's working fine as it's inlined by webpack, need to double check
25
- async function runSWCustomCode ( params ) {
25
+ async function runSWCustomCode ( params : { offlineMode : boolean ; debug : boolean } ) {
26
26
if ( process . env . PWA_SW_CUSTOM ) {
27
27
const customSW = await import ( process . env . PWA_SW_CUSTOM ) ;
28
28
if ( typeof customSW . default === 'function' ) {
@@ -38,40 +38,34 @@ async function runSWCustomCode(params) {
38
38
/**
39
39
* Gets different possible variations for a request URL. Similar to
40
40
* https://git.io/JvixK
41
- *
42
- * @param {string } url
43
41
*/
44
- function getPossibleURLs ( url ) {
45
- const possibleURLs = [ ] ;
42
+ function getPossibleURLs ( url : string ) {
46
43
const urlObject = new URL ( url , self . location . href ) ;
47
44
48
45
if ( urlObject . origin !== self . location . origin ) {
49
- return possibleURLs ;
46
+ return [ ] ;
50
47
}
51
48
52
49
// Ignore search params and hash
53
50
urlObject . search = '' ;
54
51
urlObject . hash = '' ;
55
52
56
- // /blog.html
57
- possibleURLs . push ( urlObject . href ) ;
58
-
59
- // /blog/ => /blog/index.html
60
- if ( urlObject . pathname . endsWith ( '/' ) ) {
61
- possibleURLs . push ( `${ urlObject . href } index.html` ) ;
62
- } else {
53
+ return [
54
+ // /blog.html
55
+ urlObject . href ,
56
+ // /blog/ => /blog/index.html
63
57
// /blog => /blog/index.html
64
- possibleURLs . push ( `${ urlObject . href } /index.html` ) ;
65
- }
66
-
67
- return possibleURLs ;
58
+ `${ urlObject . href } ${ urlObject . pathname . endsWith ( '/' ) ? '' : '/' } index.html` ,
59
+ ] ;
68
60
}
69
61
70
62
( async ( ) => {
71
63
const params = parseSwParams ( ) ;
72
64
73
65
// eslint-disable-next-line no-underscore-dangle
74
- const precacheManifest = self . __WB_MANIFEST ;
66
+ const precacheManifest = (
67
+ self as typeof globalThis & { __WB_MANIFEST : ( string | PrecacheEntry ) [ ] }
68
+ ) . __WB_MANIFEST ;
75
69
const controller = new PrecacheController ( {
76
70
// Safer to turn this true?
77
71
fallbackToNetwork : true ,
@@ -80,41 +74,38 @@ function getPossibleURLs(url) {
80
74
if ( params . offlineMode ) {
81
75
controller . addToCacheList ( precacheManifest ) ;
82
76
if ( params . debug ) {
83
- console . log ( '[Docusaurus-PWA][SW]: addToCacheList' , {
84
- precacheManifest,
85
- } ) ;
77
+ console . log ( '[Docusaurus-PWA][SW]: addToCacheList' , { precacheManifest} ) ;
86
78
}
87
79
}
88
80
89
81
await runSWCustomCode ( params ) ;
90
82
91
83
self . addEventListener ( 'install' , ( event ) => {
92
84
if ( params . debug ) {
93
- console . log ( '[Docusaurus-PWA][SW]: install event' , {
94
- event,
95
- } ) ;
85
+ console . log ( '[Docusaurus-PWA][SW]: install event' , { event} ) ;
96
86
}
97
- event . waitUntil ( controller . install ( event ) ) ;
87
+ ( event as ExtendableEvent ) . waitUntil (
88
+ controller . install ( event as ExtendableEvent ) ,
89
+ ) ;
98
90
} ) ;
99
91
100
92
self . addEventListener ( 'activate' , ( event ) => {
101
93
if ( params . debug ) {
102
- console . log ( '[Docusaurus-PWA][SW]: activate event' , {
103
- event,
104
- } ) ;
94
+ console . log ( '[Docusaurus-PWA][SW]: activate event' , { event} ) ;
105
95
}
106
- event . waitUntil ( controller . activate ( event ) ) ;
96
+ ( event as ExtendableEvent ) . waitUntil (
97
+ controller . activate ( event as ExtendableEvent ) ,
98
+ ) ;
107
99
} ) ;
108
100
109
101
self . addEventListener ( 'fetch' , async ( event ) => {
110
102
if ( params . offlineMode ) {
111
- const requestURL = event . request . url ;
103
+ const requestURL = ( event as FetchEvent ) . request . url ;
112
104
const possibleURLs = getPossibleURLs ( requestURL ) ;
113
- for ( let i = 0 ; i < possibleURLs . length ; i += 1 ) {
114
- const possibleURL = possibleURLs [ i ] ;
105
+ for ( const possibleURL of possibleURLs ) {
115
106
const cacheKey = controller . getCacheKeyForURL ( possibleURL ) ;
116
107
if ( cacheKey ) {
117
- const cachedResponse = caches . match ( cacheKey ) ;
108
+ const cachedResponse = caches . match ( cacheKey ) as Promise < Response > ;
118
109
if ( params . debug ) {
119
110
console . log ( '[Docusaurus-PWA][SW]: serving cached asset' , {
120
111
requestURL,
@@ -124,7 +115,7 @@ function getPossibleURLs(url) {
124
115
cachedResponse,
125
116
} ) ;
126
117
}
127
- event . respondWith ( cachedResponse ) ;
118
+ ( event as FetchEvent ) . respondWith ( cachedResponse ) ;
128
119
break ;
129
120
}
130
121
}
@@ -133,15 +124,14 @@ function getPossibleURLs(url) {
133
124
134
125
self . addEventListener ( 'message' , async ( event ) => {
135
126
if ( params . debug ) {
136
- console . log ( '[Docusaurus-PWA][SW]: message event' , {
137
- event,
138
- } ) ;
127
+ console . log ( '[Docusaurus-PWA][SW]: message event' , { event} ) ;
139
128
}
140
129
141
- const type = event . data ?. type ;
130
+ const type = ( event as MessageEvent ) . data ?. type ;
142
131
143
132
if ( type === 'SKIP_WAITING' ) {
144
- self . skipWaiting ( ) ;
133
+ // lib def bug, see https://github.com/microsoft/TypeScript/issues/14877
134
+ ( self as typeof globalThis & ServiceWorkerGlobalScope ) . skipWaiting ( ) ;
145
135
}
146
136
} ) ;
147
137
} ) ( ) ;
0 commit comments