Skip to content

Commit d9f973b

Browse files
committed
revert plugin-offline to master (gatsbyjs#14385)
1 parent a717317 commit d9f973b

File tree

4 files changed

+34
-40
lines changed

4 files changed

+34
-40
lines changed

packages/gatsby-plugin-offline/src/gatsby-browser.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
exports.registerServiceWorker = () => true
22

3-
const prefetchedResources = []
3+
const prefetchedPathnames = []
44
const whitelistedPathnames = []
55

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

29+
// Loop over all resources and fetch the page component and JSON
30+
// to add it to the sw cache.
31+
const prefetchedResources = []
32+
prefetchedPathnames.forEach(path =>
33+
getResourceURLsForPathname(path).forEach(resource =>
34+
prefetchedResources.push(resource)
35+
)
36+
)
37+
2638
const resources = [...headerResources, ...prefetchedResources]
2739
resources.forEach(resource => {
2840
// Create a prefetch link for each resource, so Workbox runtime-caches them
@@ -57,12 +69,12 @@ function whitelistPathname(pathname, includesPrefix) {
5769
}
5870
}
5971

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

65-
whitelistPathname(path, false)
77+
whitelistPathname(pathname, false)
6678

6779
// if SW is not installed, we need to record any prefetches
6880
// that happen so we can then add them to SW cache once installed
@@ -73,6 +85,6 @@ exports.onPostPrefetch = ({ path, resourceUrls }) => {
7385
navigator.serviceWorker.controller.state === `activated`
7486
)
7587
) {
76-
prefetchedResources.push(...resourceUrls)
88+
prefetchedPathnames.push(pathname)
7789
}
7890
}

packages/gatsby/cache-dir/api-runner-browser.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ exports.apiRunner = (api, args = {}, defaultReturn, argTransform) => {
2828
args.getResourcesForPathnameSync = getResourcesForPathnameSync
2929
// Deprecated April 2019. Use `loadPage` instead
3030
args.getResourcesForPathname = getResourcesForPathname
31-
// Deprecated April 2019. Use resources passed in `onPostPrefetch` instead
3231
args.getResourceURLsForPathname = getResourceURLsForPathname
3332
args.loadPage = loadPage
3433
args.loadPageSync = loadPageSync

packages/gatsby/cache-dir/loader.js

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ let prefetchTriggered = {}
173173
let prefetchCompleted = {}
174174
let disableCorePrefetching = false
175175

176-
const onPostPrefetch = ({ path, resourceUrls }) => {
177-
if (!prefetchCompleted[path]) {
178-
apiRunner(`onPostPrefetch`, { path, resourceUrls })
179-
prefetchCompleted[path] = true
176+
const onPostPrefetchPathname = pathname => {
177+
if (!prefetchCompleted[pathname]) {
178+
apiRunner(`onPostPrefetchPathname`, { pathname })
179+
prefetchCompleted[pathname] = true
180180
}
181181
}
182182

@@ -250,11 +250,7 @@ const queue = {
250250
const chunkName = pageData.componentChunkName
251251
const componentUrls = createComponentUrls(chunkName)
252252
return Promise.all(componentUrls.map(prefetchHelper)).then(() => {
253-
const resourceUrls = [pageDataUrl].concat(componentUrls)
254-
onPostPrefetch({
255-
path: rawPath,
256-
resourceUrls,
257-
})
253+
onPostPrefetchPathname(rawPath)
258254
})
259255
})
260256
}
@@ -312,16 +308,9 @@ const queue = {
312308
page: pageResources,
313309
pageResources,
314310
})
311+
315312
if (process.env.NODE_ENV === `production`) {
316-
const pageDataUrl = createPageDataUrl(cleanAndFindPath(rawPath))
317-
const componentUrls = createComponentUrls(
318-
pageData.componentChunkName
319-
)
320-
const resourceUrls = [pageDataUrl].concat(componentUrls)
321-
onPostPrefetch({
322-
path: rawPath,
323-
resourceUrls,
324-
})
313+
onPostPrefetchPathname(rawPath)
325314
}
326315

327316
return pageResources
@@ -340,14 +329,14 @@ const queue = {
340329

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

343-
// Deprecated April 2019. Query results used to be in a separate
344-
// file, but are now included in the page-data.json, which is
345-
// already loaded into the browser by the time this function is
346-
// called. Use the resource URLs passed in `onPostPrefetch` instead.
347-
getResourceURLsForPathname: path => {
348-
const pageData = queue.loadPageSync(path)
332+
getResourceURLsForPathname: rawPath => {
333+
const path = cleanAndFindPath(rawPath)
334+
const pageData = pageDatas[path]
349335
if (pageData) {
350-
return createComponentUrls(pageData.componentChunkName)
336+
return [
337+
...createComponentUrls(pageData.componentChunkName),
338+
createPageDataUrl(path),
339+
]
351340
} else {
352341
return null
353342
}
@@ -393,14 +382,9 @@ export const publicLoader = {
393382
)
394383
return queue.loadPageSync(rawPath)
395384
},
396-
getResourceURLsForPathname: pathname => {
397-
console.warn(
398-
`Warning: getResourceURLsForPathname is deprecated. Use onPostPrefetch instead`
399-
)
400-
return queue.getResourceURLsForPathname
401-
},
402385

403386
// Real methods
387+
getResourceURLsForPathname: queue.getResourceURLsForPathname,
404388
loadPage: queue.loadPage,
405389
loadPageSync: queue.loadPageSync,
406390
}

packages/gatsby/src/utils/api-browser-docs.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,10 @@ exports.onPrefetchPathname = true
198198
* Called when prefetching for a pathname is successful. Allows
199199
* for plugins with custom prefetching logic.
200200
* @param {object} $0
201-
* @param {string} $0.path The pathname whose resources have now been prefetched
202-
* @param {resourceUrls} $0.resourceUrls An array of resource URLs (page data and component) that have been prefetched for this path
201+
* @param {string} $0.pathname The pathname whose resources have now been prefetched
203202
* @param {pluginOptions} pluginOptions
204203
*/
205-
exports.onPostPrefetch = true
204+
exports.onPostPrefetchPathname = true
206205

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

0 commit comments

Comments
 (0)