Skip to content

Commit cbe4db5

Browse files
authored
fix(gatsby): support slices containing static queries during SSR/DSG (#36779)
1 parent eb8cf8d commit cbe4db5

File tree

2 files changed

+52
-15
lines changed

2 files changed

+52
-15
lines changed

packages/gatsby/src/utils/page-data.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ export interface IPageDataWithQueryResult extends IPageData {
3030
result: IExecutionResult
3131
}
3232

33+
export interface ISliceData {
34+
componentChunkName: string
35+
result: IExecutionResult
36+
staticQueryHashes: Array<string>
37+
}
38+
3339
export async function readPageData(
3440
publicDir: string,
3541
pagePath: string
@@ -162,11 +168,13 @@ export async function writeSliceData(
162168

163169
const outputFilePath = path.join(publicDir, `slice-data`, `${name}.json`)
164170

165-
const body = JSON.stringify({
171+
const sliceData: ISliceData = {
166172
componentChunkName,
167173
result,
168174
staticQueryHashes,
169-
})
175+
}
176+
177+
const body = JSON.stringify(sliceData)
170178

171179
const sliceDataSize = Buffer.byteLength(body) / 1000
172180

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

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { IExecutionResult } from "../../query/types"
88
import type { IGatsbyPage, IGatsbySlice, IGatsbyState } from "../../redux/types"
99
import { IGraphQLTelemetryRecord } from "../../schema/type-definitions"
1010
import type { IScriptsAndStyles } from "../client-assets-for-template"
11-
import type { IPageDataWithQueryResult } from "../page-data"
11+
import type { IPageDataWithQueryResult, ISliceData } from "../page-data"
1212
import type { Request } from "express"
1313
import type { Span, SpanContext } from "opentracing"
1414

@@ -329,9 +329,7 @@ const readStaticQueryContext = async (
329329
return JSON.parse(rawSQContext)
330330
}
331331

332-
const readSliceData = async (
333-
sliceName: string
334-
): Promise<Record<string, { data: unknown }>> => {
332+
const readSliceData = async (sliceName: string): Promise<ISliceData> => {
335333
const filePath = path.join(__dirname, `slice-data`, `${sliceName}.json`)
336334

337335
const rawSliceData = await fs.readFile(filePath, `utf-8`)
@@ -365,6 +363,29 @@ export async function renderHTML({
365363
})
366364
}
367365

366+
const sliceData: Record<string, ISliceData> = {}
367+
if (_CFLAGS_.GATSBY_MAJOR === `5` && process.env.GATSBY_SLICES) {
368+
let readSliceDataActivity: MaybePhantomActivity
369+
try {
370+
if (wrapperActivity) {
371+
readSliceDataActivity = reporter.phantomActivity(
372+
`Preparing slice-data`,
373+
{
374+
parentSpan: wrapperActivity.span,
375+
}
376+
)
377+
readSliceDataActivity.start()
378+
}
379+
for (const sliceName of Object.values(pageData.slicesMap)) {
380+
sliceData[sliceName] = await readSliceData(sliceName)
381+
}
382+
} finally {
383+
if (readSliceDataActivity) {
384+
readSliceDataActivity.end()
385+
}
386+
}
387+
}
388+
368389
let readStaticQueryContextActivity: MaybePhantomActivity
369390
let staticQueryContext: Record<string, { data: unknown }>
370391
try {
@@ -377,22 +398,30 @@ export async function renderHTML({
377398
)
378399
readStaticQueryContextActivity.start()
379400
}
380-
staticQueryContext = await readStaticQueryContext(
381-
data.page.componentChunkName
401+
402+
const uniqueUsedComponentChunkNames = [data.page.componentChunkName]
403+
for (const singleSliceData of Object.values(sliceData)) {
404+
if (
405+
singleSliceData.componentChunkName &&
406+
!uniqueUsedComponentChunkNames.includes(
407+
singleSliceData.componentChunkName
408+
)
409+
) {
410+
uniqueUsedComponentChunkNames.push(singleSliceData.componentChunkName)
411+
}
412+
}
413+
414+
const contextsToMerge = await Promise.all(
415+
uniqueUsedComponentChunkNames.map(readStaticQueryContext)
382416
)
417+
418+
staticQueryContext = Object.assign({}, ...contextsToMerge)
383419
} finally {
384420
if (readStaticQueryContextActivity) {
385421
readStaticQueryContextActivity.end()
386422
}
387423
}
388424

389-
const sliceData = {}
390-
if (_CFLAGS_.GATSBY_MAJOR === `5` && process.env.GATSBY_SLICES) {
391-
for (const sliceName of Object.values(pageData.slicesMap)) {
392-
sliceData[sliceName] = await readSliceData(sliceName)
393-
}
394-
}
395-
396425
let renderHTMLActivity: MaybePhantomActivity
397426
try {
398427
if (wrapperActivity) {

0 commit comments

Comments
 (0)