Gatsby: Pages rebuilt when no code or content has changed due to non-deterministic hashes

Created on 9 Apr 2020  路  2Comments  路  Source: gatsbyjs/gatsby

Description

When using the experimental page build optimisation (GATSBY_EXPERIMENTAL_PAGE_BUILD_ON_DATA_CHANGES=true), the webpack hash is not deterministic, resulting in unnecessary rebuilds.

This is due to the fact that the pages are not always emitted in the same order, causing the keys in .cache/async-requires.js to be written in a different order and producing a different hash for the module, and therefore the build.

Steps to reproduce

  1. Using the starter with issue reproduction below, and run the build:
$ GATSBY_EXPERIMENTAL_PAGE_BUILD_ON_DATA_CHANGES=true npm run build -- --log-pages
  1. Run the build again a few times.

Gatsby starter with minimal reproduction:
https://github.com/peteris/gatsby-bug-repro-non-deterministic-builds

Expected result

Since no code or content has changed, the build hash should not change and no pages should be emitted.

Actual result

Pages are still periodically emitted. This is due to the fact that the pages are not always emitted in the same order, causing the keys in .cache/async-requires.js to be written in a different order and producing a different hash for the module, and the build.

Potential solution

I believe all we need to do is sort the keys in this function.

// before
.uniqBy(c => c.componentChunkName)

// after
.uniqBy(c => c.componentChunkName)
.orderBy(c => c.componentChunkName) 

I'm happy to open a PR if a maintainer can +1 this idea.

Environment

System:
OS: macOS 10.15.4
CPU: (8) x64 Intel(R) Core(TM) i5-8279U CPU @ 2.40GHz
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 12.6.0 - ~/.nave/installed/12.6.0/bin/node
npm: 6.9.0 - ~/.nave/installed/12.6.0/bin/npm
Languages:
Python: 2.7.16 - /usr/bin/python
Browsers:
Chrome: 80.0.3987.163
Safari: 13.1
npmPackages:
gatsby: ^2.20.12 => 2.20.12

bug

Most helpful comment

I started writing reply suggesting exactly same solution that you did, before realizing you provided it. So definitely "+1" on your solution and pull request like is more than welcome

All 2 comments

I started writing reply suggesting exactly same solution that you did, before realizing you provided it. So definitely "+1" on your solution and pull request like is more than welcome

Was this page helpful?
0 / 5 - 0 ratings

Related issues

KyleAMathews picture KyleAMathews  路  3Comments

ghost picture ghost  路  3Comments

benstr picture benstr  路  3Comments

jimfilippou picture jimfilippou  路  3Comments

magicly picture magicly  路  3Comments