-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(gatsby-plugin-offline): fix certain resources being cached excessively #9923
Changes from all commits
23599eb
e02f9d5
fdd5844
14cdb95
ff6ca9b
b25b07b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,18 +91,25 @@ exports.onPostBuild = (args, pluginOptions) => { | |
navigateFallbackWhitelist: [/^([^.?]*|[^?]*\.([^.?]{5,}|html))(\?.*)?$/], | ||
navigateFallbackBlacklist: [/\?(.+&)?no-cache=1$/], | ||
cacheId: `gatsby-plugin-offline`, | ||
// Don't cache-bust JS or CSS files, and anything in the static directory | ||
dontCacheBustUrlsMatching: /(.*\.js$|.*\.css$|\/static\/)/, | ||
// Don't cache-bust JS or CSS files, and anything in the static directory, | ||
// since these files have unique URLs and their contents will never change | ||
dontCacheBustUrlsMatching: /(\.js$|\.css$|\/static\/)/, | ||
runtimeCaching: [ | ||
{ | ||
// Add runtime caching of various page resources. | ||
urlPattern: /\.(?:png|jpg|jpeg|webp|svg|gif|tiff|js|woff|woff2|json|css)$/, | ||
// Use cacheFirst since these don't need to be revalidated (same RegExp | ||
// and same reason as above) | ||
urlPattern: /(\.js$|\.css$|\/static\/)/, | ||
handler: `cacheFirst`, | ||
}, | ||
{ | ||
// Add runtime caching of various other page resources | ||
urlPattern: /^https?:.*\.(png|jpg|jpeg|webp|svg|gif|tiff|js|woff|woff2|json|css)$/, | ||
handler: `staleWhileRevalidate`, | ||
}, | ||
{ | ||
// Use the Network First handler for external resources | ||
urlPattern: /^https?:/, | ||
handler: `networkFirst`, | ||
// Google Fonts CSS (doesn't end in .css so we need to specify it) | ||
urlPattern: /^https?:\/\/fonts\.googleapis\.com\/css/, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe just use a regex like in this recipe? https://gist.github.com/addyosmani/0e1cfeeccad94edc2f0985a15adefe54#cache-all-google-fonts-requests-up-to-a-maximum-of-30-cache-entries Also from what I understand, staleWhileRevalidate will request both cache/network each time, and then update the cache on success. It seems reasonable that something like google fonts can hit the cache first, then the network if that fails? What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Also, since the linked code calls Workbox APIs directly, we'd have to move it to |
||
handler: `staleWhileRevalidate`, | ||
}, | ||
], | ||
skipWaiting: true, | ||
|
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 | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're no longer using this, do we even need this file at all? Additionally, we probably can remove the appending stuff, as well.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought you would ask that question - I decided to leave it because I've added some new APIs to the same file in #9907, so there doesn't seem to be much point in removing it if we're only going to add it back again later. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it possible that we're duplicating a prefetch here? i.e.
head > link[href]
could matchand we'd create another prefetch link. Is that OK?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is expected behaviour - we need to "prefetch" after the SW has activated, so that Workbox can cache the responses.