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.

External images not finished processing before page queries are executed in build

See original GitHub issue

Description

In a project of mine, I process quite a lot of external images at build time.

Something weird started happening one day. The same image would appear on one page, and then not on another. The queried field for the processed image would return undefined. I use Gatsby Image by the way.

Here is a simplified version of how I create the remote files at build time:

const { createRemoteFileNode } = require("gatsby-source-filesystem");

exports.createSchemaCustomization = ({ actions }) => {
    const { createTypes } = actions;
    createTypes(`
    type mongodbVimcsRepositories implements Node {
      processed_images: [File]
    }
  `);
};

exports.onCreateNode = ({
    node,
    actions: { createNode },
    store,
    cache,
    createNodeId,
}) => {
    if (node.internal.type === "<type>") {
        node.image_urls.forEach(async url => {
            const fileNode = await createRemoteFileNode({
                url,
                parentNodeId: node.id,
                createNode,
                createNodeId,
                cache,
                store,
            });

            node.processed_images = [
                ...(node.processed_images || []),
                fileNode,
            ];
        });
    }
};

// page creation and more unrelated stuff below

To understand a little more what was happening, I started logging the URL of the image after the createRemoteFileNode promise was resolved.

I noticed some of them logged a little late in the build process, and coincidentally (or not), they were the images missing in the app on certain pages.

In the screenshot below, all images between the two red lines do not get sent in the result of the page query.

Screen Shot 2020-07-31 at 18 06 12

Note that all the images are queried through page queries and not static queries.

If it can be of any help, here is how I query the images on the page:

export const query = graphql`
  query(<params>) {
    repository: <type>(
      <query>
    ) {
      <other fields>
      images: processed_images {
        childImageSharp {
          fluid(maxWidth: 1280, quality: 80) {
            ...GatsbyImageSharpFluid
          }
        }
      }
    }
  }
  `;
`;

Not sure if this is an issue with Gatsby, or just how I create the remote nodes and the way I wait for the promises to resolve, or if it’s an issue at all. Anyway, I would appreciate a little insight into this as I thought it was interesting.

Workaround

I have found a way to work around this issue I’ve been having, but I’m not confident it’s a good way to solve it. What I did is accumulate the createRemoteFileNode promises in an array, and then await all the promises in the onPostBootstrap hook. Then, I would be sure all the nodes were finished processing before passing onto the build phase.

exports.onPostBootstrap = async () => {
    await Promise.all(imagePromises);
};

Steps to reproduce

I Will set up a basic repo if necessary, as it’s not a super easy setup.

Expected result

Images are all processed before the queries start to execute.

Actual result

Some images finish processing late, resulting in missing images in some pages.

Environment


  System:
    OS: macOS 10.15.4
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 14.5.0 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.5 - /usr/local/bin/npm
  Languages:
    Python: 2.7.16 - /usr/bin/python
  Browsers:
    Chrome: 84.0.4147.105
    Edge: 84.0.522.49
    Firefox: 78.0.2
    Safari: 13.1
  npmPackages:
    gatsby: ^2.24.23 => 2.24.23 
    gatsby-cli: ^2.12.68 => 2.12.68 
    gatsby-image: ^2.4.14 => 2.4.14 
    gatsby-plugin-manifest: ^2.4.21 => 2.4.21 
    gatsby-plugin-offline: ^3.2.21 => 3.2.21 
    gatsby-plugin-react-helmet: ^3.3.1 => 3.3.10 
    gatsby-plugin-robots-txt: ^1.5.1 => 1.5.1 
    gatsby-plugin-sass: ^2.3.1 => 2.3.12 
    gatsby-plugin-sharp: ^2.6.24 => 2.6.24 
    gatsby-source-filesystem: ^2.3.23 => 2.3.23 
    gatsby-source-mongodb: ^2.3.6 => 2.3.10 
    gatsby-transformer-json: ^2.4.1 => 2.4.11 
    gatsby-transformer-sharp: ^2.5.12 => 2.5.12 
  npmGlobalPackages:
    gatsby-cli: 2.6.5

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vladarcommented, Aug 3, 2020

Hi @reobin !

My initial thought is that onCreateNode must be async in your case, that is:

exports.onCreateNode = async ({
    node,
    actions: { createNode },
    store,
    cache,
    createNodeId,
}) => {
  // Hook contents
}

So please try that first. If that doesn’t help then it is incredibly helpful if you’re able to create a minimal reproduction. This is a simplified example of the issue that makes it clear and obvious what the issue is and how we can begin to debug it.

If you’re up for it, we’d very much appreciate if you could provide a minimal reproduction and we’ll be able to take another look.

Thanks for using Gatsby! 💜

1reaction
reobincommented, Aug 3, 2020

@polarathene It happened very consistenly on a medium to high speed connection, so I don’t think that is quite the issue.

Like in my workaround, waiting for the node creation promise to resolve fixes the problem. This means that the image just didn’t have the time to process, and not that it failed at doing so.

I’ll research this a little more this week, and create a repo to reproduce it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Build stuck at running jobs (image transformation) #34051
It's not stuck at "run queries in workers" as that step finishes; Sometimes (not always) it hangs on running image transformation (jobs). If...
Read more >
Troubleshooting Common Errors - Gatsby
If the error is describing an Unknown field 'X' on type 'Query' , the content type you are trying to source is likely...
Read more >
Waiting for image to load in JavaScript - Stack Overflow
So, I have to wait for the image to load to know its dimension before setting position and switching visibility to visible. I...
Read more >
How To Test a React App with Jest and React Testing Library
You expect the placeholder image to be present in the document since a search has not taken place. When finished, save the src/App.test.js...
Read more >
GitLab Runner commands
This command allows to fetch and process builds from GitLab. gitlab-runner run. This is main command that is executed when GitLab Runner is...
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