Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-plugin-gatsby-cloud): add total page count ipc #36668

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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