Skip to content

Commit 44afaf5

Browse files
authored
fix(gatsby): fix hydration flicker on initial render of ssr page (#33134)
* inline page-data object in html when it contains serverData * skip settting page-data preload link header for ssr pages
1 parent f1141a0 commit 44afaf5

File tree

7 files changed

+42
-21
lines changed

7 files changed

+42
-21
lines changed

packages/gatsby-plugin-gatsby-cloud/src/__tests__/__snapshots__/build-headers-program.js.snap

+4-4
Large diffs are not rendered by default.

packages/gatsby-plugin-gatsby-cloud/src/__tests__/build-headers-program.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,22 @@ describe(`build-headers-program`, () => {
125125
pluginCreatorId: `049c1cfd-95f7-5555-a4ac-9b396d098b26`,
126126
},
127127
],
128+
[
129+
`/test/`,
130+
{
131+
jsonName: `test`,
132+
internalComponentName: `ComponentTest`,
133+
path: `/test/`,
134+
matchPath: undefined,
135+
componentChunkName: `component---src-pages-test-js`,
136+
isCreatedByStatefulCreatePages: true,
137+
context: {},
138+
updatedAt: 1557740602361,
139+
pluginCreator___NODE: `049c1cfd-95f7-5555-a4ac-9b396d098b26`,
140+
pluginCreatorId: `049c1cfd-95f7-5555-a4ac-9b396d098b26`,
141+
mode: `SSR`,
142+
},
143+
],
128144
[
129145
`/`,
130146
{
@@ -203,8 +219,8 @@ describe(`build-headers-program`, () => {
203219
expect(output).toMatchSnapshot()
204220
expect(output).toMatch(/app-data\.json/)
205221
expect(output).toMatch(/page-data\.json/)
206-
// we should only check page-data & app-data once which leads to 2 times
207-
expect(fs.existsSync).toBeCalledTimes(2)
222+
// we should only check app-data once which leads to 1 time
223+
expect(fs.existsSync).toBeCalledTimes(1)
208224
})
209225

210226
it(`with manifest['pages-manifest']`, async () => {
@@ -237,7 +253,7 @@ describe(`build-headers-program`, () => {
237253
expect(output).toMatchSnapshot()
238254
expect(output).toMatch(/\/pages-manifest-ab11f09e0ca7ecd3b43e\.js/g)
239255
expect(output).not.toMatch(/\/app-data\.json/g)
240-
expect(output).not.toMatch(/\/page-data\.json/g)
256+
expect(output).toMatch(/\/page-data\.json/g)
241257
expect(output).not.toMatch(/\/undefined/g)
242258
})
243259

packages/gatsby-plugin-gatsby-cloud/src/build-headers-program.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,6 @@ function preloadHeadersByPage({ pages, manifest, pathPrefix, publicFolder }) {
100100
const appDataPath = publicFolder(PAGE_DATA_DIR, `app-data.json`)
101101
const hasAppData = existsSync(appDataPath)
102102

103-
let hasPageData = false
104-
if (pages.size) {
105-
// test if 1 page-data file exists, if it does we know we're on a gatsby version that supports page-data
106-
const pageDataPath = publicFolder(
107-
getPageDataPath(pages.get(pages.keys().next().value).path)
108-
)
109-
hasPageData = existsSync(pageDataPath)
110-
}
111-
112103
pages.forEach(page => {
113104
const scripts = _.flatMap(COMMON_BUNDLES, file =>
114105
getScriptPath(file, manifest)
@@ -121,7 +112,9 @@ function preloadHeadersByPage({ pages, manifest, pathPrefix, publicFolder }) {
121112
json.push(posix.join(PAGE_DATA_DIR, `app-data.json`))
122113
}
123114

124-
if (hasPageData) {
115+
// page-data gets inline for SSR, so we won't be doing page-data request
116+
// and we shouldn't add preload link header for it.
117+
if (page.mode !== `SSR`) {
125118
json.push(getPageDataPath(page.path))
126119
}
127120

packages/gatsby/cache-dir/loader.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ const createComponentUrls = componentChunkName =>
487487
)
488488

489489
export class ProdLoader extends BaseLoader {
490-
constructor(asyncRequires, matchPaths) {
490+
constructor(asyncRequires, matchPaths, pageData) {
491491
const loadComponent = chunkName => {
492492
if (!asyncRequires.components[chunkName]) {
493493
throw new Error(
@@ -504,6 +504,14 @@ export class ProdLoader extends BaseLoader {
504504
}
505505

506506
super(loadComponent, matchPaths)
507+
508+
if (pageData) {
509+
this.pageDataDb.set(pageData.path, {
510+
pagePath: pageData.path,
511+
payload: pageData,
512+
status: `success`,
513+
})
514+
}
507515
}
508516

509517
doPrefetch(pagePath) {

packages/gatsby/cache-dir/production-app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import stripPrefix from "./strip-prefix"
2525
// Generated during bootstrap
2626
import matchPaths from "$virtual/match-paths.json"
2727

28-
const loader = new ProdLoader(asyncRequires, matchPaths)
28+
const loader = new ProdLoader(asyncRequires, matchPaths, window.pageData)
2929
setLoader(loader)
3030
loader.setApiRunner(apiRunner)
3131

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export default async function staticPage({
110110
scripts,
111111
reversedStyles,
112112
reversedScripts,
113+
inlinePageData = false,
113114
}) {
114115
// for this to work we need this function to be sync or at least ensure there is single execution of it at a time
115116
global.unsafeBuiltinUsage = []
@@ -330,7 +331,7 @@ export default async function staticPage({
330331
)
331332
})
332333

333-
if (pageData) {
334+
if (pageData && !inlinePageData) {
334335
headComponents.push(
335336
<link
336337
as="fetch"
@@ -393,7 +394,9 @@ export default async function staticPage({
393394
})
394395

395396
// Add page metadata for the current page
396-
const windowPageData = `/*<![CDATA[*/window.pagePath="${pagePath}";/*]]>*/`
397+
const windowPageData = `/*<![CDATA[*/window.pagePath="${pagePath}";${
398+
inlinePageData ? `window.pageData=${JSON.stringify(pageData)};` : ``
399+
}/*]]>*/`
397400

398401
postBodyComponents.push(
399402
<script

packages/gatsby/src/utils/page-ssr-module/entry.ts

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ export async function renderHTML({
165165
pageData,
166166
staticQueryContext,
167167
...data.templateDetails.assets,
168+
inlinePageData: data.page.mode === `SSR` && data.results.serverData,
168169
})
169170

170171
return results.html

0 commit comments

Comments
 (0)