Gatsby: Could Node IDs be different in develop vs build?

Created on 7 Nov 2019  路  4Comments  路  Source: gatsbyjs/gatsby

Summary

Could Node IDs (generated by source plugins) be different based on the environment?

How much can we聽rely on them in development: will they always stay the same in production?

Relevant information

I'm setting up a backend service that will serve comments to my Gatsby site.

All of my articles are generated from MDX and they all have a unique Node ID that I can query in GraphQL.

I thought that it would be a good idea to add this article ID to my Comment entity in the database, so that I can query comments by article ID.

However, it seems that the article ID is not the same in development (gatsby develop) and on live environments (gatsby build).

Is this the expected behavior? Could these IDs ever change based on the environment (or any other factors)?

If they do, I will have to find another way to reliably link comments in my DB to Gatsby articles.

Environment (if relevant)

  System:
    OS: Linux 4.15 Ubuntu 18.04.3 LTS (Bionic Beaver)
    CPU: (4) x64 Intel(R) Core(TM) i5-4260U CPU @ 1.40GHz
    Shell: 4.4.20 - /bin/bash
  Binaries:
    Node: 12.9.0 - /tmp/yarn--1573080630304-0.9022882547315243/node
    Yarn: 1.19.1 - /tmp/yarn--1573080630304-0.9022882547315243/yarn
    npm: 6.12.0 - ~/.nvm/versions/node/v12.9.0/bin/npm
  Languages:
    Python: 2.7.15+ - /usr/bin/python
  Browsers:
    Chrome: 67.0.3396.87
    Firefox: 70.0
  npmPackages:
    gatsby: ^2.10.0 => 2.13.32 
    gatsby-image: ^2.2.1 => 2.2.6 
    gatsby-plugin-manifest: ^2.2.0 => 2.2.3 
    gatsby-plugin-mdx: ^1.0.5 => 1.0.15 
    gatsby-plugin-offline: ^2.2.0 => 2.2.4 
    gatsby-plugin-react-helmet: ^3.1.0 => 3.1.2 
    gatsby-plugin-sharp: ^2.2.1 => 2.2.8 
    gatsby-plugin-styled-components: ^3.1.2 => 3.1.2 
    gatsby-plugin-svgr: ^2.0.2 => 2.0.2 
    gatsby-remark-images: ^3.1.0 => 3.1.7 
    gatsby-source-filesystem: ^2.1.0 => 2.1.5 
    gatsby-transformer-remark: ^2.5.0 => 2.6.8 
    gatsby-transformer-sharp: ^2.2.0 => 2.2.4 
    gatsby-transformer-yaml: ^2.2.0 => 2.2.3 

File contents (if changed)

Note: these changed quite a bit but I聽don't think it's relevant here. If the issue I聽raised is not the expected behavior, I'll provide a reproduction repo.

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

question or discussion

All 4 comments

Perhaps you could you slug as the database id. How are you generating the url for each post?

Hey @universse, yes I do have slug. I'm considering it as an alternative, but the problem with it is that if a slug ever changes (for example, renaming an article or changing the slug pattern), the field in the database would have to be manually updated.

I'm still wondering if the behavior I described in the issue is intended in Gatsby. If it's not, then I could look into fixing it instead of working around it 馃檪

@robinmetral Node ID is typically generated using createNodeId which is in gatsby/packages/gatsby/src/utils/create-node-id.js

Now

  • the seedConstant is constant (passed in to uuidv5)
  • uuidv5 is a pure function so will always return the same uuid for the same seed and value
  • the value passed in is typically a string that includes the parent type and its id

So based on the above, IDs are _meant_ to be the same in develop and build.

_However_, IDs are very much for internal use only. We strongly recommend _not_ depending on them.

Now coming to why gatsby-plugin-mdx seems to change them, I'm not sure. gatsby-source-filesystem uses file path to generate them (which I reckon won't change between develop and build) and gatsby-plugin-mdx uses the ${node.id} >>> Mdx format (which again shouldn't change)

Hope this gives you enough to look into this!

Marking this as answered and closing the issue for now! Thank you so much and feel free to comment or reopen if you would like to continue this discussion 馃檪

Was this page helpful?
0 / 5 - 0 ratings