src/service-worker/core.js
/**
 * Purges our cache of old files.
 *
 * @param {!Cache} cache
 * @param {!RtvVersion} version
 * @param {string} pathname
 * @param {?Array<!RtvVersion>} diversions
 * @return {!Promise<undefined>}
 */
Purges our cache of old files.
Purges our cache of old files .
function purge(cache, version, pathname, diversions) {
  return cache.keys().then(requests => {
    const downloadedEnv = rtvEnvironment(version);
    for (let i = 0; i < requests.length; i++) {
      const request = requests[i];
      const url = request.url;
      const cachedData = requestData(url);
      if (!cachedData) {
        continue;
      }
      if (version === cachedData.rtv) {
        continue;
      }
      const cachedEnv = rtvEnvironment(cachedData.rtv);
      const cachedIsProd = BASE_RTV_ENVIRONMENT === cachedEnv;
      if (cachedIsProd) {
        if (BASE_RTV_ENVIRONMENT !== downloadedEnv) {
          continue;
        }
        if (pathname !== cachedData.pathname) {
          continue;
        }
      } else {
        if (!diversions || diversions.includes(cachedData.rtv)) {
          continue;
        }
      }
      cache.delete(request);
    }
  });
}
function purge ( cache , version , pathname , diversions ) { return cache . keys ( ) . then ( requests => { const downloadedEnv = rtvEnvironment ( version ) ; for ( let i = 0 ; i < requests . length ; i ++ ) { const request = requests [ i ] ; const url = request . url ; const cachedData = requestData ( url ) ; if ( ! cachedData ) { continue ; } if ( version === cachedData . rtv ) { continue ; } const cachedEnv = rtvEnvironment ( cachedData . rtv ) ; const cachedIsProd = BASE_RTV_ENVIRONMENT === cachedEnv ; if ( cachedIsProd ) { if ( BASE_RTV_ENVIRONMENT !== downloadedEnv ) { continue ; } if ( pathname !== cachedData . pathname ) { continue ; } } else { if ( ! diversions || diversions . includes ( cachedData . rtv ) ) { continue ; } } cache . delete ( request ) ; } } ) ; }
Purges our cache of old files.
Purges our cache of old files .
// We never delete files that match the version we just downloaded. // We prune production environments based on the downloaded version. // But, if we downloaded a diversion, we have no information on what // the current production version is. So, don't delete the production // script. // We only purge the old version of the newly downloaded file. // This is because we might request this particular other script later // on in this request, and will purge it then. // We will only delete a diversion if we know for certain the versions // that are diversions. // At this point, we know the cached file is either: // - An old production env of the newly downloaded script. // - An old diversion. // Importantly, it CANNOT be one of the following: // - The same version as the newly fetched script (This is the current //   production version or a current diversion). // - Any production version when we downloaded a diversion. // - Any production version of any other script. // - A current diversion, or a suspected diversion.