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(static-query): Use @babel/traverse for traversing algorithm #10443

Merged

Conversation

victorandcode
Copy link
Contributor

Hello 👋 , this PR fixes #10016.

Summary

After digging deep into the rabbit hole of the traversal algorithm in /packages/gatsby/src/internal-plugins/query-runners/file-parser.js, I found out that the traversing algorithm wasn't taking into account the ast nodes of type JSXFragment. Internally, this is because considered in the babel-types version used by babel-traverse v6.26. Therefore, I am using now the more updated @babel/traverse which is a dependency this package already had.

Test code

I created a simple gatsby new gastsby-test and created a index with the following code

import React from 'react'
import { StaticQuery, graphql } from 'gatsby'

import Layout from '../components/layout'

const query = graphql`
  query {
    site {
      siteMetadata {
        title
      }
    }
  }
`;

const IndexPage = () => (
  <Layout>
      <>
        <h1>Hello world!</h1>
        <StaticQuery query={query} render={() => (<div>Hello world</div>)}/>
      </>
  </Layout>
)

export default IndexPage

Then, tested it out with before the changes (displaying loading forever) and after the changes (displays correct text). It works fine and tests are still passing 😄

Let me know if you have any comments
Cheers

@jgierer12
Copy link
Contributor

Thanks! We should probably also change the dependency in package.json and yarn.lock

@pieh
Copy link
Contributor

pieh commented Dec 12, 2018

We could also update https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/bootstrap/resolve-module-exports.js (only other place where we import babel-traverse). If this is drop-in replacement hopefully this is just matter of adjusting import.

Thanks! We should probably also change the dependency in package.json and yarn.lock

We already have @babel/traverse@^7 in dependencies. So if above is done, then we would need just to remove babel-traverse@^6 from deps and update lock file.

One thing that could be helpful is to adjust (or add new test) for checking correctness of StaticQuery extraction if its inside <> in https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/internal-plugins/query-runner/__tests__/file-parser.js

@pieh
Copy link
Contributor

pieh commented Dec 12, 2018

And btw - Awesome job on figuring this one out - it looks like really fringe issue to debug

@victorandcode
Copy link
Contributor Author

Thanks @pieh :) I'll make those changes and update the PR shortly.

@victorandcode victorandcode force-pushed the topics/10016-static-query-loads-forever branch from 542fcfb to 7760d23 Compare December 12, 2018 22:52
@KyleAMathews
Copy link
Contributor

Woah! Thanks for reaching deep and finding this! 🙏

Copy link
Contributor

@pieh pieh left a comment

Choose a reason for hiding this comment

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

Verified locally and works as advertised - once again - awesome debugging to figure this one out! Thanks @victor-cordova!

@pieh pieh merged commit d504d44 into gatsbyjs:master Dec 13, 2018
@gatsbot
Copy link

gatsbot bot commented Dec 13, 2018

Holy buckets, @victor-cordova — we just merged your PR to Gatsby! 💪💜

Gatsby is built by awesome people like you. Let us say “thanks” in two ways:

  1. We’d like to send you some Gatsby swag. As a token of our appreciation, you can go to the Gatsby Swag Store and log in with your GitHub account to get a coupon code good for one free piece of swag. (Currently we’ve got a couple t-shirts available, plus some socks that are really razzing our berries right now.)
  2. We just invited you to join the Gatsby organization on GitHub. This will add you to our team of maintainers. Accept the invite by visiting https://github.com/orgs/gatsbyjs/invitation. By joining the team, you’ll be able to label issues, review pull requests, and merge approved pull requests.

If there’s anything we can do to help, please don’t hesitate to reach out to us: tweet at @gatsbyjs and we’ll come a-runnin’.

Thanks again!

m-allanson added a commit to Bouncey/gatsby that referenced this pull request Dec 14, 2018
* master: (33 commits)
  fix(blog): youfit case study typofix
  Doc improvements to Visual testing with Storybook guide (gatsbyjs#10436)
  fix(gatsby-plugin-offline): prevent incorrect revisioning of static file by workbox (gatsbyjs#10416)
  fix(starters): ttag repo link
  fix typo in pull request template (gatsbyjs#10454)
  fix(www) Fix query for plugin links always ?=undefined (gatsbyjs#10453)
  chore(release): Publish
  fix(gatsby): fix extracting StaticQuery nested in shorthand fragment (gatsbyjs#10443)
  fix(www): avoid querying for no-cache=1 (gatsbyjs#10389)
  fix(gatsby-image): update typescript definitions - properly mark fields as optional (gatsbyjs#10419)
  refactor(gatsby): improve EnsureResources (gatsbyjs#10224)
  Fixed minor Typos and grammatical errors (gatsbyjs#9353)
  docs: add ClinciJS website into showcase (gatsbyjs#10437)
  docs(babel-preset-gatsby): document --save-dev flag in README (gatsbyjs#10434)
  fix(docs): Environment Variables Examples (gatsbyjs#10406)
  chore(release): Publish
  [gatsby-image] re: fade out base64 on full image load (gatsbyjs#7539)
  docs(starter-library): add example to starter library (gatsbyjs#10425)
  docs(gatsby-plugin-offline): specify to not HTTP-cache sw.js (gatsbyjs#10430)
  fix(docs): prompt => confirm (gatsbyjs#10431)
  ...
wconnorwalsh added a commit to wconnorwalsh/gatsby that referenced this pull request Dec 15, 2018
* master:
  feat(www): add unbird feedback component to starter lib (gatsbyjs#10450)
  fix(blog): youfit case study typofix
  Doc improvements to Visual testing with Storybook guide (gatsbyjs#10436)
  fix(gatsby-plugin-offline): prevent incorrect revisioning of static file by workbox (gatsbyjs#10416)
  fix(starters): ttag repo link
  fix typo in pull request template (gatsbyjs#10454)
  fix(www) Fix query for plugin links always ?=undefined (gatsbyjs#10453)
  chore(release): Publish
  fix(gatsby): fix extracting StaticQuery nested in shorthand fragment (gatsbyjs#10443)
m-allanson added a commit to lipis/gatsby that referenced this pull request Dec 17, 2018
* master: (1037 commits)
  Update starters.yml (gatsbyjs#10505)
  chore(release): Publish
  fix(graphql-skip-limit): fix hasNextPage (gatsbyjs#10504)
  chore: use cjs instead of esm for consistency (gatsbyjs#10494)
  feat(gatsby-remark-copy-linked-files): add support for video elements with `src` attribute (gatsbyjs#10395)
  typofix (gatsbyjs#10488)
  Add kobit.in to showcase (gatsbyjs#10496)
  fix(docs): window.reload => window.location.reload (gatsbyjs#10459)
  feat(www): add unbird feedback component to starter lib (gatsbyjs#10450)
  fix(blog): youfit case study typofix
  Doc improvements to Visual testing with Storybook guide (gatsbyjs#10436)
  fix(gatsby-plugin-offline): prevent incorrect revisioning of static file by workbox (gatsbyjs#10416)
  fix(starters): ttag repo link
  fix typo in pull request template (gatsbyjs#10454)
  fix(www) Fix query for plugin links always ?=undefined (gatsbyjs#10453)
  chore(release): Publish
  fix(gatsby): fix extracting StaticQuery nested in shorthand fragment (gatsbyjs#10443)
  fix(www): avoid querying for no-cache=1 (gatsbyjs#10389)
  fix(gatsby-image): update typescript definitions - properly mark fields as optional (gatsbyjs#10419)
  refactor(gatsby): improve EnsureResources (gatsbyjs#10224)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Static query loads forever when inside a fragment <></>
4 participants