Skip to content

Commit

Permalink
Add debugging statements + fix some minor problems
Browse files Browse the repository at this point in the history
  • Loading branch information
David Bailey committed Nov 13, 2018
1 parent c60bb5c commit 465a4d2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 30 deletions.
5 changes: 4 additions & 1 deletion packages/gatsby/cache-dir/ensure-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,14 @@ class EnsureResources extends React.Component {
}

render() {
// This should only occur if there's no custom 404 page
// This should only occur if the network is offline, or if the
// path is nonexistent and there's no custom 404 page.
if (
process.env.NODE_ENV === `production` &&
!(this.state.pageResources && this.state.pageResources.json)
) {
console.error(`Failed to get resources for ${location.pathname}`)
window.location.reload()
return null
}

Expand Down
52 changes: 32 additions & 20 deletions packages/gatsby/cache-dir/loader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pageFinderFactory from "./find-page"
import emitter from "./emitter"
import stripPrefix from "./strip-prefix"
import prefetchHelper from "./prefetch"

const preferDefault = m => (m && m.default) || m
Expand All @@ -16,7 +15,6 @@ let fetchingPageResourceMapPromise = null
let fetchedPageResourceMap = false
let apiRunner
const failedPaths = {}
const failedResources = {}
const MAX_HISTORY = 5

const jsonPromiseStore = {}
Expand Down Expand Up @@ -53,25 +51,38 @@ const createComponentUrls = componentChunkName =>
)

const fetchResource = resourceName => {
console.log(`fetchResource ${resourceName}`)
// Find resource
let resourceFunction
if (resourceName.slice(0, 12) === `component---`) {
console.log(
`resourceFunction: resolving ${resourceName} with asyncRequires.components:`
)
console.log(asyncRequires.components)
resourceFunction = asyncRequires.components[resourceName]
} else {
if (resourceName in jsonPromiseStore) {
console.log(
`resourceFunction: found ${resourceName} in jsonPromiseStore:`
)
console.log(jsonPromiseStore)
resourceFunction = () => jsonPromiseStore[resourceName]
} else {
resourceFunction = () => {
const fetchPromise = new Promise((resolve, reject) => {
const url = createJsonURL(jsonDataPaths[resourceName])
console.log(`resourceFunction: fetching ${url} from network`)
const req = new XMLHttpRequest()
req.open(`GET`, url, true)
req.withCredentials = true
req.onreadystatechange = () => {
if (req.readyState == 4) {
if (req.status === 200) {
console.log(`resolving ${url}`)
resolve(JSON.parse(req.responseText))
} else {
console.log(`rejecting ${url} - didn't return 200`)
delete jsonPromiseStore[resourceName]
reject()
}
}
Expand All @@ -90,7 +101,9 @@ const fetchResource = resourceName => {
const fetchPromise = resourceFunction()
let failed = false
return fetchPromise
.catch(() => {
.catch(e => {
console.log(`fetchPromise for ${resourceName} failed:`)
console.error(e)
failed = true
})
.then(component => {
Expand All @@ -99,18 +112,17 @@ const fetchResource = resourceName => {
succeeded: !failed,
})

if (!failedResources[resourceName]) {
failedResources[resourceName] = failed
}

fetchHistory = fetchHistory.slice(-MAX_HISTORY)

console.log(`finally resolving fetchResource for ${resourceName} with:`)
console.log(component)
resolve(component)
})
})
}

const prefetchResource = resourceName => {
console.log(`prefetchResource ${resourceName}`)
if (resourceName.slice(0, 12) === `component---`) {
createComponentUrls(resourceName).forEach(url => prefetchHelper(url))
} else {
Expand All @@ -134,6 +146,7 @@ const appearsOnLine = () => {
}

const handleResourceLoadError = (path, message) => {
console.log(`handleResourceLoadError for path ${path}`)
if (!failedPaths[path]) {
failedPaths[path] = message
}
Expand All @@ -148,14 +161,14 @@ const handleResourceLoadError = (path, message) => {

const onPrefetchPathname = pathname => {
if (!prefetchTriggered[pathname]) {
apiRunner(`onPrefetchPathname`, { pathname: pathname })
apiRunner(`onPrefetchPathname`, { pathname })
prefetchTriggered[pathname] = true
}
}

const onPostPrefetchPathname = pathname => {
if (!prefetchCompleted[pathname]) {
apiRunner(`onPostPrefetchPathname`, { pathname: pathname })
apiRunner(`onPostPrefetchPathname`, { pathname })
prefetchCompleted[pathname] = true
}
}
Expand Down Expand Up @@ -188,12 +201,10 @@ const queue = {
// Hovering on a link is a very strong indication the user is going to
// click on it soon so let's start prefetching resources for this
// pathname.
hovering: rawPath => {
const path = stripPrefix(rawPath, __PATH_PREFIX__)
hovering: path => {
queue.getResourcesForPathname(path)
},
enqueue: rawPath => {
const path = stripPrefix(rawPath, __PATH_PREFIX__)
enqueue: path => {
if (!apiRunner)
console.error(`Run setApiRunnerForLoader() before enqueing paths`)

Expand All @@ -218,7 +229,7 @@ const queue = {
) {
// If page wasn't found check and we didn't fetch resources map for
// all pages, wait for fetch to complete and try find page again
return fetchPageResourceMap().then(() => queue.enqueue(rawPath))
return fetchPageResourceMap().then(() => queue.enqueue(path))
}

if (!page) {
Expand All @@ -238,9 +249,6 @@ const queue = {
prefetchResource(page.componentChunkName)
}

// Tell plugins the path has been successfully prefetched
onPostPrefetchPathname(path)

return true
},

Expand Down Expand Up @@ -274,6 +282,7 @@ const queue = {
// and getting resources for page changes.
getResourcesForPathname: path =>
new Promise((resolve, reject) => {
console.log(`getResourcesForPathname ${path}`)
const doingInitialRender = inInitialRender
inInitialRender = false

Expand Down Expand Up @@ -350,6 +359,9 @@ const queue = {
page,
pageResources,
})
// Tell plugins the path has been successfully prefetched
onPostPrefetchPathname(path)

resolve(pageResources)
})
} else {
Expand Down Expand Up @@ -378,16 +390,16 @@ const queue = {
pageResources,
})

// Tell plugins the path has been successfully prefetched
onPostPrefetchPathname(path)

if (doingInitialRender) {
// We got all resources needed for first mount,
// we can fetch resoures for all pages.
fetchPageResourceMap()
}
})
}

// Tell plugins the path has been successfully prefetched
onPostPrefetchPathname(path)
}),
}

Expand Down
11 changes: 2 additions & 9 deletions packages/gatsby/cache-dir/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,8 @@ const navigate = (to, options = {}) => {
}, 1000)

loader.getResourcesForPathname(pathname).then(pageResources => {
if (!pageResources && process.env.NODE_ENV === `production`) {
loader.getResourcesForPathname(`/404.html`).then(() => {
clearTimeout(timeoutId)
reachNavigate(to, options)
})
} else {
reachNavigate(to, options)
clearTimeout(timeoutId)
}
reachNavigate(to, options)
clearTimeout(timeoutId)
})
}

Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/cache-dir/prefetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ const preFetched = {}

const prefetch = function(url) {
if (preFetched[url]) {
console.log(`prefetch ${url}: already prefetched`)
return
}
preFetched[url] = true
console.log(`prefetch ${url}: actually doing it now`)
supportedPrefetchStrategy(url)
}

Expand Down

0 comments on commit 465a4d2

Please sign in to comment.