Skip to content

Commit

Permalink
fix: Ensure component order is deterministic (#22965)
Browse files Browse the repository at this point in the history
* Ensure component order is deterministic

Ensure component order - and therefore hash - is deterministic when writing out .cache/async-requires.js

* Add test for getComponents
  • Loading branch information
peteris authored Apr 11, 2020
1 parent cff1d7f commit 94267be
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/gatsby/src/bootstrap/__tests__/requires-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,29 @@ describe(`requires-writer`, () => {
})
})
})

describe(`getComponents`, () => {
it(`should return components in a deterministic order`, () => {
const pagesInput = generatePagesState([
{
component: `component1`,
componentChunkName: `chunkName1`,
matchPath: `matchPath1`,
path: `/path1`,
},
{
component: `component2`,
componentChunkName: `chunkName2`,
path: `/path2`,
},
])

const pages = [...pagesInput.values()]
const pagesReversed = [...pagesInput.values()].reverse()

expect(requiresWriter.getComponents(pages)).toEqual(
requiresWriter.getComponents(pagesReversed)
)
})
})
})
2 changes: 2 additions & 0 deletions packages/gatsby/src/bootstrap/requires-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const getComponents = pages =>
_(pages)
.map(pickComponentFields)
.uniqBy(c => c.componentChunkName)
.orderBy(c => c.componentChunkName)
.value()

/**
Expand Down Expand Up @@ -254,4 +255,5 @@ module.exports = {
writeAll,
resetLastHash,
startListener,
getComponents,
}

0 comments on commit 94267be

Please sign in to comment.