@@ -2,120 +2,154 @@ importScripts(
2
2
'https://storage.googleapis.com/workbox-cdn/releases/7.3.0/workbox-sw.js'
3
3
) ;
4
4
5
- // This is your Service Worker, you can put any of your custom Service Worker
6
- // code in this file, above the `precacheAndRoute` line.
7
-
8
- // When widget is installed/pinned, push initial state.
9
- self . addEventListener ( 'widgetinstall' , ( event ) => {
10
- event . waitUntil ( updateWidget ( event ) ) ;
11
- } ) ;
12
-
13
- // When widget is shown, update content to ensure it is up-to-date.
14
- self . addEventListener ( 'widgetresume' , ( event ) => {
15
- event . waitUntil ( updateWidget ( event ) ) ;
16
- } ) ;
17
-
18
- // When the user clicks an element with an associated Action.Execute,
19
- // handle according to the 'verb' in event.action.
20
- self . addEventListener ( 'widgetclick' , ( event ) => {
21
- if ( event . action == "updateName" ) {
22
- event . waitUntil ( updateName ( event ) ) ;
5
+ if ( workbox ) {
6
+ console . log ( 'Workbox loaded successfully!' ) ;
7
+ console . log ( 'workbox.core:' , workbox . core ) ; // Add this line
8
+
9
+ workbox . core . initializeApp ( ) ; // Access initializeApp through the global workbox object
10
+
11
+ const { NetworkFirst, CacheFirst, StaleWhileRevalidate } = workbox . strategies ;
12
+ const { registerRoute } = workbox . routing ;
13
+ const { precacheAndRoute } = workbox . precaching ;
14
+
15
+ // This is your Service Worker, you can put any of your custom Service Worker
16
+ // code in this file, above the `precacheAndRoute` line.
17
+
18
+ // When widget is installed/pinned, push initial state.
19
+ self . addEventListener ( 'widgetinstall' , ( event ) => {
20
+ event . waitUntil ( updateWidget ( event ) ) ;
21
+ } ) ;
22
+
23
+ // When widget is shown, update content to ensure it is up-to-date.
24
+ self . addEventListener ( 'widgetresume' , ( event ) => {
25
+ event . waitUntil ( updateWidget ( event ) ) ;
26
+ } ) ;
27
+
28
+ // When the user clicks an element with an associated Action.Execute,
29
+ // handle according to the 'verb' in event.action.
30
+ self . addEventListener ( 'widgetclick' , ( event ) => {
31
+ if ( event . action == "updateName" ) {
32
+ event . waitUntil ( updateName ( event ) ) ;
33
+ }
34
+ } ) ;
35
+
36
+ // When the widget is uninstalled/unpinned, clean up any unnecessary
37
+ // periodic sync or widget-related state.
38
+ self . addEventListener ( 'widgetuninstall' , ( event ) => { } ) ;
39
+
40
+ const updateWidget = async ( event ) => {
41
+ // The widget definition represents the fields specified in the manifest.
42
+ const widgetDefinition = event . widget . definition ;
43
+
44
+ // Fetch the template and data defined in the manifest to generate the payload.
45
+ const payload = {
46
+ template : JSON . stringify ( await ( await fetch ( widgetDefinition . msAcTemplate ) ) . json ( ) ) ,
47
+ data : JSON . stringify ( await ( await fetch ( widgetDefinition . data ) ) . json ( ) ) ,
48
+ } ;
49
+
50
+ // Push payload to widget.
51
+ await self . widgets . updateByInstanceId ( event . instanceId , payload ) ;
23
52
}
24
- } ) ;
25
53
26
- // When the widget is uninstalled/unpinned, clean up any unnecessary
27
- // periodic sync or widget-related state.
28
- self . addEventListener ( 'widgetuninstall' , ( event ) => { } ) ;
54
+ const updateName = async ( event ) => {
55
+ const name = event . data . json ( ) . name ;
29
56
30
- const updateWidget = async ( event ) => {
31
- // The widget definition represents the fields specified in the manifest.
32
- const widgetDefinition = event . widget . definition ;
57
+ // The widget definition represents the fields specified in the manifest.
58
+ const widgetDefinition = event . widget . definition ;
33
59
34
- // Fetch the template and data defined in the manifest to generate the payload.
35
- const payload = {
36
- template : JSON . stringify ( await ( await fetch ( widgetDefinition . msAcTemplate ) ) . json ( ) ) ,
37
- data : JSON . stringify ( await ( await fetch ( widgetDefinition . data ) ) . json ( ) ) ,
38
- } ;
60
+ // Fetch the template and data defined in the manifest to generate the payload.
61
+ const payload = {
62
+ template : JSON . stringify ( await ( await fetch ( widgetDefinition . msAcTemplate ) ) . json ( ) ) ,
63
+ data : JSON . stringify ( { name } ) ,
64
+ } ;
39
65
40
- // Push payload to widget.
41
- await self . widgets . updateByInstanceId ( event . instanceId , payload ) ;
42
- }
43
-
44
- const updateName = async ( event ) => {
45
- const name = event . data . json ( ) . name ;
46
-
47
- // The widget definition represents the fields specified in the manifest.
48
- const widgetDefinition = event . widget . definition ;
49
-
50
- // Fetch the template and data defined in the manifest to generate the payload.
51
- const payload = {
52
- template : JSON . stringify ( await ( await fetch ( widgetDefinition . msAcTemplate ) ) . json ( ) ) ,
53
- data : JSON . stringify ( { name } ) ,
54
- } ;
55
-
56
- // Push payload to widget.
57
- await self . widgets . updateByInstanceId ( event . instanceId , payload ) ;
58
- }
59
-
60
- // Workbox Precaching
61
- workbox . precaching . precacheAndRoute ( self . __WB_MANIFEST || [ ] ) ;
62
-
63
- // Cache data
64
- workbox . routing . registerRoute (
65
- ( { url} ) => url . pathname . startsWith ( '/data' ) ,
66
- new workbox . strategies . CacheFirst ( )
67
- ) ;
68
- // Cache app images
69
- workbox . routing . registerRoute (
70
- ( { url} ) => url . pathname . startsWith ( '/assets' ) ,
71
- new workbox . strategies . CacheFirst ( )
72
- ) ;
73
-
74
- // Cache app CSS and JS files
75
- workbox . routing . registerRoute (
76
- ( { request} ) => request . destination === 'script' ||
77
- request . destination === 'style' ||
78
- request . destination === 'module' , // Cache dynamically imported modules
79
- new workbox . strategies . StaleWhileRevalidate ( )
80
- ) ;
81
-
82
-
83
- // Navigation Routing
84
- workbox . routing . registerRoute (
85
- ( { request } ) => request . mode === 'navigate' ,
86
- workbox . strategies . networkFirst ( )
87
- ) ;
88
-
89
- self . addEventListener ( 'push' , function ( event ) {
90
- console . log ( '[Service Worker] Push Received.' ) ;
91
- if ( Notification . permission === "granted" ) {
92
- const data = event . data . json ( ) ;
93
- event . waitUntil (
94
- self . registration . showNotification ( data . title , {
95
- body : data . body ,
96
- icon : '/assets/icons/icon_192.png' , // Replace with your icon
97
- tag : 'my-tag' // Optional: prevents multiple notifications with the same tag
98
- } )
99
- ) ;
100
- } else {
101
- console . log ( "Notification permission has been denied" ) ;
66
+ // Push payload to widget.
67
+ await self . widgets . updateByInstanceId ( event . instanceId , payload ) ;
102
68
}
103
- } ) ;
104
-
105
- self . addEventListener ( 'notificationclick' , ( event ) => {
106
- event . notification . close ( ) ;
107
- var fullPath = self . location . origin + event . notification . data . path ;
108
- clients . openWindow ( fullPath ) ;
109
- } ) ;
110
-
111
- // Update notification events
112
- self . addEventListener ( 'message' , ( event ) => {
113
- if ( event . data && event . data . type === 'SKIP_WAITING' ) {
114
- console . log ( "Skip waiting was called in the service worker." ) ;
115
- self . skipWaiting ( ) ;
116
- }
117
- } ) ;
118
69
119
- self . addEventListener ( "install" , ( event ) => {
120
- console . log ( "Service Worker install:" , event )
121
- } ) ;
70
+ // Workbox Precaching
71
+ // precacheAndRoute(self.__WB_MANIFEST || []);
72
+
73
+ // Assuming your sl-icon icons are served from a specific path, e.g., '/assets/icons/' or a CDN
74
+ registerRoute (
75
+ ( { url
} ) => url . pathname . startsWith ( '/assets/icons/' ) || url . host . includes ( 'https://cdn.jsdelivr.net/npm/@fortawesome/[email protected] ' ) , // Adjust the URL pattern
76
+ new CacheFirst ( {
77
+ cacheName : 'sl-icon-cache' ,
78
+ plugins : [
79
+ new workbox . cacheableResponse . CacheableResponsePlugin ( {
80
+ statuses : [ 0 , 200 ] , // Cache successful responses and opaque responses (for cross-origin)
81
+ } ) ,
82
+ new workbox . expiration . ExpirationPlugin ( {
83
+ maxAgeSeconds : 30 * 24 * 60 * 60 , // Cache for 30 days (adjust as needed)
84
+ maxEntries : 50 , // Keep a maximum of 50 icon files (adjust as needed)
85
+ } ) ,
86
+ ] ,
87
+ } )
88
+ ) ;
89
+
90
+ // Cache data
91
+ registerRoute (
92
+ ( { url } ) => url . pathname . startsWith ( '/data' ) ,
93
+ new CacheFirst ( )
94
+ ) ;
95
+
96
+ // Cache app images
97
+ registerRoute (
98
+ ( { url } ) => url . pathname . startsWith ( '/assets' ) ,
99
+ new CacheFirst ( )
100
+ ) ;
101
+
102
+ // Cache app CSS and JS files
103
+ registerRoute (
104
+ ( { request } ) =>
105
+ request . destination === 'script' ||
106
+ request . destination === 'style' ||
107
+ request . destination === 'module' , // Cache dynamically imported modules
108
+ new StaleWhileRevalidate ( )
109
+ ) ;
110
+
111
+ // Navigation Routing
112
+ registerRoute (
113
+ ( { request } ) => request . mode === 'navigate' ,
114
+ new NetworkFirst ( )
115
+ ) ;
116
+
117
+ self . addEventListener ( 'push' , function ( event ) {
118
+ console . log ( '[Service Worker] Push Received.' ) ;
119
+ if ( Notification . permission === "granted" ) {
120
+ const data = event . data . json ( ) ;
121
+ event . waitUntil (
122
+ self . registration . showNotification ( data . title , {
123
+ body : data . body ,
124
+ icon : '/assets/icons/icon_192.png' , // Replace with your icon
125
+ tag : 'my-tag' // Optional: prevents multiple notifications with the same tag
126
+ } )
127
+ ) ;
128
+ } else {
129
+ console . log ( "Notification permission has been denied" ) ;
130
+ }
131
+ } ) ;
132
+
133
+ self . addEventListener ( 'notificationclick' , ( event ) => {
134
+ event . notification . close ( ) ;
135
+ var fullPath = self . location . origin + event . notification . data . path ;
136
+ clients . openWindow ( fullPath ) ;
137
+ } ) ;
138
+
139
+ // Update notification events
140
+ self . addEventListener ( 'message' , ( event ) => {
141
+ if ( event . data && event . data . type === 'SKIP_WAITING' ) {
142
+ console . log ( "Skip waiting was called in the service worker." ) ;
143
+ self . skipWaiting ( ) ;
144
+ }
145
+ } ) ;
146
+
147
+ self . addEventListener ( "install" , ( event ) => {
148
+ console . log ( "Service Worker install:" , event )
149
+ } ) ;
150
+
151
+ precacheAndRoute ( self . __WB_MANIFEST || [ ] ) ; // Ensure your manifest also includes icon files if they are part of your build
152
+
153
+ } else {
154
+ console . error ( 'Workbox failed to load from CDN.' ) ;
155
+ }
0 commit comments