From 3d8a7db7f51f61f6ba41de60782c28c4a298187e Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Wed, 24 Feb 2021 20:04:15 +0100 Subject: [PATCH] fix: re-implement loadPageDataSync for onRenderBody in gatsby-ssr (#29734) Co-authored-by: gatsbybot --- .../tracking-unsafe-module-wrapper.js | 7 +++- packages/gatsby/cache-dir/static-entry.js | 35 +++++++++++++------ 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/packages/gatsby/cache-dir/ssr-builtin-trackers/tracking-unsafe-module-wrapper.js b/packages/gatsby/cache-dir/ssr-builtin-trackers/tracking-unsafe-module-wrapper.js index df374dab131ad..419cd613f8bdb 100644 --- a/packages/gatsby/cache-dir/ssr-builtin-trackers/tracking-unsafe-module-wrapper.js +++ b/packages/gatsby/cache-dir/ssr-builtin-trackers/tracking-unsafe-module-wrapper.js @@ -12,7 +12,12 @@ function createProxyHandler(prefix) { } Error.captureStackTrace(myErrorHolder, wrapper) - global.unsafeBuiltinUsage.push(myErrorHolder.stack) + // loadPageDataSync already is tracked with dedicated warning messages, + // so skipping marking it to avoid multiple messages for same usage + if (!myErrorHolder.stack.includes(`loadPageDataSync`)) { + global.unsafeBuiltinUsage.push(myErrorHolder.stack) + } + return value.apply(target, args) } } else if (typeof value === `object` && value !== null) { diff --git a/packages/gatsby/cache-dir/static-entry.js b/packages/gatsby/cache-dir/static-entry.js index 86d68faf02356..6ac6ebdf748a4 100644 --- a/packages/gatsby/cache-dir/static-entry.js +++ b/packages/gatsby/cache-dir/static-entry.js @@ -2,13 +2,9 @@ const React = require(`react`) const path = require(`path`) const { renderToString, renderToStaticMarkup } = require(`react-dom/server`) const { ServerLocation, Router, isRedirect } = require(`@reach/router`) -const { - merge, - - flattenDeep, - replace, -} = require(`lodash`) +const { merge, flattenDeep, replace } = require(`lodash`) const { StaticQueryContext } = require(`gatsby`) +const fs = require(`fs`) const { RouteAnnouncerProps } = require(`./route-announcer-props`) const apiRunner = require(`./api-runner-ssr`) @@ -62,10 +58,6 @@ const getStaticQueryUrl = hash => const getAppDataUrl = () => `${__PATH_PREFIX__}/${join(`page-data`, `app-data.json`)}` -function loadPageDataSync() { - throw new Error(`"loadPageDataSync" is no longer available`) -} - const createElement = React.createElement export const sanitizeComponents = components => { @@ -121,6 +113,29 @@ export default ({ let postBodyComponents = [] let bodyProps = {} + function loadPageDataSync(_pagePath) { + if (_pagePath === pagePath) { + // no need to use fs if we are asking for pageData of current page + return pageData + } + + const pageDataPath = getPageDataPath(_pagePath) + const pageDataFile = join(process.cwd(), `public`, pageDataPath) + try { + // deprecation notice + const myErrorHolder = { + name: `Usage of loadPageDataSync for page other than currently generated page disables incremental html generation in future builds`, + } + Error.captureStackTrace(myErrorHolder, loadPageDataSync) + global.unsafeBuiltinUsage.push(myErrorHolder.stack) + const pageDataJson = fs.readFileSync(pageDataFile) + return JSON.parse(pageDataJson) + } catch (error) { + // not an error if file is not found. There's just no page data + return null + } + } + const replaceBodyHTMLString = body => { bodyHtml = body }