danielmahon/gatsby-remark-relative-images

Error if using cache

tadeas22 opened this issue · 2 comments

Hello

i am getting into a issue when running gatsby build: Cannot read property 'childImageSharp' of null

success run queries - ...
failed Building static HTML for pages - ...s

 ERROR #95313 

Building static HTML failed for path "/..."

See our docs page for more info on this error: https://gatsby.dev/debug-html

  WebpackError: TypeError: Cannot read property 'childImageSharp' of null
  
  - build-html.js:110 doBuildPages
    [pages]/[gatsby]/dist/commands/build-html.js:110:24
  
  - build-html.js:124 async buildHTML
    [pages]/[gatsby]/dist/commands/build-html.js:124:3
  
  - build.js:200 async build
    [pages]/[gatsby]/dist/commands/build.js:200:5
  

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! synetech-web@2.0.0 build: `gatsby build --prefix-paths`

Steps to reproduce:

  1. Create gatsby page with markdowns with paths to file in frontmatter
  2. First build without cache to create the cache
  3. Modify the source markdown in any way. For example add empty line. The modification must trigger onCreateNode
  4. Second build with cache from step 2.

As the result WebpackError: TypeError: Cannot read property 'childImageSharp' of null is thrown.

After deep dive into function fmImagesToRelative i realize that array fileNodes did not contain any node (except one), because onCreateNode was call exactly ones for modified markdown.

fileNodes.push(node);

As work around y must register all nodes before first call of fmImagesToRelative

let isRegister = true

exports.onCreateNode = ({ node, getNodes }) => {
	if(isRegister) {
		const nodes = getNodes()
		nodes.forEach(node => fmImagesToRelative(node))
		isRegister = false
	}

	fmImagesToRelative(node); // convert image paths for gatsby images
	// ...
};

had no idea getNodes() existed... pushing update soon that should fix this

Cool nice work thx