@@ -8,7 +8,7 @@ import type { IExecutionResult } from "../../query/types"
8
8
import type { IGatsbyPage , IGatsbySlice , IGatsbyState } from "../../redux/types"
9
9
import { IGraphQLTelemetryRecord } from "../../schema/type-definitions"
10
10
import type { IScriptsAndStyles } from "../client-assets-for-template"
11
- import type { IPageDataWithQueryResult } from "../page-data"
11
+ import type { IPageDataWithQueryResult , ISliceData } from "../page-data"
12
12
import type { Request } from "express"
13
13
import type { Span , SpanContext } from "opentracing"
14
14
@@ -329,9 +329,7 @@ const readStaticQueryContext = async (
329
329
return JSON . parse ( rawSQContext )
330
330
}
331
331
332
- const readSliceData = async (
333
- sliceName : string
334
- ) : Promise < Record < string , { data : unknown } > > => {
332
+ const readSliceData = async ( sliceName : string ) : Promise < ISliceData > => {
335
333
const filePath = path . join ( __dirname , `slice-data` , `${ sliceName } .json` )
336
334
337
335
const rawSliceData = await fs . readFile ( filePath , `utf-8` )
@@ -365,6 +363,29 @@ export async function renderHTML({
365
363
} )
366
364
}
367
365
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
+
368
389
let readStaticQueryContextActivity : MaybePhantomActivity
369
390
let staticQueryContext : Record < string , { data : unknown } >
370
391
try {
@@ -377,22 +398,30 @@ export async function renderHTML({
377
398
)
378
399
readStaticQueryContextActivity . start ( )
379
400
}
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 )
382
416
)
417
+
418
+ staticQueryContext = Object . assign ( { } , ...contextsToMerge )
383
419
} finally {
384
420
if ( readStaticQueryContextActivity ) {
385
421
readStaticQueryContextActivity . end ( )
386
422
}
387
423
}
388
424
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
-
396
425
let renderHTMLActivity : MaybePhantomActivity
397
426
try {
398
427
if ( wrapperActivity ) {
0 commit comments