diff --git a/packages/gatsby/cache-dir/loader.js b/packages/gatsby/cache-dir/loader.js index c18ef9550fbbe..35a5fdb32e650 100644 --- a/packages/gatsby/cache-dir/loader.js +++ b/packages/gatsby/cache-dir/loader.js @@ -38,9 +38,6 @@ const loadPageDataJson = loadObj => { if (status === 200) { try { const jsonPayload = JSON.parse(responseText) - if (jsonPayload.webpackCompilationHash === undefined) { - throw new Error(`not a valid pageData response`) - } return Object.assign(loadObj, { status: `success`, @@ -173,8 +170,12 @@ export class BaseLoader { return this.inFlightDb.get(pagePath) } - const inFlight = this.loadPageDataJson(pagePath) - .then(result => { + const inFlight = Promise.all([ + this.loadAppData(), + this.loadPageDataJson(pagePath), + ]) + .then(allData => { + const result = allData[1] if (result.notFound) { // if request was a 404, we should fallback to findMatchPath. let foundMatchPatch = findMatchPath(pagePath) @@ -199,7 +200,7 @@ export class BaseLoader { ) } - const pageData = result.payload + let pageData = result.payload const { componentChunkName } = pageData return this.loadComponent(componentChunkName).then(component => { const finalResult = { createdAt: new Date() } @@ -211,6 +212,9 @@ export class BaseLoader { if (result.notFound === true) { finalResult.notFound = true } + pageData = Object.assign(pageData, { + webpackCompilationHash: allData[0].appHash, + }) pageResources = toPageResources(pageData, component) finalResult.payload = pageResources emitter.emit(`onPostLoadPageResources`, { @@ -320,6 +324,30 @@ export class BaseLoader { const page = this.pageDb.get(pagePath) return page && page.notFound === true } + + loadAppData() { + return doFetch(`${__PATH_PREFIX__}/app-data.json`).then(req => { + const { status, responseText } = req + + let appData + + // Handle 200 + if (status === 200) { + try { + const jsonPayload = JSON.parse(responseText) + if (jsonPayload.appHash === undefined) { + throw new Error(`not a valid app-data response`) + } + + appData = jsonPayload + } catch (err) { + // continue regardless of error + } + } + + return appData + }) + } } const createComponentUrls = componentChunkName => @@ -389,6 +417,7 @@ export const publicLoader = { prefetch: rawPath => instance.prefetch(rawPath), isPageNotFound: rawPath => instance.isPageNotFound(rawPath), hovering: rawPath => instance.hovering(rawPath), + loadAppData: () => instance.loadAppData(), } export default publicLoader diff --git a/packages/gatsby/cache-dir/production-app.js b/packages/gatsby/cache-dir/production-app.js index f1a46e15f5006..2632093ca6751 100644 --- a/packages/gatsby/cache-dir/production-app.js +++ b/packages/gatsby/cache-dir/production-app.js @@ -26,7 +26,6 @@ loader.setApiRunner(apiRunner) window.asyncRequires = asyncRequires window.___emitter = emitter window.___loader = publicLoader -window.___webpackCompilationHash = window.webpackCompilationHash navigationInit() @@ -125,6 +124,9 @@ apiRunnerAsync(`onClientEntry`).then(() => { } not found. Not rendering React` ) } + + window.___webpackCompilationHash = page.page.webpackCompilationHash + const Root = () => ( {locationContext => } diff --git a/packages/gatsby/cache-dir/static-entry.js b/packages/gatsby/cache-dir/static-entry.js index f9ec9060c693f..9b68fb6feb4cb 100644 --- a/packages/gatsby/cache-dir/static-entry.js +++ b/packages/gatsby/cache-dir/static-entry.js @@ -322,6 +322,15 @@ export default (pagePath, callback) => { ) }) + headComponents.push( + + ) if (pageData) { headComponents.push( { } }) - const webpackCompilationHash = pageData.webpackCompilationHash - // Add page metadata for the current page - const windowPageData = `/**/` + const windowPageData = `/**/` postBodyComponents.push(