From 1eeb520bba52ecd19ccf2af314e0f88fa4db760e Mon Sep 17 00:00:00 2001 From: Anthony Marcar Date: Tue, 16 Apr 2019 18:05:28 -0700 Subject: [PATCH] Websocket manager use page data (#13389) * add utils/page-data.read * read websocket page data from utils/page-data --- packages/gatsby/src/utils/page-data.js | 7 +++++ .../gatsby/src/utils/websocket-manager.js | 28 +++++++++---------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/packages/gatsby/src/utils/page-data.js b/packages/gatsby/src/utils/page-data.js index 7583664a769b6..276ec3a6723aa 100644 --- a/packages/gatsby/src/utils/page-data.js +++ b/packages/gatsby/src/utils/page-data.js @@ -6,6 +6,12 @@ const getFilePath = ({ publicDir }, pagePath) => { return path.join(publicDir, `page-data`, fixedPagePath, `page-data.json`) } +const read = async ({ publicDir }, pagePath) => { + const filePath = getFilePath({ publicDir }, pagePath) + const rawPageData = await fs.readFile(filePath, `utf-8`) + return JSON.parse(rawPageData) +} + const write = async ({ publicDir }, page, result) => { const filePath = getFilePath({ publicDir }, page.path) const body = { @@ -17,5 +23,6 @@ const write = async ({ publicDir }, page, result) => { } module.exports = { + read, write, } diff --git a/packages/gatsby/src/utils/websocket-manager.js b/packages/gatsby/src/utils/websocket-manager.js index 93812e795d6fb..62d858d12bcc3 100644 --- a/packages/gatsby/src/utils/websocket-manager.js +++ b/packages/gatsby/src/utils/websocket-manager.js @@ -3,6 +3,7 @@ const path = require(`path`) const { store } = require(`../redux`) const fs = require(`fs`) +const pageDataUtil = require(`../utils/page-data`) type QueryResult = { id: string, @@ -32,27 +33,24 @@ const readCachedResults = (dataFileName: string, directory: string): object => { * @param {string} pagePath Path to a page. * @param {string} directory Root directory of current project. */ -const getCachedPageData = ( +const getCachedPageData = async ( pagePath: string, directory: string ): QueryResult => { - const { jsonDataPaths, pages } = store.getState() - const page = pages.get(pagePath) - if (!page) { - return null - } - const dataPath = jsonDataPaths[page.jsonName] - if (typeof dataPath === `undefined`) { + const { program } = store.getState() + const publicDir = path.join(program.directory, `public`) + try { + const pageData = await pageDataUtil.read({ publicDir }, pagePath) + return { + result: pageData.result, + id: pagePath, + } + } catch (err) { console.log( `Error loading a result for the page query in "${pagePath}". Query was not run and no cached result was found.` ) return undefined } - - return { - result: readCachedResults(dataPath, directory), - id: pagePath, - } } /** @@ -156,9 +154,9 @@ class WebsocketManager { } } - const getDataForPath = path => { + const getDataForPath = async path => { if (!this.pageResults.has(path)) { - const result = getCachedPageData(path, this.programDir) + const result = await getCachedPageData(path, this.programDir) if (result) { this.pageResults.set(path, result) } else {