Skip to content

Commit

Permalink
fix: adjust page order to make nested matchPaths work (#9719)
Browse files Browse the repository at this point in the history
As per [#9705](#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 pieh committed Nov 6, 2018
1 parent 5dc9346 commit 5b4e0b5
Showing 1 changed file with 6 additions and 4 deletions.
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 5b4e0b5

Please sign in to comment.