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): Main process can save slices of state & worker can access it #31822

Merged
merged 18 commits into from
Jun 11, 2021

Conversation

LekoArts
Copy link
Contributor

@LekoArts LekoArts commented Jun 8, 2021

Description

In order to save certain parts (slices) of the internal redux store and make it accessible in workers I've added two helper functions called saveStateForWorkers and loadStateInWorker. They reuse the existing readFromCache and writeToCache functions.

Since we now also store parts of the Gatsby State inside redux I had to change some TS typings to move from IGatsbyState to DeepPartial<IGatsbyState>

Related Issues

[ch32396]

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jun 8, 2021
@LekoArts LekoArts added topic: core Relates to Gatsby's core (e.g. page loading, reporter, state machine) and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Jun 8, 2021
@@ -83,15 +84,19 @@ export const configureStore = (initialState: IGatsbyState) =>
)

export type GatsbyReduxStore = ReturnType<typeof configureStore>
export const store: GatsbyReduxStore = configureStore(readState())
export const store: GatsbyReduxStore = configureStore(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

So that the worker doesn't load the persisted state by default

export function readFromCache(): ICachedReduxState {
export function readFromCache(
slices?: Array<GatsbyStateSlices>
): DeepPartial<ICachedReduxState> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since the results for workers (using the slices) won't be the complete state I had to use DeepPartial in some places of this file

@@ -320,6 +320,8 @@ export interface IGatsbyState {
}
}

export type GatsbyStateSlices = keyof IGatsbyState
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This gives you autocomplete for e.g. ['components', 'staticQueryComponents'] etc.

Copy link
Contributor

Choose a reason for hiding this comment

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

The naming is a bit weird IMO. GatsbyStateKeys would be better. I read it as it's a partial piece of state

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point, naming change done in af20d74

packages/gatsby/src/services/initialize.ts Outdated Show resolved Hide resolved
@LekoArts LekoArts marked this pull request as ready for review June 9, 2021 11:48
@LekoArts
Copy link
Contributor Author

@wardpeet wardpeet force-pushed the save-read-state-slices branch from 115ceab to 05c6cea Compare June 10, 2021 11:05
@LekoArts LekoArts merged commit 7aa9c85 into master Jun 11, 2021
@LekoArts LekoArts deleted the save-read-state-slices branch June 11, 2021 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: core Relates to Gatsby's core (e.g. page loading, reporter, state machine)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants