Skip to content

Commit

Permalink
inline page-data object in html when it contains serverData
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Sep 11, 2021
1 parent 709271e commit bdf42b5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
10 changes: 9 additions & 1 deletion packages/gatsby/cache-dir/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ const createComponentUrls = componentChunkName =>
)

export class ProdLoader extends BaseLoader {
constructor(asyncRequires, matchPaths) {
constructor(asyncRequires, matchPaths, pageData) {
const loadComponent = chunkName => {
if (!asyncRequires.components[chunkName]) {
throw new Error(
Expand All @@ -504,6 +504,14 @@ export class ProdLoader extends BaseLoader {
}

super(loadComponent, matchPaths)

if (pageData) {
this.pageDataDb.set(pageData.path, {
pagePath: pageData.path,
payload: pageData,
status: `success`,
})
}
}

doPrefetch(pagePath) {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/cache-dir/production-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import stripPrefix from "./strip-prefix"
// Generated during bootstrap
import matchPaths from "$virtual/match-paths.json"

const loader = new ProdLoader(asyncRequires, matchPaths)
const loader = new ProdLoader(asyncRequires, matchPaths, window.pageData)
setLoader(loader)
loader.setApiRunner(apiRunner)

Expand Down
7 changes: 5 additions & 2 deletions 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,
inlinePageData = false,
}) {
// 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 @@ -330,7 +331,7 @@ export default async function staticPage({
)
})

if (pageData) {
if (pageData && !inlinePageData) {
headComponents.push(
<link
as="fetch"
Expand Down Expand Up @@ -393,7 +394,9 @@ export default async function staticPage({
})

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

postBodyComponents.push(
<script
Expand Down
1 change: 1 addition & 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,7 @@ export async function renderHTML({
pageData,
staticQueryContext,
...data.templateDetails.assets,
inlinePageData: data.page.mode === `SSR` && data.results.serverData,
})

return results.html
Expand Down

0 comments on commit bdf42b5

Please sign in to comment.