Skip to content

Commit

Permalink
feat(gatsby-plugin-gatsby-cloud): add total page count ipc (#36668)
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet authored Sep 22, 2022
1 parent 5dbb103 commit 3ba9b63
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 49 deletions.
138 changes: 90 additions & 48 deletions packages/gatsby-plugin-gatsby-cloud/src/__tests__/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand All @@ -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()
},
},
},
Expand Down Expand Up @@ -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
})
})
3 changes: 2 additions & 1 deletion packages/gatsby-plugin-gatsby-cloud/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -115,6 +115,7 @@ exports.onPostBuild = async ({ store }, userPluginOptions) => {
createSiteConfig(pluginData, pluginOptions),
createRedirects(pluginData, redirects, rewrites),
copyFunctionsManifest(pluginData),
emitTotalRenderedPageCount(pages.size),
])
}

Expand Down
12 changes: 12 additions & 0 deletions packages/gatsby-plugin-gatsby-cloud/src/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down

0 comments on commit 3ba9b63

Please sign in to comment.