-
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
[offline-plugin] Add support for SW caching of prefetched resources #6566
Changes from 3 commits
0d8eabe
4fe78a0
399b435
3de5990
4f4389f
c50ff4c
9324b6b
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
/gatsby-ssr.js | ||
/gatsby-browser.js | ||
/app-shell.js | ||
/prefetch-catcher.js |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,40 @@ | ||
exports.registerServiceWorker = () => true | ||
|
||
let swNotInstalled = true | ||
const pathnameResources = [] | ||
|
||
exports.onPrefetchPathname = ({ pathname, getResourcesForPathname }) => { | ||
if (swNotInstalled && `serviceWorker` in navigator) { | ||
pathnameResources.push( | ||
new Promise(resolve => { | ||
getResourcesForPathname(pathname, resources => { | ||
resolve(resources) | ||
}) | ||
}) | ||
) | ||
} | ||
} | ||
|
||
exports.onServiceWorkerInstalled = () => { | ||
swNotInstalled = false | ||
|
||
// grab nodes from head of document | ||
const nodes = document.querySelectorAll( | ||
`head > script[src], head > link[as=script]` | ||
) | ||
|
||
// get all script URLs | ||
const scripts = [].slice | ||
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. ol' skool dom hacking 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. 😎 |
||
.call(nodes) | ||
.map(node => (node.src ? node.src : node.href)) | ||
|
||
Promise.all(pathnameResources).then(pageResources => { | ||
pageResources.forEach(pageResource => { | ||
const [script] = scripts.filter(s => | ||
s.includes(pageResource.page.componentChunkName) | ||
) | ||
fetch(pageResource.page.jsonURL) | ||
fetch(script) | ||
}) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import emitter from "./emitter" | ||
import { apiRunner } from "./api-runner-browser" | ||
|
||
if (`serviceWorker` in navigator) { | ||
navigator.serviceWorker | ||
.register(`${__PATH_PREFIX__}/sw.js`) | ||
.then(function(reg) { | ||
reg.addEventListener(`updatefound`, () => { | ||
apiRunner(`onServiceWorkerUpdateFound`) | ||
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. let's pass in |
||
// The updatefound event implies that reg.installing is set; see | ||
// https://w3c.github.io/ServiceWorker/#service-worker-registration-updatefound-event | ||
const installingWorker = reg.installing | ||
|
@@ -21,12 +22,19 @@ if (`serviceWorker` in navigator) { | |
// At this point, everything has been precached. | ||
// It's the perfect time to display a "Content is cached for offline use." message. | ||
console.log(`Content is now available offline!`) | ||
emitter.emit(`sw:installed`) | ||
|
||
// post to service worker that install is complete | ||
apiRunner(`onServiceWorkerInstalled`) | ||
} | ||
break | ||
|
||
case `redundant`: | ||
console.error(`The installing service worker became redundant.`) | ||
apiRunner(`onServiceWorkerRedundant`) | ||
break | ||
|
||
case `active`: | ||
apiRunner(`onServiceWorkerActive`) | ||
break | ||
} | ||
}) | ||
|
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.
This could use a comment to explain that here we record resources that are prefetched before the sw has been installed and can cache things directly.