src/service-worker/core.js
/**
 * Handles fetching the request from Cache, or fetching and caching from the
 * Cache CDN, if we care about the request.
 * My assumptions:
 *   - Doc requests one uniform AMP release version for all files, anything
 *     else is malarkey.
 *   - The requested version is always the newest AMP version.
 *
 * @param {!Request} request
 * @param {string|undefined} maybeClientId
 * @return {?Promise<!Response>}
 * @visibleForTesting
 */
Handles fetching the request from Cache, or fetching and caching from the
Cache CDN, if we care about the request.
My assumptions:
- Doc requests one uniform AMP release version for all files, anything
else is malarkey.
- The requested version is always the newest AMP version.

@param {!Request} request
@param {string|undefined} maybeClientId
@return {?Promise}
@visibleForTesting
Handles fetching the request from Cache or fetching and caching from the Cache CDN if we care about the request . My assumptions : - Doc requests one uniform AMP release version for all files anything else is malarkey . - The requested version is always the newest AMP version . @param { !Request } request @param { string|undefined } maybeClientId @return { ?Promise } @visibleForTesting
function handleFetch(request, maybeClientId) {
  const url = request.url;
  const data = requestData(url);
  if (!maybeClientId || !data) {
    return null;
  }
  const clientId = (maybeClientId);
  const {pathname, rtv} = data;
  request = normalizedRequest(request, rtv);
  return  (cachePromise.then(() => {
    if (clientsVersion[clientId]) {
      return clientsVersion[clientId];
    }
    return clientsVersion[clientId] = getCachedVersion(cache, rtv, pathname);
  }).then(version => {
    const versionedRequest = normalizedRequest(request, version);
    return cache.match(versionedRequest).then(response => {
      if (response) {
        if (version !== rtv && rtv == BASE_RTV_VERSION) {
          fetchJsFile(cache, request, rtv, pathname);
        }
        return response;
      }
      return fetchJsFile(cache, versionedRequest, version, pathname);
    });
  }).catch(err => {
    Promise.reject(err);
    throw err;
  }));
}
function handleFetch ( request , maybeClientId ) { const url = request . url ; const data = requestData ( url ) ; if ( ! maybeClientId || ! data ) { return null ; } const clientId = ( maybeClientId ) ; const { pathname , rtv } = data ; request = normalizedRequest ( request , rtv ) ; return ( cachePromise . then ( ( ) => { if ( clientsVersion [ clientId ] ) { return clientsVersion [ clientId ] ; } return clientsVersion [ clientId ] = getCachedVersion ( cache , rtv , pathname ) ; } ) . then ( version => { const versionedRequest = normalizedRequest ( request , version ) ; return cache . match ( versionedRequest ) . then ( response => { if ( response ) { if ( version !== rtv && rtv == BASE_RTV_VERSION ) { fetchJsFile ( cache , request , rtv , pathname ) ; } return response ; } return fetchJsFile ( cache , versionedRequest , version , pathname ) ; } ) ; } ) . catch ( err => { Promise . reject ( err ) ; throw err ; } ) ) ; }
Handles fetching the request from Cache, or fetching and caching from the
 Cache CDN, if we care about the request.
Handles fetching the request from Cache or fetching and caching from the Cache CDN if we care about the request .
// We only cache CDN JS files, and we need a clientId to do our magic. // Closure Compiler! /** @type {string} */ // Rewrite unversioned requests to the versioned RTV URL. This is a noop if // it's already versioned. // Wait for the cachePromise to resolve. This is necessary // since the SW thread may be killed and restarted at any time. /** @type {!Promise<!Response>} */ // If we already registered this client, we must always use the same // version. // If not, let's find the version to serve up. // Cache hit! // Now, was it because we served an old cached version or because // they requested this exact version; If we served an old version, // let's get the new one. // If not, let's fetch and cache the request. // Throw error out of band.