@@ -26,6 +26,11 @@ export abstract class AssetGroup {
2626 */
2727 private inFlightRequests = new Map < string , Promise < Response > > ( ) ;
2828
29+ /**
30+ * Normalized resource URLs.
31+ */
32+ protected urls : string [ ] = [ ] ;
33+
2934 /**
3035 * Regular expression patterns.
3136 */
@@ -52,29 +57,33 @@ export abstract class AssetGroup {
5257 protected idle : IdleScheduler , protected config : AssetGroupConfig ,
5358 protected hashes : Map < string , string > , protected db : Database , protected prefix : string ) {
5459 this . name = config . name ;
60+
61+ // Normalize the config's URLs to take the ServiceWorker's scope into account.
62+ this . urls = config . urls . map ( url => adapter . normalizeUrl ( url ) ) ;
63+
5564 // Patterns in the config are regular expressions disguised as strings. Breathe life into them.
56- this . patterns = this . config . patterns . map ( pattern => new RegExp ( pattern ) ) ;
65+ this . patterns = config . patterns . map ( pattern => new RegExp ( pattern ) ) ;
5766
5867 // This is the primary cache, which holds all of the cached requests for this group. If a
5968 // resource
6069 // isn't in this cache, it hasn't been fetched yet.
61- this . cache = this . scope . caches . open ( `${ this . prefix } :${ this . config . name } :cache` ) ;
70+ this . cache = scope . caches . open ( `${ this . prefix } :${ config . name } :cache` ) ;
6271
6372 // This is the metadata table, which holds specific information for each cached URL, such as
6473 // the timestamp of when it was added to the cache.
65- this . metadata =
66- this . db . open ( `${ this . prefix } :${ this . config . name } :meta` , this . config . cacheQueryOptions ) ;
74+ this . metadata = this . db . open ( `${ this . prefix } :${ config . name } :meta` , config . cacheQueryOptions ) ;
6775 }
6876
6977 async cacheStatus ( url : string ) : Promise < UpdateCacheStatus > {
7078 const cache = await this . cache ;
7179 const meta = await this . metadata ;
72- const res = await cache . match ( this . adapter . newRequest ( url ) , this . config . cacheQueryOptions ) ;
80+ const req = this . adapter . newRequest ( url ) ;
81+ const res = await cache . match ( req , this . config . cacheQueryOptions ) ;
7382 if ( res === undefined ) {
7483 return UpdateCacheStatus . NOT_CACHED ;
7584 }
7685 try {
77- const data = await meta . read < UrlMetadata > ( url ) ;
86+ const data = await meta . read < UrlMetadata > ( req . url ) ;
7887 if ( ! data . used ) {
7988 return UpdateCacheStatus . CACHED_BUT_UNUSED ;
8089 }
@@ -105,7 +114,7 @@ export abstract class AssetGroup {
105114 // Either the request matches one of the known resource URLs, one of the patterns for
106115 // dynamically matched URLs, or neither. Determine which is the case for this request in
107116 // order to decide how to handle it.
108- if ( this . config . urls . indexOf ( url ) !== - 1 || this . patterns . some ( pattern => pattern . test ( url ) ) ) {
117+ if ( this . urls . indexOf ( url ) !== - 1 || this . patterns . some ( pattern => pattern . test ( url ) ) ) {
109118 // This URL matches a known resource. Either it's been cached already or it's missing, in
110119 // which case it needs to be loaded from the network.
111120
@@ -235,7 +244,8 @@ export abstract class AssetGroup {
235244 const metaTable = await this . metadata ;
236245
237246 // Lookup the response in the cache.
238- const response = await cache . match ( this . adapter . newRequest ( url ) , this . config . cacheQueryOptions ) ;
247+ const request = this . adapter . newRequest ( url ) ;
248+ const response = await cache . match ( request , this . config . cacheQueryOptions ) ;
239249 if ( response === undefined ) {
240250 // It's not found, return null.
241251 return null ;
@@ -244,7 +254,7 @@ export abstract class AssetGroup {
244254 // Next, lookup the cached metadata.
245255 let metadata : UrlMetadata | undefined = undefined ;
246256 try {
247- metadata = await metaTable . read < UrlMetadata > ( url ) ;
257+ metadata = await metaTable . read < UrlMetadata > ( request . url ) ;
248258 } catch {
249259 // Do nothing, not found. This shouldn't happen, but it can be handled.
250260 }
@@ -258,9 +268,10 @@ export abstract class AssetGroup {
258268 */
259269 async unhashedResources ( ) : Promise < string [ ] > {
260270 const cache = await this . cache ;
261- // Start with the set of all cached URLs .
271+ // Start with the set of all cached requests .
262272 return ( await cache . keys ( ) )
263- . map ( request => request . url )
273+ // Normalize their URLs.
274+ . map ( request => this . adapter . normalizeUrl ( request . url ) )
264275 // Exclude the URLs which have hashes.
265276 . filter ( url => ! this . hashes . has ( url ) ) ;
266277 }
@@ -307,7 +318,7 @@ export abstract class AssetGroup {
307318
308319 // If the request is not hashed, update its metadata, especially the timestamp. This is
309320 // needed for future determination of whether this cached response is stale or not.
310- if ( ! this . hashes . has ( req . url ) ) {
321+ if ( ! this . hashes . has ( this . adapter . normalizeUrl ( req . url ) ) ) {
311322 // Metadata is tracked for requests that are unhashed.
312323 const meta : UrlMetadata = { ts : this . adapter . time , used} ;
313324 const metaTable = await this . metadata ;
@@ -492,7 +503,7 @@ export class PrefetchAssetGroup extends AssetGroup {
492503 // Cache all known resources serially. As this reduce proceeds, each Promise waits
493504 // on the last before starting the fetch/cache operation for the next request. Any
494505 // errors cause fall-through to the final Promise which rejects.
495- await this . config . urls . reduce ( async ( previous : Promise < void > , url : string ) => {
506+ await this . urls . reduce ( async ( previous : Promise < void > , url : string ) => {
496507 // Wait on all previous operations to complete.
497508 await previous ;
498509
@@ -527,8 +538,8 @@ export class PrefetchAssetGroup extends AssetGroup {
527538 // First, narrow down the set of resources to those which are handled by this group.
528539 // Either it's a known URL, or it matches a given pattern.
529540 . filter (
530- url => this . config . urls . indexOf ( url ) !== - 1 ||
531- this . patterns . some ( pattern => pattern . test ( url ) ) )
541+ url =>
542+ this . urls . indexOf ( url ) !== - 1 || this . patterns . some ( pattern => pattern . test ( url ) ) )
532543 // Finally, process each resource in turn.
533544 . reduce ( async ( previous , url ) => {
534545 await previous ;
@@ -552,7 +563,7 @@ export class PrefetchAssetGroup extends AssetGroup {
552563 // Write it into the cache. It may already be expired, but it can still serve
553564 // traffic until it's updated (stale-while-revalidate approach).
554565 await cache . put ( req , res . response ) ;
555- await metaTable . write ( url , { ...res . metadata , used : false } as UrlMetadata ) ;
566+ await metaTable . write ( req . url , { ...res . metadata , used : false } as UrlMetadata ) ;
556567 } , Promise . resolve ( ) ) ;
557568 }
558569 }
@@ -570,7 +581,7 @@ export class LazyAssetGroup extends AssetGroup {
570581 const cache = await this . cache ;
571582
572583 // Loop through the listed resources, caching any which are available.
573- await this . config . urls . reduce ( async ( previous : Promise < void > , url : string ) => {
584+ await this . urls . reduce ( async ( previous : Promise < void > , url : string ) => {
574585 // Wait on all previous operations to complete.
575586 await previous ;
576587
0 commit comments