forked from gatsbyjs/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby-plugin-offline): fix certain resources being cached excess…
…ively (gatsbyjs#9923) Fixes gatsbyjs#9415 This PR leverages Workbox's automatic caching when a resource is fetched, by creating a `<link>` prefetch element for each resource. This means we don't have to worry about whether the resource can be fetched with CORS or not, since the browser handles it automatically. I've also changed the runtime caching RegExps so that only paths with certain extensions are cached, but 3rd party resources with these extensions can now be cached.
- Loading branch information
Showing
4 changed files
with
42 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1 @@ | ||
/* global workbox */ | ||
|
||
self.addEventListener(`message`, event => { | ||
const { api } = event.data | ||
if (api === `gatsby-runtime-cache`) { | ||
const { resources } = event.data | ||
const cacheName = workbox.core.cacheNames.runtime | ||
|
||
event.waitUntil( | ||
caches.open(cacheName).then(cache => | ||
Promise.all( | ||
resources.map(resource => { | ||
let request | ||
|
||
// Some external resources don't allow | ||
// CORS so get an opaque response | ||
if (resource.match(/^https?:/)) { | ||
request = fetch(resource, { mode: `no-cors` }) | ||
} else { | ||
request = fetch(resource) | ||
} | ||
|
||
return request.then(response => cache.put(resource, response)) | ||
}) | ||
) | ||
) | ||
) | ||
} | ||
}) | ||
// noop |