Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: re-implement loadPageDataSync for onRenderBody in gatsby-ssr #29734

Merged
merged 3 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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