Skip to content

Commit

Permalink
fix(gatsby): Load static query results from its own Db (#26077)
Browse files Browse the repository at this point in the history
* Load static query results from its own Db

* Keep StaticQueryContext because it is public API

* Switch to an Object for static query results from a Map

* Move getStaticQueryResults to private API
  • Loading branch information
sidharthachatterjee authored and KyleAMathews committed Jul 29, 2020
1 parent 385f901 commit 57d3753
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
12 changes: 8 additions & 4 deletions packages/gatsby/cache-dir/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class BaseLoader {
// }
this.pageDb = new Map()
this.inFlightDb = new Map()
this.staticQueryDb = new Map()
this.staticQueryDb = {}
this.pageDataDb = new Map()
this.prefetchTriggered = new Set()
this.prefetchCompleted = new Set()
Expand Down Expand Up @@ -258,8 +258,8 @@ export class BaseLoader {
const staticQueryBatchPromise = Promise.all(
staticQueryHashes.map(staticQueryHash => {
// Check for cache in case this static query result has already been loaded
if (this.staticQueryDb.has(staticQueryHash)) {
const jsonPayload = this.staticQueryDb.get(staticQueryHash)
if (this.staticQueryDb[staticQueryHash]) {
const jsonPayload = this.staticQueryDb[staticQueryHash]
return { staticQueryHash, jsonPayload }
}

Expand All @@ -275,7 +275,7 @@ export class BaseLoader {

staticQueryResults.forEach(({ staticQueryHash, jsonPayload }) => {
staticQueryResultsMap[staticQueryHash] = jsonPayload
this.staticQueryDb.set(staticQueryHash, jsonPayload)
this.staticQueryDb[staticQueryHash] = jsonPayload
})

return staticQueryResultsMap
Expand Down Expand Up @@ -532,3 +532,7 @@ export const publicLoader = {
}

export default publicLoader

export function getStaticQueryResults() {
return instance.staticQueryDb
}
20 changes: 11 additions & 9 deletions packages/gatsby/cache-dir/production-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ProdLoader,
publicLoader,
PageResourceStatus,
getStaticQueryResults,
} from "./loader"
import EnsureResources from "./ensure-resources"
import stripPrefix from "./strip-prefix"
Expand Down Expand Up @@ -70,15 +71,16 @@ apiRunnerAsync(`onClientEntry`).then(() => {
<Location>
{({ location }) => (
<EnsureResources location={location}>
{({ pageResources, location }) => (
<StaticQueryContext.Provider
value={pageResources.staticQueryResults}
>
<DataContext.Provider value={{ pageResources, location }}>
{children}
</DataContext.Provider>
</StaticQueryContext.Provider>
)}
{({ pageResources, location }) => {
const staticQueryResults = getStaticQueryResults()
return (
<StaticQueryContext.Provider value={staticQueryResults}>
<DataContext.Provider value={{ pageResources, location }}>
{children}
</DataContext.Provider>
</StaticQueryContext.Provider>
)
}}
</EnsureResources>
)}
</Location>
Expand Down

0 comments on commit 57d3753

Please sign in to comment.