Skip to content

Commit

Permalink
fix: adjust page order to make nested matchPaths work (gatsbyjs#9719)
Browse files Browse the repository at this point in the history
As per [gatsbyjs#9705](gatsbyjs#9705), this changes the order of the pages in pages-writer.js in order to account for the matchPath specificity and therefore make "nested" matchPaths work.

The proposed fix `sortByNumeric` is the most performant solution I could come up with:
[Benchmarks](https://runkit.com/juliansthl/5be08b8f2513440012001807)

Thanks a lot to @pieh for all the help!

(this is my first PR, please let me know if I have to adjust anything)
  • Loading branch information
juliansthl authored and gpetrioli committed Jan 22, 2019
1 parent 2fca0f8 commit 8049bf2
Showing 1 changed file with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ const writePages = async () => {
})

pagesData = _(pagesData)
// Ensure pages keep the same sorting through builds
// and sort pages with matchPath to end so explicit routes
// will match before general.
.sortBy(p => `${p.matchPath ? 1 : 0}${p.path}`)
// Ensure pages keep the same sorting through builds.
// Pages without matchPath come first, then pages with matchPath,
// where more specific patterns come before less specific patterns.
// This ensures explicit routes will match before general.
// Specificity is inferred from number of path segments.
.sortBy(p => `${p.matchPath ? 9999 - p.matchPath.split(`/`).length : `0000`}${p.path}`)
.value()
const newHash = crypto
.createHash(`md5`)
Expand Down

0 comments on commit 8049bf2

Please sign in to comment.