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

refactor(gatsby): improve EnsureResources #10224

Merged
merged 7 commits into from
Dec 12, 2018
Merged

refactor(gatsby): improve EnsureResources #10224

merged 7 commits into from
Dec 12, 2018

Conversation

vtenfys
Copy link
Contributor

@vtenfys vtenfys commented Nov 30, 2018

  • getDerivedStateFromProps
    • Remove unused parameter pageResources
    • Return even if we don't have resources, to allow detecting them later
  • Remove componentDidUpdate and move some of its contents into new retryResources
  • Throw an error on initial render only upon missing resources - otherwise just don't update the component, and remove the function shouldRenderStaticHTML
    • Note: this may need updating in the future - React docs say shouldComponentUpdate is only for performance purposes and may not prevent rendering in future versions
  • Add a new function for detecting if we have resources which works for production and develop

@vtenfys vtenfys requested a review from a team as a code owner November 30, 2018 18:24
@pieh
Copy link
Contributor

pieh commented Dec 7, 2018

I'm bit worried about updating state and then relying on shouldComponentUpdate to block the render if resources are not there yet - it seems fragile.

If I understand correctly this is to ensure we only reload during navigation if resources can't be loaded and not on initial render (to prevent infinite reload loop) - if so we can just store variable to see if we are doing initial render to avoid reload if it's initial render?

@pieh
Copy link
Contributor

pieh commented Dec 7, 2018

Also not really related to this PR, but something that I didn't caught in previous PR - there seems to be edge cases where:

const __html = document.getElementById(`___gatsby`).innerHTML
return <div dangerouslySetInnerHTML={{ __html }} />

doesn't work correctly - see https://5c0a7e2267610c06381781cc--vibrant-allen-8e8d7b.netlify.com/page-2/ (I just deleted .json for that page there to trigger this) - part of the page doesn't end up in the DOM - this would be caused by react hydration I would assume - when using this setInnerHTML we put more wrapping divs - one from @reach/router and one extra from <div dangerouslySetInnerHTML={{ __html }} /> itself. Unfortunately it doesn't seem we can use dangerouslySetInnerHTML without div or span ( facebook/react#12014 )

@vtenfys
Copy link
Contributor Author

vtenfys commented Dec 10, 2018

I'm bit worried about updating state and then relying on shouldComponentUpdate to block the render if resources are not there yet - it seems fragile.

If I understand correctly this is to ensure we only reload during navigation if resources can't be loaded and not on initial render (to prevent infinite reload loop) - if so we can just store variable to see if we are doing initial render to avoid reload if it's initial render?

Putting the check in shouldComponentUpdate is good because we can check if we have resources early on - but I agree it might be fragile with future versions of React, so I've added an additional check in the render function (so we don't have to rely on it blocking renders).

Also not really related to this PR, but something that I didn't caught in previous PR - there seems to be edge cases where: ... doesn't work correctly

I've updated it to throw an error rather than using dangerouslySetInnerHTML so we no longer have this problem - previously when I tested this, throwing an error broke navigation, but with this refactor links work okay.

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 once again after updates. Nice one, thanks @davidbailey00!

@pieh pieh merged commit a0bba64 into gatsbyjs:master Dec 12, 2018
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)
  ...
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)
  ...
@apvarun
Copy link
Contributor

apvarun commented Dec 18, 2018

I was looking into the changes in this pull request for #10534 and it looks like throwing an error when the resource is not found results in 404 page not being displayed.

@davidbailey00 : Can you elaborate on the gain we get (prevent hydration) by throwing an error? Just trying to understand it.

@vtenfys
Copy link
Contributor Author

vtenfys commented Dec 23, 2018

@apvarun By throwing an error, we prevent React from rendering a blank page. Pages are server-rendered, so if we prevent React rendering then we will still see the page as rendered by the server, which is better than it being blank.

@apvarun
Copy link
Contributor

apvarun commented Dec 26, 2018

I think we should not do this in dev environment as the errors get displayed on the screen for debugging. This prevents the gatsby's default page that lists all the existing routes from not displaying during development.

@vtenfys
Copy link
Contributor Author

vtenfys commented Jan 9, 2019

@apvarun This is now fixed in #10625 :)

gpetrioli pushed a commit to gpetrioli/gatsby that referenced this pull request Jan 22, 2019
- `getDerivedStateFromProps`
  - Remove unused parameter `pageResources`
  - Return even if we don't have resources, to allow detecting them later
- Remove `componentDidUpdate` and move some of its contents into new `retryResources`
- Throw an error on initial render only upon missing resources - otherwise just don't update the component, and remove the function `shouldRenderStaticHTML`
  - Note: this may need updating in the future - React docs say `shouldComponentUpdate` is only for performance purposes and may not prevent rendering in future versions
- Add a new function for detecting if we have resources which works for production and develop
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.

4 participants