question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can you access body html elements before its written to files?

See original GitHub issue

sources we’ve used:

Summary

We are trying to create a gatsby theme that programmatically searches the DOM for certain html elements (‘h1’ tags, etc…), so we can decorate (add attributes to) the elements before they are written to disk. We have looked into certain ssr hooks such as onPreRenderHTML and onCreatePage (gatsby-node.js) but neither one seems to give us access to the page contents.

Environment (if relevant)

System:
  OS: macOS 10.14.6
  CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
  Shell: 3.2.57 - /bin/bash
Binaries:
  Node: 12.8.0 - ~/.nvm/versions/node/v12.8.0/bin/node
  Yarn: 1.17.3 - ~/.yarn/bin/yarn
  npm: 6.10.3 - ~/.nvm/versions/node/v12.8.0/bin/npm
Languages:
  Python: 2.7.16 - /usr/bin/python
Browsers:
  Chrome: 81.0.4044.113
  Safari: 13.1
npmPackages:
  gatsby: 2.20.8 => 2.20.8 
  gatsby-plugin-react-helmet: ^3.1.22 => 3.1.22 
  gatsby-plugin-sass: ^2.1.29 => 2.1.29 
  gatsby-source-contentful: ^2.1.89 => 2.1.89 
  gatsby-transformer-remark: ^2.6.53 => 2.6.53 
npmGlobalPackages:
  gatsby-cli: 2.7.58
  gatsby-core-utils: 1.0.13
  gatsby-dev-cli: 2.5.35
  gatsby-theme-minimal: 1.0.0
  gatsby: 2.8.8

File contents (if changed)

gatsby-config.js: N/A package.json: N/A gatsby-node.js:

exports.createPages = ({ page }) => {
...
          console.log(page)
          //logging undefined

gatsby-browser.js: N/A gatsby-ssr.js:

exports.onPreRenderHTML = ({ getPreBodyComponents }) => {
  let preBodyComponents = getPreBodyComponents()
  console.log(JSON.stringify(preBodyComponents, null, 2))
  //logging empty []
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
wardpeetcommented, May 13, 2020

@abmagil, this could slowdown the process by a ton and seems like a niche use case. Thanks for the feedback, I’ll share it with the team!

1reaction
wardpeetcommented, Apr 17, 2020

You can use pageContext to inject variables in your page but you still have to wire them correctly. See https://www.gatsbyjs.org/docs/creating-and-modifying-pages/#pass-context-to-pages

I don’t have the full use case but I feel like the onPostBuild is your best bet. You can still use https://github.com/facebook/jest/tree/master/packages/jest-worker to distribute the work over cores. Also Gatsby only does a part distributed, we’ll work more on this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Show text file contents inside html body: how to get the result ...
You can create an html element with id and then access it by document.getElementById() and set innerText property.
Read more >
The Document Body element - HTML - MDN Web Docs - Mozilla
The <body> element exposes the HTMLBodyElement interface. You can access the <body> element through the document.body property. Attributes.
Read more >
How To Access Elements in the DOM - DigitalOcean
The easiest way to access a single element in the DOM is by its unique ID. You can get an element by ID...
Read more >
HTML DOM Document body Property - W3Schools
Setting the body property overwrites all elements in the document's <body>. Note. The difference between document.body and document.documentElement: document.
Read more >
Different ways to access HTML elements using JavaScript
In this scenario, users can use JavaScript to change HTML elements without overwriting them. Before we move ahead to change the HTML element ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found