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

revert gatsby-plugin-offline and update getResourceURLsForPathname #14385

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 17 additions & 5 deletions packages/gatsby-plugin-offline/src/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
exports.registerServiceWorker = () => true

const prefetchedResources = []
const prefetchedPathnames = []
const whitelistedPathnames = []

exports.onServiceWorkerActive = ({ serviceWorker }) => {
exports.onServiceWorkerActive = ({
getResourceURLsForPathname,
serviceWorker,
}) => {
// if the SW has just updated then reset whitelisted paths and don't cache
// stuff, since we're on the old revision until we navigate to another page
if (window.___swUpdated) {
Expand All @@ -23,6 +26,15 @@ exports.onServiceWorkerActive = ({ serviceWorker }) => {
.call(nodes)
.map(node => node.src || node.href || node.getAttribute(`data-href`))

// Loop over all resources and fetch the page component and JSON
// to add it to the sw cache.
const prefetchedResources = []
prefetchedPathnames.forEach(path =>
getResourceURLsForPathname(path).forEach(resource =>
prefetchedResources.push(resource)
)
)

const resources = [...headerResources, ...prefetchedResources]
resources.forEach(resource => {
// Create a prefetch link for each resource, so Workbox runtime-caches them
Expand Down Expand Up @@ -57,12 +69,12 @@ function whitelistPathname(pathname, includesPrefix) {
}
}

exports.onPostPrefetch = ({ path, resourceUrls }) => {
exports.onPostPrefetchPathname = ({ pathname }) => {
// do nothing if the SW has just updated, since we still have old pages in
// memory which we don't want to be whitelisted
if (window.___swUpdated) return

whitelistPathname(path, false)
whitelistPathname(pathname, false)

// if SW is not installed, we need to record any prefetches
// that happen so we can then add them to SW cache once installed
Expand All @@ -73,6 +85,6 @@ exports.onPostPrefetch = ({ path, resourceUrls }) => {
navigator.serviceWorker.controller.state === `activated`
)
) {
prefetchedResources.push(...resourceUrls)
prefetchedPathnames.push(pathname)
}
}
1 change: 0 additions & 1 deletion packages/gatsby/cache-dir/api-runner-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ exports.apiRunner = (api, args = {}, defaultReturn, argTransform) => {
args.getResourcesForPathnameSync = getResourcesForPathnameSync
// Deprecated April 2019. Use `loadPage` instead
args.getResourcesForPathname = getResourcesForPathname
// Deprecated April 2019. Use resources passed in `onPostPrefetch` instead
args.getResourceURLsForPathname = getResourceURLsForPathname
args.loadPage = loadPage
args.loadPageSync = loadPageSync
Expand Down
46 changes: 15 additions & 31 deletions packages/gatsby/cache-dir/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ let prefetchTriggered = {}
let prefetchCompleted = {}
let disableCorePrefetching = false

const onPostPrefetch = ({ path, resourceUrls }) => {
if (!prefetchCompleted[path]) {
apiRunner(`onPostPrefetch`, { path, resourceUrls })
prefetchCompleted[path] = true
const onPostPrefetchPathname = pathname => {
if (!prefetchCompleted[pathname]) {
apiRunner(`onPostPrefetchPathname`, { pathname })
prefetchCompleted[pathname] = true
}
}

Expand Down Expand Up @@ -250,11 +250,7 @@ const queue = {
const chunkName = pageData.componentChunkName
const componentUrls = createComponentUrls(chunkName)
return Promise.all(componentUrls.map(prefetchHelper)).then(() => {
const resourceUrls = [pageDataUrl].concat(componentUrls)
onPostPrefetch({
path: rawPath,
resourceUrls,
})
onPostPrefetchPathname(rawPath)
})
})
}
Expand Down Expand Up @@ -312,16 +308,9 @@ const queue = {
page: pageResources,
pageResources,
})

if (process.env.NODE_ENV === `production`) {
const pageDataUrl = createPageDataUrl(cleanAndFindPath(rawPath))
const componentUrls = createComponentUrls(
pageData.componentChunkName
)
const resourceUrls = [pageDataUrl].concat(componentUrls)
onPostPrefetch({
path: rawPath,
resourceUrls,
})
onPostPrefetchPathname(rawPath)
}

return pageResources
Expand All @@ -340,14 +329,14 @@ const queue = {

loadPageSync: rawPath => pathScriptsCache[cleanAndFindPath(rawPath)],

// Deprecated April 2019. Query results used to be in a separate
// file, but are now included in the page-data.json, which is
// already loaded into the browser by the time this function is
// called. Use the resource URLs passed in `onPostPrefetch` instead.
getResourceURLsForPathname: path => {
const pageData = queue.loadPageSync(path)
getResourceURLsForPathname: rawPath => {
const path = cleanAndFindPath(rawPath)
const pageData = pageDatas[path]
if (pageData) {
return createComponentUrls(pageData.componentChunkName)
return [
...createComponentUrls(pageData.componentChunkName),
createPageDataUrl(path),
]
} else {
return null
}
Expand Down Expand Up @@ -393,14 +382,9 @@ export const publicLoader = {
)
return queue.loadPageSync(rawPath)
},
getResourceURLsForPathname: pathname => {
console.warn(
`Warning: getResourceURLsForPathname is deprecated. Use onPostPrefetch instead`
)
return queue.getResourceURLsForPathname
},

// Real methods
getResourceURLsForPathname: queue.getResourceURLsForPathname,
loadPage: queue.loadPage,
loadPageSync: queue.loadPageSync,
}
Expand Down
5 changes: 2 additions & 3 deletions packages/gatsby/src/utils/api-browser-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,10 @@ exports.onPrefetchPathname = true
* Called when prefetching for a pathname is successful. Allows
* for plugins with custom prefetching logic.
* @param {object} $0
* @param {string} $0.path The pathname whose resources have now been prefetched
* @param {resourceUrls} $0.resourceUrls An array of resource URLs (page data and component) that have been prefetched for this path
* @param {string} $0.pathname The pathname whose resources have now been prefetched
* @param {pluginOptions} pluginOptions
*/
exports.onPostPrefetch = true
exports.onPostPrefetchPathname = true

/**
* Plugins can take over prefetching logic. If they do, they should call this
Expand Down