Skip to content

Commit

Permalink
fix(gatsby): fix hydration flicker on initial render of ssr page
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Sep 10, 2021
1 parent 709271e commit a128345
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/gatsby/cache-dir/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ export class BaseLoader {
throw new Error(`not a valid pageData response`)
}

if (window.serverData) {
// merge inlined serverData for page-data response for initial page
// and unset window.serverData
jsonPayload.result.serverData = window.serverData
window.serverData = null
}

return Object.assign(loadObj, {
status: PageResourceStatus.Success,
payload: jsonPayload,
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby/cache-dir/static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export default async function staticPage({
scripts,
reversedStyles,
reversedScripts,
extraPageMetadata = ``,
}) {
// for this to work we need this function to be sync or at least ensure there is single execution of it at a time
global.unsafeBuiltinUsage = []
Expand Down Expand Up @@ -393,7 +394,7 @@ export default async function staticPage({
})

// Add page metadata for the current page
const windowPageData = `/*<![CDATA[*/window.pagePath="${pagePath}";/*]]>*/`
const windowPageData = `/*<![CDATA[*/window.pagePath="${pagePath}";${extraPageMetadata}/*]]>*/`

postBodyComponents.push(
<script
Expand Down
4 changes: 4 additions & 0 deletions packages/gatsby/src/utils/page-ssr-module/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ export async function renderHTML({
pageData,
staticQueryContext,
...data.templateDetails.assets,
extraPageMetadata:
data.page.mode && data.results.serverData !== `SSR`
? `window.serverData=${JSON.stringify(data.results.serverData)};`
: undefined,
})

return results.html
Expand Down

0 comments on commit a128345

Please sign in to comment.