Skip to content

Commit

Permalink
fix: re-implement loadPageDataSync for onRenderBody in gatsby-ssr (#2…
Browse files Browse the repository at this point in the history
…9734)

Co-authored-by: gatsbybot <[email protected]>
  • Loading branch information
pieh and gatsbybot authored Feb 24, 2021
1 parent 684ac0e commit 3d8a7db
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
35 changes: 25 additions & 10 deletions packages/gatsby/cache-dir/static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 3d8a7db

Please sign in to comment.