From 57d375383653d5e87491a44c20ba63190c5844cf Mon Sep 17 00:00:00 2001 From: Sidhartha Chatterjee Date: Wed, 29 Jul 2020 14:56:10 +0530 Subject: [PATCH] fix(gatsby): Load static query results from its own Db (#26077) * 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 --- packages/gatsby/cache-dir/loader.js | 12 ++++++++---- packages/gatsby/cache-dir/production-app.js | 20 +++++++++++--------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/packages/gatsby/cache-dir/loader.js b/packages/gatsby/cache-dir/loader.js index bd18f4a3ae212..c81c37c27ae0b 100644 --- a/packages/gatsby/cache-dir/loader.js +++ b/packages/gatsby/cache-dir/loader.js @@ -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() @@ -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 } } @@ -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 @@ -532,3 +532,7 @@ export const publicLoader = { } export default publicLoader + +export function getStaticQueryResults() { + return instance.staticQueryDb +} diff --git a/packages/gatsby/cache-dir/production-app.js b/packages/gatsby/cache-dir/production-app.js index a7bbab8c0e8ba..4e870917528f8 100644 --- a/packages/gatsby/cache-dir/production-app.js +++ b/packages/gatsby/cache-dir/production-app.js @@ -18,6 +18,7 @@ import { ProdLoader, publicLoader, PageResourceStatus, + getStaticQueryResults, } from "./loader" import EnsureResources from "./ensure-resources" import stripPrefix from "./strip-prefix" @@ -70,15 +71,16 @@ apiRunnerAsync(`onClientEntry`).then(() => { {({ location }) => ( - {({ pageResources, location }) => ( - - - {children} - - - )} + {({ pageResources, location }) => { + const staticQueryResults = getStaticQueryResults() + return ( + + + {children} + + + ) + }} )}