Skip to content

Commit

Permalink
feat(gatsby): PQR workers can access page & static queries (#31852)
Browse files Browse the repository at this point in the history
* test(gatsby): check if worker can access node created in different process

* make timeout longer for workerpool tests

* test fixes from other pr

* wip: saveStateForWorkers / loadStateInWorker

* basic functionality + type fixes

* wip (failing tests)

* revert child

* add redux state func

* revert early return changes

* initial

* update tests and add env var

* Restore pool.ts

* updates

* rename setExtractedSlices to setQueries

* ignore all node_modules

* rename GatsbyStateSlices type to GatsbyStateKeys to more accurately describe it

Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
  • Loading branch information
3 people committed Jun 14, 2021
1 parent 28ca867 commit 222a5ed
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 7 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Expand Up @@ -253,6 +253,7 @@ jobs:
image: "14.17.0"
environment:
GATSBY_EXPERIMENTAL_LMDB_STORE: 1
GATSBY_EXPERIMENTAL_PARALLEL_QUERY_RUNNING: 1
<<: *test_template

integration_tests_gatsby_source_wordpress:
Expand Down
5 changes: 5 additions & 0 deletions packages/gatsby/src/bootstrap/index.ts
Expand Up @@ -16,6 +16,7 @@ import reporter from "gatsby-cli/lib/reporter"
import { globalTracer } from "opentracing"
import type { GatsbyWorkerPool } from "../utils/worker/pool"
import { handleStalePageData } from "../utils/page-data"
import { saveStateForWorkers } from "../redux"

const tracer = globalTracer()

Expand Down Expand Up @@ -62,6 +63,10 @@ export async function bootstrap(

await extractQueries(context)

if (process.env.GATSBY_EXPERIMENTAL_PARALLEL_QUERY_RUNNING) {
saveStateForWorkers([`components`, `staticQueryComponents`])
}

await writeOutRedirects(context)

startRedirectListener()
Expand Down
50 changes: 44 additions & 6 deletions packages/gatsby/src/utils/worker/__tests__/share-state.ts
Expand Up @@ -170,6 +170,7 @@ describe(`worker (share-state)`, () => {

it(`can set slices results into state and access it`, async () => {
worker = createTestWorker()
const staticQueryID = `1`

store.dispatch({
type: `CREATE_PAGE`,
Expand All @@ -179,20 +180,57 @@ describe(`worker (share-state)`, () => {
},
})

const slices: Array<GatsbyStateKeys> = [`components`]
store.dispatch({
type: `REPLACE_STATIC_QUERY`,
plugin: {
name: `test`,
},
payload: {
name: `foo`,
componentPath: dummyPagePayload.component,
id: staticQueryID,
query: `I'm a static query`,
hash: `hash`,
},
})

saveStateForWorkers(slices)
store.dispatch({
type: `QUERY_EXTRACTED`,
payload: {
componentPath: `/foo`,
componentChunkName: `foo`,
query: `I'm a page query`,
},
plugin: {
name: `test`,
},
})

await worker.setState(slices)
saveStateForWorkers([`components`, `staticQueryComponents`])

const res = await worker.getComponent(dummyPagePayload.component)
await worker.setQueries()

expect(res).toMatchInlineSnapshot(`
const components = await worker.getComponent(dummyPagePayload.component)
const staticQueryComponents = await worker.getStaticQueryComponent(
staticQueryID
)

expect(components).toMatchInlineSnapshot(`
Object {
"componentPath": "/foo",
"isInBootstrap": true,
"pages": Object {},
"query": "",
"query": "I'm a page query",
}
`)

expect(staticQueryComponents).toMatchInlineSnapshot(`
Object {
"componentPath": "/foo",
"hash": "hash",
"id": "1",
"name": "foo",
"query": "I'm a static query",
}
`)
})
Expand Down
@@ -1,6 +1,10 @@
import { getNode } from "../../../../datastore"
import { store } from "../../../../redux"
import { IGatsbyPage, IGatsbyPageComponent } from "../../../../redux/types"
import {
IGatsbyPage,
IGatsbyPageComponent,
IGatsbyStaticQueryComponents,
} from "../../../../redux/types"
import reporter from "gatsby-cli/lib/reporter"
import apiRunner from "../../../api-runner-node"

Expand All @@ -24,6 +28,11 @@ export function getComponent(
): IGatsbyPageComponent | undefined {
return store.getState().components.get(componentPath)
}
export function getStaticQueryComponent(
id: IGatsbyStaticQueryComponents["id"]
): IGatsbyStaticQueryComponents | undefined {
return store.getState().staticQueryComponents.get(id)
}

// test: reporter
export function log(message: string): boolean {
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/utils/worker/child/index.ts
@@ -1,3 +1,4 @@
// Note: this doesn't check for conflicts between module exports
export { renderHTMLProd, renderHTMLDev } from "./render-html"
export { setQueries } from "./schema"
export { loadConfigAndPlugins } from "./load-config-and-plugins"
5 changes: 5 additions & 0 deletions packages/gatsby/src/utils/worker/child/schema.ts
@@ -0,0 +1,5 @@
import { setState } from "./state"

export function setQueries(): void {
setState([`components`, `staticQueryComponents`])
}

0 comments on commit 222a5ed

Please sign in to comment.