22/// <reference lib="esnext" />
33/// <reference lib="webworker" />
44import { defaultCache } from "@serwist/turbopack/worker" ;
5- import { type PrecacheEntry , Serwist , type SerwistGlobalConfig } from "serwist" ;
5+ import {
6+ type PrecacheEntry ,
7+ Serwist ,
8+ type SerwistGlobalConfig ,
9+ type SerwistPlugin ,
10+ } from "serwist" ;
11+ import {
12+ ADDITIONAL_PRECACHE_PROGRESS_MESSAGE ,
13+ type AdditionalPrecacheProgressMessage ,
14+ type AdditionalPrecacheProgressPayload ,
15+ isGetAdditionalPrecacheProgressMessage ,
16+ } from "@/lib/serwist/additional-precache-progress" ;
617
718// This declares the value of `injectionPoint` to TypeScript.
819// `injectionPoint` is the string that will be replaced by the
@@ -15,6 +26,78 @@ declare global {
1526}
1627
1728declare const self : ServiceWorkerGlobalScope ;
29+ declare const __FINITO_ADDITIONAL_PRECACHE_URLS__ : string [ ] ;
30+
31+ const additionalPrecacheUrlSet = new Set (
32+ __FINITO_ADDITIONAL_PRECACHE_URLS__ . map (
33+ ( url ) => new URL ( url , self . location . origin ) . href ,
34+ ) ,
35+ ) ;
36+
37+ let completedAdditionalPrecacheUrls = new Set < string > ( ) ;
38+ let additionalPrecacheProgress : AdditionalPrecacheProgressPayload = {
39+ completed : 0 ,
40+ status : "idle" ,
41+ total : additionalPrecacheUrlSet . size ,
42+ } ;
43+
44+ const createAdditionalPrecacheProgressMessage =
45+ ( ) : AdditionalPrecacheProgressMessage => ( {
46+ type : ADDITIONAL_PRECACHE_PROGRESS_MESSAGE ,
47+ payload : additionalPrecacheProgress ,
48+ } ) ;
49+
50+ const broadcastAdditionalPrecacheProgress = async ( ) => {
51+ const clients = await self . clients . matchAll ( {
52+ type : "window" ,
53+ includeUncontrolled : true ,
54+ } ) ;
55+ const message = createAdditionalPrecacheProgressMessage ( ) ;
56+
57+ for ( const client of clients ) {
58+ client . postMessage ( message ) ;
59+ }
60+ } ;
61+
62+ const resetAdditionalPrecacheProgress = ( ) => {
63+ completedAdditionalPrecacheUrls = new Set ( ) ;
64+ additionalPrecacheProgress = {
65+ completed : 0 ,
66+ status : additionalPrecacheUrlSet . size > 0 ? "running" : "idle" ,
67+ total : additionalPrecacheUrlSet . size ,
68+ } ;
69+ } ;
70+
71+ const additionalPrecacheProgressPlugin : SerwistPlugin = {
72+ handlerDidComplete : async ( { event, error, request } ) => {
73+ if ( event . type !== "install" ) return ;
74+ if ( ! additionalPrecacheUrlSet . has ( request . url ) ) return ;
75+ if ( additionalPrecacheProgress . status === "error" ) return ;
76+
77+ if ( error ) {
78+ additionalPrecacheProgress = {
79+ ...additionalPrecacheProgress ,
80+ status : "error" ,
81+ } ;
82+ await broadcastAdditionalPrecacheProgress ( ) ;
83+ return ;
84+ }
85+
86+ if ( completedAdditionalPrecacheUrls . has ( request . url ) ) return ;
87+
88+ completedAdditionalPrecacheUrls . add ( request . url ) ;
89+ additionalPrecacheProgress = {
90+ completed : completedAdditionalPrecacheUrls . size ,
91+ status :
92+ completedAdditionalPrecacheUrls . size === additionalPrecacheUrlSet . size
93+ ? "complete"
94+ : "running" ,
95+ total : additionalPrecacheUrlSet . size ,
96+ } ;
97+
98+ await broadcastAdditionalPrecacheProgress ( ) ;
99+ } ,
100+ } ;
18101
19102const serwist = new Serwist ( {
20103 precacheEntries : self . __SW_MANIFEST ,
@@ -38,4 +121,23 @@ const serwist = new Serwist({
38121 } ,
39122} ) ;
40123
124+ serwist . precacheStrategy . plugins . push ( additionalPrecacheProgressPlugin ) ;
125+
126+ self . addEventListener ( "install" , ( event ) => {
127+ if ( additionalPrecacheUrlSet . size === 0 ) return ;
128+
129+ resetAdditionalPrecacheProgress ( ) ;
130+ event . waitUntil ( broadcastAdditionalPrecacheProgress ( ) ) ;
131+ } ) ;
132+
133+ self . addEventListener ( "message" , ( event ) => {
134+ if ( ! isGetAdditionalPrecacheProgressMessage ( event . data ) ) return ;
135+
136+ event . waitUntil (
137+ ( async ( ) => {
138+ event . ports [ 0 ] ?. postMessage ( createAdditionalPrecacheProgressMessage ( ) ) ;
139+ } ) ( ) ,
140+ ) ;
141+ } ) ;
142+
41143serwist . addEventListeners ( ) ;
0 commit comments