Skip to content

Commit 3d8a7db

Browse files
piehgatsbybot
and
gatsbybot
authored
fix: re-implement loadPageDataSync for onRenderBody in gatsby-ssr (#29734)
Co-authored-by: gatsbybot <[email protected]>
1 parent 684ac0e commit 3d8a7db

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

packages/gatsby/cache-dir/ssr-builtin-trackers/tracking-unsafe-module-wrapper.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ function createProxyHandler(prefix) {
1212
}
1313
Error.captureStackTrace(myErrorHolder, wrapper)
1414

15-
global.unsafeBuiltinUsage.push(myErrorHolder.stack)
15+
// loadPageDataSync already is tracked with dedicated warning messages,
16+
// so skipping marking it to avoid multiple messages for same usage
17+
if (!myErrorHolder.stack.includes(`loadPageDataSync`)) {
18+
global.unsafeBuiltinUsage.push(myErrorHolder.stack)
19+
}
20+
1621
return value.apply(target, args)
1722
}
1823
} else if (typeof value === `object` && value !== null) {

packages/gatsby/cache-dir/static-entry.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ const React = require(`react`)
22
const path = require(`path`)
33
const { renderToString, renderToStaticMarkup } = require(`react-dom/server`)
44
const { ServerLocation, Router, isRedirect } = require(`@reach/router`)
5-
const {
6-
merge,
7-
8-
flattenDeep,
9-
replace,
10-
} = require(`lodash`)
5+
const { merge, flattenDeep, replace } = require(`lodash`)
116
const { StaticQueryContext } = require(`gatsby`)
7+
const fs = require(`fs`)
128

139
const { RouteAnnouncerProps } = require(`./route-announcer-props`)
1410
const apiRunner = require(`./api-runner-ssr`)
@@ -62,10 +58,6 @@ const getStaticQueryUrl = hash =>
6258
const getAppDataUrl = () =>
6359
`${__PATH_PREFIX__}/${join(`page-data`, `app-data.json`)}`
6460

65-
function loadPageDataSync() {
66-
throw new Error(`"loadPageDataSync" is no longer available`)
67-
}
68-
6961
const createElement = React.createElement
7062

7163
export const sanitizeComponents = components => {
@@ -121,6 +113,29 @@ export default ({
121113
let postBodyComponents = []
122114
let bodyProps = {}
123115

116+
function loadPageDataSync(_pagePath) {
117+
if (_pagePath === pagePath) {
118+
// no need to use fs if we are asking for pageData of current page
119+
return pageData
120+
}
121+
122+
const pageDataPath = getPageDataPath(_pagePath)
123+
const pageDataFile = join(process.cwd(), `public`, pageDataPath)
124+
try {
125+
// deprecation notice
126+
const myErrorHolder = {
127+
name: `Usage of loadPageDataSync for page other than currently generated page disables incremental html generation in future builds`,
128+
}
129+
Error.captureStackTrace(myErrorHolder, loadPageDataSync)
130+
global.unsafeBuiltinUsage.push(myErrorHolder.stack)
131+
const pageDataJson = fs.readFileSync(pageDataFile)
132+
return JSON.parse(pageDataJson)
133+
} catch (error) {
134+
// not an error if file is not found. There's just no page data
135+
return null
136+
}
137+
}
138+
124139
const replaceBodyHTMLString = body => {
125140
bodyHtml = body
126141
}

0 commit comments

Comments
 (0)