-
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 2 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 |
---|---|---|
|
@@ -21,13 +21,12 @@ exports.onServiceWorkerActive = ({ | |
// grab nodes from head of document | ||
const nodes = document.querySelectorAll(` | ||
head > script[src], | ||
head > link[as=script], | ||
head > link[rel=stylesheet], | ||
head > link[href], | ||
head > style[data-href] | ||
`) | ||
|
||
// get all resource URLs | ||
const resources = [].slice | ||
const headerResources = [].slice | ||
.call(nodes) | ||
.map(node => node.src || node.href || node.getAttribute(`data-href`)) | ||
|
||
|
@@ -40,8 +39,14 @@ exports.onServiceWorkerActive = ({ | |
) | ||
) | ||
|
||
serviceWorker.active.postMessage({ | ||
api: `gatsby-runtime-cache`, | ||
resources: [...resources, ...prefetchedResources], | ||
const resources = [...headerResources, ...prefetchedResources] | ||
resources.forEach(resource => { | ||
// Create a prefetch link for each resource, so Workbox runtime-caches them | ||
const link = document.createElement(`link`) | ||
link.rel = `prefetch` | ||
link.href = resource | ||
|
||
document.head.appendChild(link) | ||
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. Should the order of these (append vs. onload) be swapped? I'd think we'd want to register the onload/remove behavior before adding it. If nothing else, seems to be more intuitive that way, even if both work. 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. These can be reordered if you like - although it should work fine as-is, since the browser doesn't start fetching until it's appended. Would you like me to swap the order? 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. Sure, just for clarity! More of a nit than substantive, but I think it's a bit clearer. |
||
link.onload = link.remove | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,13 +96,13 @@ exports.onPostBuild = (args, pluginOptions) => { | |
runtimeCaching: [ | ||
{ | ||
// Add runtime caching of various page resources. | ||
urlPattern: /\.(?:png|jpg|jpeg|webp|svg|gif|tiff|js|woff|woff2|json|css)$/, | ||
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.