@@ -2,13 +2,9 @@ const React = require(`react`)
2
2
const path = require ( `path` )
3
3
const { renderToString, renderToStaticMarkup } = require ( `react-dom/server` )
4
4
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` )
11
6
const { StaticQueryContext } = require ( `gatsby` )
7
+ const fs = require ( `fs` )
12
8
13
9
const { RouteAnnouncerProps } = require ( `./route-announcer-props` )
14
10
const apiRunner = require ( `./api-runner-ssr` )
@@ -62,10 +58,6 @@ const getStaticQueryUrl = hash =>
62
58
const getAppDataUrl = ( ) =>
63
59
`${ __PATH_PREFIX__ } /${ join ( `page-data` , `app-data.json` ) } `
64
60
65
- function loadPageDataSync ( ) {
66
- throw new Error ( `"loadPageDataSync" is no longer available` )
67
- }
68
-
69
61
const createElement = React . createElement
70
62
71
63
export const sanitizeComponents = components => {
@@ -121,6 +113,29 @@ export default ({
121
113
let postBodyComponents = [ ]
122
114
let bodyProps = { }
123
115
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
+
124
139
const replaceBodyHTMLString = body => {
125
140
bodyHtml = body
126
141
}
0 commit comments