Skip to content

Commit

Permalink
actually enable query on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Nov 17, 2020
1 parent d0fe1ee commit 10ca94e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/gatsby/cache-dir/dev-loader.js
Expand Up @@ -77,6 +77,9 @@ class DevLoader extends BaseLoader {
}

doPrefetch(pagePath) {
if (process.env.GATSBY_EXPERIMENTAL_QUERY_ON_DEMAND) {
return Promise.resolve()
}
return super.doPrefetch(pagePath).then(result => result.payload)
}

Expand Down
39 changes: 38 additions & 1 deletion packages/gatsby/src/services/calculate-dirty-queries.ts
Expand Up @@ -5,11 +5,48 @@ import { assertStore } from "../utils/assert-store"

export async function calculateDirtyQueries({
store,
websocketManager,
currentlyHandledPendingQueryRuns,
}: Partial<IQueryRunningContext>): Promise<{
queryIds: IGroupedQueryIds
}> {
assertStore(store)
const state = store.getState()
const queryIds = calcDirtyQueryIds(state)
return { queryIds: groupQueryIds(queryIds) }

let queriesToRun: Array<string> = queryIds

if (
process.env.gatsby_executing_command === `develop` &&
process.env.GATSBY_EXPERIMENTAL_QUERY_ON_DEMAND
) {
// 404 are special cases in our runtime that ideally use
// generic things to work, but for now they have special handling
const pagePathFilter = new Set([`/404.html`, `/dev-404-page/`])

// we want to make sure we run queries for pages that user currently
// view in the browser
if (websocketManager?.activePaths) {
websocketManager.activePaths.forEach(
pagePathFilter.add.bind(pagePathFilter)
)
}

// we also want to make sure we include pages that were requested from
// via `page-data` fetches or websocket requests
if (currentlyHandledPendingQueryRuns) {
currentlyHandledPendingQueryRuns.forEach(
pagePathFilter.add.bind(pagePathFilter)
)
}

// static queries are also not on demand
queriesToRun = queryIds.filter(
queryId => queryId.startsWith(`sq--`) || pagePathFilter.has(queryId)
)
}

return {
queryIds: groupQueryIds(queriesToRun),
}
}
17 changes: 16 additions & 1 deletion packages/gatsby/src/services/initialize.ts
@@ -1,5 +1,5 @@
import _ from "lodash"
import { slash } from "gatsby-core-utils"
import { slash, isCI } from "gatsby-core-utils"
import fs from "fs-extra"
import md5File from "md5-file"
import crypto from "crypto"
Expand Down Expand Up @@ -162,6 +162,21 @@ export async function initialize({

activity.end()

if (
process.env.gatsby_executing_command === `develop` &&
process.env.GATSBY_EXPERIMENTAL_QUERY_ON_DEMAND
) {
if (isCI()) {
process.env.GATSBY_EXPERIMENTAL_QUERY_ON_DEMAND = undefined
reporter.warn(
`Experimental Query on Demand feature is not available in CI environment. Continuing with regular mode.`
)
} else {
reporter.info(`Using experimental Query on Demand feature`)
telemetry.trackFeatureIsUsed(`QueryOnDemand`)
}
}

// run stale jobs
store.dispatch(removeStaleJobs(store.getState()))

Expand Down
3 changes: 3 additions & 0 deletions packages/gatsby/src/utils/webpack.config.js
Expand Up @@ -85,6 +85,9 @@ module.exports = async (
envObject.PUBLIC_DIR = JSON.stringify(`${process.cwd()}/public`)
envObject.BUILD_STAGE = JSON.stringify(stage)
envObject.CYPRESS_SUPPORT = JSON.stringify(process.env.CYPRESS_SUPPORT)
envObject.GATSBY_EXPERIMENTAL_QUERY_ON_DEMAND = JSON.stringify(
!!process.env.GATSBY_EXPERIMENTAL_QUERY_ON_DEMAND
)

if (stage === `develop`) {
envObject.GATSBY_SOCKET_IO_DEFAULT_TRANSPORT = JSON.stringify(
Expand Down

0 comments on commit 10ca94e

Please sign in to comment.