Skip to content

Commit c991ab4

Browse files
authored
feat(gatsby): add node manifest page data digest (#32837)
* process node manifests as part of page-data.json writing so we can add a page-data digest * move for processNodeManifests to work with gatsby build as well as inc-builds
1 parent 13415e4 commit c991ab4

File tree

5 files changed

+62
-8
lines changed

5 files changed

+62
-8
lines changed

integration-tests/node-manifest/__tests__/create-node-manifest.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const urling = require(`urling`)
55
const rimraf = require(`rimraf`)
66
const path = require(`path`)
77
const fs = require(`fs-extra`)
8+
const {
9+
getPageDataDigestForPagePath,
10+
} = require(`gatsby/dist/utils/node-manifest`)
811

912
const manifestDir = path.join(
1013
process.cwd(),
@@ -72,6 +75,17 @@ describe(`Node Manifest API in "gatsby ${gatsbyCommandName}"`, () => {
7275
)
7376
).toBe(true)
7477
})
78+
79+
// this doesn't work in gatsby develop since page-data.json files aren't written out
80+
it(`Adds a correct page-data.json digest to the manifest`, async () => {
81+
const pageDataDigest = await getPageDataDigestForPagePath(
82+
`/one`,
83+
process.cwd()
84+
)
85+
const manifest = await getManifestContents(`1`)
86+
87+
expect(pageDataDigest).toEqual(manifest.pageDataDigest)
88+
})
7589
}
7690

7791
it(`Creates a node manifest with a null page path when createNodeManifest is called but a page is not created for the provided node in the Gatsby site`, async () => {

packages/gatsby/src/commands/build-html.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { IProgram, Stage } from "./types"
1818
import { PackageJson } from "../.."
1919
import type { GatsbyWorkerPool } from "../utils/worker/pool"
2020
import { IPageDataWithQueryResult } from "../utils/page-data"
21+
import { processNodeManifests } from "../utils/node-manifest"
2122

2223
type IActivity = any // TODO
2324

@@ -459,5 +460,8 @@ export async function buildHTMLPagesAndDeleteStaleArtifacts({
459460
deletePageDataActivityTimer.end()
460461
}
461462

463+
// we process node manifests in this location because we need to make sure all page-data.json files are written for gatsby as well as inc-builds (both call builHTMLPagesAndDeleteStaleArtifacts). Node manifests include a digest of the corresponding page-data.json file and at this point we can be sure page-data has been written out for the latest updates in gatsby build AND inc builds.
464+
await processNodeManifests()
465+
462466
return { toRegenerate, toDelete }
463467
}

packages/gatsby/src/query/index.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
} from "../utils/websocket-manager"
1212
import { GraphQLRunner } from "./graphql-runner"
1313
import { IGroupedQueryIds } from "../services"
14-
import { processNodeManifests } from "../utils/node-manifest"
1514

1615
if (process.env.GATSBY_EXPERIMENTAL_QUERY_CONCURRENCY) {
1716
console.info(
@@ -238,13 +237,6 @@ export async function processPageQueries(
238237
graphqlTracing,
239238
})
240239

241-
if (process.env.NODE_ENV !== `development`) {
242-
/**
243-
* only process node manifests here when not in develop. for gatsby develop we process node manifests in src/query/query-watcher.ts everytime queries are re-run. Because we process node manifests in this location for gatsby build we have all the information needed to create the manifests. In query-watcher during gatsby build we might not have all information about created pages and queries.
244-
*/
245-
await processNodeManifests()
246-
}
247-
248240
return processedQueries
249241
}
250242

packages/gatsby/src/utils/node-manifest.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { store } from "../redux/"
66
import { internalActions } from "../redux/actions"
77
import path from "path"
88
import fs from "fs-extra"
9+
import { readPageData } from "./page-data"
10+
import { createContentDigest } from "gatsby-core-utils"
911

1012
interface INodeManifestPage {
1113
path?: string
@@ -20,6 +22,7 @@ interface INodeManifestOut {
2022
id: string
2123
}
2224
foundPageBy: FoundPageBy
25+
pageDataDigest: string | null
2326
}
2427

2528
type FoundPageBy =
@@ -202,6 +205,41 @@ export function warnAboutNodeManifestMappingProblems({
202205
}
203206
}
204207

208+
/**
209+
* Retrieves the content digest of a page-data.json file for use in creating node manifest files.
210+
*/
211+
export async function getPageDataDigestForPagePath(
212+
pagePath?: string,
213+
directory?: string
214+
): Promise<string | null> {
215+
if (
216+
// if no page was created for the node we're creating a manifest for, there wont be a page path.
217+
!pagePath ||
218+
// we only add page data digests to node manifests in production because page-data.json may not exist in development.
219+
(process.env.NODE_ENV !== `production` && process.env.NODE_ENV !== `test`)
220+
) {
221+
return null
222+
}
223+
224+
try {
225+
const publicDirectory = path.join(
226+
directory || store.getState().program.directory,
227+
`public`
228+
)
229+
const pageData = await readPageData(publicDirectory, pagePath)
230+
231+
const pageDataDigest = createContentDigest(pageData)
232+
233+
return pageDataDigest
234+
} catch (e) {
235+
reporter.warn(
236+
`No page-data.json found for ${pagePath} while processing node manifests.`
237+
)
238+
239+
return null
240+
}
241+
}
242+
205243
/**
206244
* Prepares and then writes out an individual node manifest file to be used for routing to previews. Manifest files are added via the public unstable_createNodeManifest action
207245
*/
@@ -229,10 +267,15 @@ export async function processNodeManifest(
229267
foundPageBy,
230268
})
231269

270+
const pageDataDigest = await getPageDataDigestForPagePath(
271+
nodeManifestPage.path
272+
)
273+
232274
const finalManifest: INodeManifestOut = {
233275
node: inputManifest.node,
234276
page: nodeManifestPage,
235277
foundPageBy,
278+
pageDataDigest,
236279
}
237280

238281
const gatsbySiteDirectory = store.getState().program.directory

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ export async function flush(): Promise<void> {
279279
}
280280

281281
writePageDataActivity.end()
282+
282283
isFlushing = false
283284

284285
return

0 commit comments

Comments
 (0)