Skip to content

Commit

Permalink
Use no-cors mode for all service worker resources
Browse files Browse the repository at this point in the history
Some of the resources we load from other origins must be loaded as
opaque resources that can't be read by the application.  In fact, it
turns out that there are no app resources that need to be read by the
app.  So we should load everything in no-cors mode as opaque
resources.  This fixes failures loading and caching MDL resources.

Change-Id: I25ec4228285e3149746d906e6461ac04aeedc7e1
  • Loading branch information
joeyparrish committed May 1, 2019
1 parent e8d95a4 commit d7e2501
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions demo/service_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,24 @@ const CACHEABLE_URL_PREFIXES = [
function onInstall(event) {
const preCacheApplication = async () => {
const cache = await caches.open(CACHE_NAME);
// Fetching these with addAll fails for CORS-restricted content, so we use
// fetchAndCache with no-cors mode to work around it.

// Optional resources: failure on these will NOT fail the Promise chain.
// We will also not wait for them to be installed.
// Fetching these with addAll fails for CORS-restricted content, so we use
// fetchAndCache with no-cors mode to work around it.
for (const url of OPTIONAL_RESOURCES) {
const request = new Request(url, {mode: 'no-cors'});
fetchAndCache(cache, request).catch(() => {});
}

// Critical resources: failure on these will fail the Promise chain.
// The installation will not be complete until these are all cached.
return cache.addAll(CRITICAL_RESOURCES);
const criticalFetches = [];
for (const url of CRITICAL_RESOURCES) {
const request = new Request(url, {mode: 'no-cors'});
criticalFetches.push(fetchAndCache(cache, request));
}
return Promise.all(criticalFetches);
};

event.waitUntil(preCacheApplication());
Expand Down

0 comments on commit d7e2501

Please sign in to comment.