Skip to content
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

feat(gatsby-plugin-offline): replace no-cache detection with dynamic path whitelist #9907

Merged
merged 26 commits into from
Nov 20, 2018
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6f53bb1
Remove all no-cache code
Nov 5, 2018
41a932a
Remove references to no-cache in offline plugin
Nov 5, 2018
a191ffe
Merge upstream/master into origin/master
Nov 12, 2018
a1cecf4
Initial work on hybrid navigation handler
Nov 12, 2018
1807752
Refactor whitelist code to allow it to support onPostPrefetchPathname
Nov 12, 2018
8f3cd4b
Fix service worker detection
Nov 12, 2018
8d826c6
Fix IndexedDB race condition
Nov 12, 2018
e8224ea
Prevent race conditions + reset whitelist on SW update
Nov 12, 2018
c60bb5c
Remove unnecessary API handler (onPostPrefetchPathname is called anyway)
Nov 12, 2018
465a4d2
Add debugging statements + fix some minor problems
Nov 13, 2018
c62c378
Fix back/forward not working after 404
Nov 13, 2018
4d47124
Remove unneeded debugging statements
Nov 13, 2018
b233cf6
Bundle idb-keyval instead of using an external CDN
Nov 13, 2018
7c4a2b6
Update README
Nov 13, 2018
0923c67
Merge upstream/master into origin/hybrid-offline-shell
Nov 13, 2018
25e4ecf
Fix excessive file caching (e.g. GA tracking gif)
Nov 14, 2018
23599eb
Backport fixes from #9907
Nov 14, 2018
e02f9d5
minor fixes for things I copy-pasted wrong
Nov 14, 2018
ffc5868
Merge origin/backport-9415-fix into origin/hybrid-offline-shell
Nov 14, 2018
fbeee6d
Merge upstream/master into origin/hybrid-offline-shell
Nov 14, 2018
0d0b001
Fetch resources the same way in enqueue to getResourcesForPathname
Nov 15, 2018
b9857c6
Revert "Fetch resources the same way in enqueue to getResourcesForPat…
Nov 15, 2018
6d6762c
Refactor prefetching so we can detect success
Nov 15, 2018
b7f8b83
Move catch to prevent onPostPrefetchPathname after failure
Nov 15, 2018
37034bc
Revert "Move catch to prevent onPostPrefetchPathname after failure"
Nov 15, 2018
0dcface
Merge upstream/master into hybrid-offline-shell
Nov 15, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Backport fixes from #9907
  • Loading branch information
David Bailey committed Nov 14, 2018

Verified

This commit was signed with the committer’s verified signature.
kianenigma Kian Paimani
commit 23599eb9cc22330cb71ef9536540fb8162b4d2bd
16 changes: 10 additions & 6 deletions packages/gatsby-plugin-offline/src/gatsby-browser.js
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,13 @@ 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)
link.onload = link.remove
})
}
7 changes: 3 additions & 4 deletions packages/gatsby-plugin-offline/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -96,13 +96,12 @@ 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/,
},
],
skipWaiting: true,
30 changes: 1 addition & 29 deletions packages/gatsby-plugin-offline/src/sw-append.js
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