From d236ff1d688109d93b103a5d9739702af5d5430f Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Thu, 22 Sep 2022 15:54:50 +0200 Subject: [PATCH] feat(gatsby-plugin-gatsby-cloud): add total page count ipc --- .../src/__tests__/routes.js | 138 ++++++++++++------ .../src/gatsby-node.js | 3 +- .../gatsby-plugin-gatsby-cloud/src/ipc.js | 12 ++ 3 files changed, 104 insertions(+), 49 deletions(-) diff --git a/packages/gatsby-plugin-gatsby-cloud/src/__tests__/routes.js b/packages/gatsby-plugin-gatsby-cloud/src/__tests__/routes.js index d19871738881e..6d3289783eb1f 100644 --- a/packages/gatsby-plugin-gatsby-cloud/src/__tests__/routes.js +++ b/packages/gatsby-plugin-gatsby-cloud/src/__tests__/routes.js @@ -3,8 +3,66 @@ import * as path from "path" import * as os from "os" const { onPostBuild } = require(`../gatsby-node`) +jest.mock(`gatsby-telemetry`, () => { + return { + captureEvent: jest.fn(), + } +}) + describe(`Routes IPC`, () => { let tmpDir + + const pages = new Map() + + pages.set(`/`, { mode: `DSG`, path: `/` }) + pages.set(`/path/1/`, { mode: `DSG`, path: `/path/1` }) + pages.set(`/path/2/`, { mode: `SSR`, path: `/path/2` }) + pages.set(`/path/3/`, { mode: `SSG`, path: `/path/3` }) + pages.set(`/path/4/`, { + mode: `SSR`, + path: `/path/[id].js`, + matchPath: `/path/:id`, + }) + pages.set(`/path/5/`, { + mode: `SSR`, + path: `/path/[...].js`, + matchPath: `/path/*`, + }) + + const getMockedState = () => { + return { + pages, + program: { + directory: tmpDir, + }, + redirects: [], + components: new Map([ + [ + 1, + { + componentChunkName: `component---node-modules-gatsby-plugin-offline-app-shell-js`, + }, + ], + [ + 2, + { + componentChunkName: `component---src-templates-blog-post-js`, + }, + ], + [ + 3, + { + componentChunkName: `component---src-templates-post-js`, + }, + ], + ]), + config: { + assetPath: ``, + pathPrefix: ``, + }, + } + } + beforeAll(async () => { tmpDir = await fs.mkdtemp( path.join(os.tmpdir(), `gatsby-plugin-gatsby-cloud-item-dir`) @@ -18,58 +76,11 @@ describe(`Routes IPC`, () => { it(`Emits pages with mode`, () => { process.send = jest.fn() - const pages = new Map() - - pages.set(`/`, { mode: `DSG`, path: `/` }) - pages.set(`/path/1/`, { mode: `DSG`, path: `/path/1` }) - pages.set(`/path/2/`, { mode: `SSR`, path: `/path/2` }) - pages.set(`/path/3/`, { mode: `SSG`, path: `/path/3` }) - pages.set(`/path/4/`, { - mode: `SSR`, - path: `/path/[id].js`, - matchPath: `/path/:id`, - }) - pages.set(`/path/5/`, { - mode: `SSR`, - path: `/path/[...].js`, - matchPath: `/path/*`, - }) - onPostBuild( { store: { getState() { - return { - pages, - program: { - directory: tmpDir, - }, - redirects: [], - components: new Map([ - [ - 1, - { - componentChunkName: `component---node-modules-gatsby-plugin-offline-app-shell-js`, - }, - ], - [ - 2, - { - componentChunkName: `component---src-templates-blog-post-js`, - }, - ], - [ - 3, - { - componentChunkName: `component---src-templates-post-js`, - }, - ], - ]), - config: { - assetPath: ``, - pathPrefix: ``, - }, - } + return getMockedState() }, }, }, @@ -160,4 +171,35 @@ describe(`Routes IPC`, () => { ) } }) + + it(`Emits totalPageCount`, async () => { + const originalSend = process.send + process.send = jest.fn() + + await onPostBuild( + { + store: { + getState() { + return getMockedState() + }, + }, + }, + {} + ) + + expect(process.send).toHaveBeenCalledWith( + { + type: `LOG_ACTION`, + action: { + type: `CREATE_TOTAL_RENDERED_PAGE_COUNT`, + payload: { + totalRenderedPageCount: 6, + }, + }, + }, + expect.anything() + ) + + process.send = originalSend + }) }) diff --git a/packages/gatsby-plugin-gatsby-cloud/src/gatsby-node.js b/packages/gatsby-plugin-gatsby-cloud/src/gatsby-node.js index 93aa1f027ef6d..2e51f73ea5c13 100644 --- a/packages/gatsby-plugin-gatsby-cloud/src/gatsby-node.js +++ b/packages/gatsby-plugin-gatsby-cloud/src/gatsby-node.js @@ -12,7 +12,7 @@ import copyFunctionsManifest from "./copy-functions-manifest" import createRedirects from "./create-redirects" import createSiteConfig from "./create-site-config" import { DEFAULT_OPTIONS, BUILD_HTML_STAGE } from "./constants" -import { emitRoutes, emitFileNodes } from "./ipc" +import { emitRoutes, emitFileNodes, emitTotalRenderedPageCount } from "./ipc" const assetsManifest = {} @@ -115,6 +115,7 @@ exports.onPostBuild = async ({ store }, userPluginOptions) => { createSiteConfig(pluginData, pluginOptions), createRedirects(pluginData, redirects, rewrites), copyFunctionsManifest(pluginData), + emitTotalRenderedPageCount(pages.size), ]) } diff --git a/packages/gatsby-plugin-gatsby-cloud/src/ipc.js b/packages/gatsby-plugin-gatsby-cloud/src/ipc.js index eb998ff310145..96bc2078306ce 100644 --- a/packages/gatsby-plugin-gatsby-cloud/src/ipc.js +++ b/packages/gatsby-plugin-gatsby-cloud/src/ipc.js @@ -41,6 +41,18 @@ export function emitRoutes(routes) { }) } +export function emitTotalRenderedPageCount(totalRenderedPageCount) { + return sendOrPromise({ + type: `LOG_ACTION`, + action: { + type: `CREATE_TOTAL_RENDERED_PAGE_COUNT`, + payload: { + totalRenderedPageCount, + }, + }, + }) +} + export function emitRedirects(redirect) { return sendOrPromise({ type: `LOG_ACTION`,