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

fix(gatsby): invalidate queries if page context changes between runs #28351

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/gatsby/src/query/__tests__/data-tracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,10 @@ const setup = async ({ restart = isFirstRun, clearCache = false } = {}) => {

if (restart) {
await require(`../../schema`).build({})
await apiRunner(`createPages`)
}

await apiRunner(`createPages`)

Object.entries(pageQueries).forEach(([componentPath, query]) => {
store.dispatch({
type: `QUERY_EXTRACTED`,
Expand Down Expand Up @@ -806,7 +807,7 @@ describe(`query caching between builds`, () => {
}, 99999)
})

describe.skip(`Changing page context invalidates page queries`, () => {
describe(`Changing page context invalidates page queries`, () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

beforeAll(() => {
let pageChangeCounter = 1
let nodeChangeCounter = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ Object {
},
},
},
"pageContextHashes": Map {
"/my-sweet-new-page/" => "4cf9cfa61c013b4697445e9bb48dd436",
},
"pageData": Map {},
"pageDataStats": Map {},
"pendingPageDataWrites": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Map {
exports[`Add pages allows you to add pages 1`] = `
Object {
"contextModified": false,
"pageContextHash": "99914b932bd37a50b983c5e7c90ae93b",
"payload": Object {
"component": "/whatever/index.js",
"componentChunkName": "component---whatever-index-js",
Expand Down Expand Up @@ -117,6 +118,7 @@ Map {
exports[`Add pages allows you to add pages with context 1`] = `
Object {
"contextModified": false,
"pageContextHash": "426ef2719c86cbc77ce9d399793dd8d4",
"payload": Object {
"component": "/whatever/index.js",
"componentChunkName": "component---whatever-index-js",
Expand Down Expand Up @@ -161,6 +163,7 @@ Map {
exports[`Add pages allows you to add pages with matchPath 1`] = `
Object {
"contextModified": false,
"pageContextHash": "99914b932bd37a50b983c5e7c90ae93b",
"payload": Object {
"component": "/whatever/index.js",
"componentChunkName": "component---whatever-index-js",
Expand Down
15 changes: 12 additions & 3 deletions packages/gatsby/src/redux/actions/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const {
const apiRunnerNode = require(`../../utils/api-runner-node`)
const { trackCli } = require(`gatsby-telemetry`)
const { getNonGatsbyCodeFrame } = require(`../../utils/stack-trace-utils`)
import { createContentDigest } from "gatsby-core-utils"

/**
* Memoize function used to pick shadowed page components to avoid expensive I/O.
Expand Down Expand Up @@ -378,6 +379,10 @@ ${reservedFields.map(f => ` * "${f}"`).join(`\n`)}
page.path = truncatedPath
}

if (!page.context) {
page.context = {}
}

const internalPage: Page = {
internalComponentName,
path: page.path,
Expand All @@ -387,7 +392,7 @@ ${reservedFields.map(f => ` * "${f}"`).join(`\n`)}
isCreatedByStatefulCreatePages:
actionOptions?.traceId === `initial-createPagesStatefully`,
// Ensure the page has a context object
context: page.context || {},
context: page.context,
updatedAt: Date.now(),
}

Expand All @@ -396,9 +401,12 @@ ${reservedFields.map(f => ` * "${f}"`).join(`\n`)}
internalPage.path = `/${internalPage.path}`
}

const oldPage: Page = store.getState().pages.get(internalPage.path)
const oldPageContextHash = store
.getState()
.pageContextHashes.get(internalPage.path)
const pageContextHash = createContentDigest(page.context)
const contextModified =
!!oldPage && !_.isEqual(oldPage.context, internalPage.context)
!!oldPageContextHash && oldPageContextHash !== pageContextHash

const alternateSlashPath = page.path.endsWith(`/`)
? page.path.slice(0, -1)
Expand All @@ -418,6 +426,7 @@ ${reservedFields.map(f => ` * "${f}"`).join(`\n`)}
...actionOptions,
type: `CREATE_PAGE`,
contextModified,
pageContextHash,
plugin,
payload: internalPage,
}
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/redux/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export const saveState = (): void => {
pendingPageDataWrites: state.pendingPageDataWrites,
staticQueriesByTemplate: state.staticQueriesByTemplate,
queries: state.queries,
pageContextHashes: state.pageContextHashes,
})
}

Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/src/redux/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { inferenceMetadataReducer } from "./inference-metadata"
import { staticQueriesByTemplateReducer } from "./static-queries-by-template"
import { queriesReducer } from "./queries"
import { visitedPagesReducer } from "./visited-page"
import { pageContextHashesReducer } from "./page-context-hashes"

/**
* @property exports.nodesTouched Set<string>
Expand Down Expand Up @@ -63,4 +64,5 @@ export {
pendingPageDataWritesReducer as pendingPageDataWrites,
staticQueriesByTemplateReducer as staticQueriesByTemplate,
queriesReducer as queries,
pageContextHashesReducer as pageContextHashes,
}
31 changes: 31 additions & 0 deletions packages/gatsby/src/redux/reducers/page-context-hashes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
IGatsbyState,
IDeleteCacheAction,
ICreatePageAction,
IDeletePageAction,
} from "../types"

export const pageContextHashesReducer = (
state: IGatsbyState["pageContextHashes"] = new Map<string, string>(),
action: IDeleteCacheAction | ICreatePageAction | IDeletePageAction
): IGatsbyState["pageContextHashes"] => {
switch (action.type) {
case `DELETE_CACHE`:
return new Map()

case `CREATE_PAGE`: {
// Add page to the state with the path as key
state.set(action.payload.path, action.pageContextHash)
return state
}

case `DELETE_PAGE`: {
state.delete(action.payload.path)

return state
}

default:
return state
}
}
3 changes: 3 additions & 0 deletions packages/gatsby/src/redux/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ export interface IGatsbyState {
pageDataStats: Map<SystemPath, number>
pageData: Map<Identifier, string>
visitedPages: Map<string, Set<string>>
pageContextHashes: Map<string, string>
}

export interface ICachedReduxState {
Expand All @@ -291,6 +292,7 @@ export interface ICachedReduxState {
staticQueriesByTemplate: IGatsbyState["staticQueriesByTemplate"]
pendingPageDataWrites: IGatsbyState["pendingPageDataWrites"]
queries: IGatsbyState["queries"]
pageContextHashes: IGatsbyState["pageContextHashes"]
}

export type ActionsUnion =
Expand Down Expand Up @@ -608,6 +610,7 @@ export interface ICreatePageAction {
payload: IGatsbyPage
plugin?: IGatsbyPlugin
contextModified?: boolean
pageContextHash: string
}

export interface ICreateRedirectAction {
Expand Down