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): track static queries by template #25549

Merged
merged 1 commit into from
Jul 13, 2020

Conversation

freiksenet
Copy link
Contributor

@freiksenet freiksenet commented Jul 6, 2020

Description

Re-merging Sid's PR with a fix to detect circular dependency.

Related Issues

#25481 #25493

@freiksenet freiksenet requested review from a team as code owners July 6, 2020 10:55
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jul 6, 2020
@freiksenet freiksenet changed the title Feat/queries by template feat(gatsby): track static queries by template Jul 6, 2020
@gatsby-cloud-staging
Copy link

gatsby-cloud-staging bot commented Jul 6, 2020

Your pull request can be previewed in Gatsby Cloud: https://build-77739d43-5d4d-4856-8781-7a79634866a0.staging-previews.gtsb.io

@pieh pieh added topic: StaticQuery type: feature or enhancement Issue that is not a bug and requests the addition of a new feature or enhancement. and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Jul 6, 2020
Copy link
Contributor

@wardpeet wardpeet left a comment

Choose a reason for hiding this comment

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

I added some nits.

packages/gatsby/src/utils/map-pages-to-static-query-hashes.ts this file probably needs some rework in the near future as it's really hard to understand

Comment on lines 165 to 181
// test(`are written correctly when using wrapRootElement`, async () => {
// const queries = [titleQuery]
// const pagePath = `/dynamic-import/`

// const { staticQueryHashes } = await readPageData(publicDir, pagePath)

// expect(staticQueryHashes.sort()).toEqual(queries.map(hashQuery).sort())
// })

// test(`are written correctly when using wrapPageElement`, async () => {
// const queries = [titleQuery]
// const pagePath = `/dynamic-import/`

// const { staticQueryHashes } = await readPageData(publicDir, pagePath)

// expect(staticQueryHashes.sort()).toEqual(queries.map(hashQuery).sort())
// })
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't we enable these?

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you tell me why we don't enable them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've enabled them, or like one of them cause they are redundant.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why not remove them?

Copy link
Contributor

Choose a reason for hiding this comment

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

We should remove them. They are indeed redundant.

if (
!isEqual(
state.staticQueriesByTemplate.get(componentPath),
staticQueryHashes.map(String)
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't map here, right? We should do it when we save identifiers.

Suggested change
staticQueryHashes.map(String)
staticQueryHashes

Comment on lines 31 to 64
compiler.hooks.compilation.tap(`webpack-dep-tree-plugin`, compilation => {
compilation.hooks.seal.tap(`webpack-dep-tree-plugin`, () => {
const state = store.getState()
const mapOfTemplatesToStaticQueryHashes = mapTemplatesToStaticQueryHashes(
state,
compilation
)

mapOfTemplatesToStaticQueryHashes.forEach(
(staticQueryHashes, componentPath) => {
if (
!isEqual(
state.staticQueriesByTemplate.get(componentPath),
staticQueryHashes.map(String)
)
) {
store.dispatch({
type: `ADD_PENDING_TEMPLATE_DATA_WRITE`,
payload: {
componentPath,
},
})
store.dispatch({
type: `SET_STATIC_QUERIES_BY_TEMPLATE`,
payload: {
componentPath,
staticQueryHashes,
},
})
}
}
)
})
})
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be a webpack plugin instead of hacking it inside build-javascript command but this can be done later in the chain :)

@freiksenet freiksenet requested a review from wardpeet July 9, 2020 08:28
@@ -194,9 +185,25 @@ export function mapTemplatesToStaticQueryHashes(

mapOfTemplatesToStaticQueryHashes.set(
page.componentPath,
staticQueryHashes.sort()
staticQueryHashes.sort().map(String)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
staticQueryHashes.sort().map(String)
staticQueryHashes.sort()

@wardpeet
Copy link
Contributor

wardpeet commented Jul 9, 2020

one small suggestion and if ci is green i'm good with merging

@sidharthachatterjee
Copy link
Contributor

Heads up: Community folks have confirmed that the recursion fix by @freiksenet works!

Avoid single char names

Sort at traversal time

Refactor reduce to forEach

Refactor another reduce to forEach

Fix type check

Add check to prevent circular dependency problem

Add integration tests

Typecheck

Address issues in review
@freiksenet freiksenet force-pushed the feat/queries-by-template branch from 13353af to ba7ac69 Compare July 13, 2020 09:32
@freiksenet freiksenet merged commit e640c5b into master Jul 13, 2020
@delete-merged-branch delete-merged-branch bot deleted the feat/queries-by-template branch July 13, 2020 09:54
pragmaticpat pushed a commit to pragmaticpat/gatsby that referenced this pull request Apr 28, 2022
Track static queries by template

Co-authored-by: Sidhartha Chatterjee <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature or enhancement Issue that is not a bug and requests the addition of a new feature or enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants