Skip to content

Commit

Permalink
use array of matchPaths
Browse files Browse the repository at this point in the history
  • Loading branch information
Moocar committed Apr 17, 2019
1 parent a8d0fed commit b5798bc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
26 changes: 14 additions & 12 deletions packages/gatsby/cache-dir/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ if (process.env.NODE_ENV !== `production`) {
// Cache for `cleanAndFindPath()`. In case `match-paths.json` is large
const cleanAndFindPathCache = {}

const findMatchPath = (matchPaths, trimmedPathname) => {
for (const { matchPath, path } of matchPaths) {
if (match(matchPath, trimmedPathname)) {
return path
}
}
return null
}

// Given a raw URL path, returns the cleaned version of it (trim off
// `#` and query params), or if it matches an entry in
// `match-paths.json`, its matched path is returned
Expand Down Expand Up @@ -56,21 +65,14 @@ const cleanAndFindPath = rawPathname => {
return cleanAndFindPathCache[trimmedPathname]
}

let foundPath
Object.keys(matchPaths).some(matchPath => {
if (match(matchPath, trimmedPathname)) {
foundPath = matchPaths[matchPath]
return foundPath
}
// Finally, try and match request with default document.
let foundPath = findMatchPath(matchPaths, trimmedPathname)
console.log(`matchPaths`, matchPaths, foundPath)
if (!foundPath) {
if (trimmedPathname === `/index.html`) {
foundPath = `/`
return foundPath
} else {
foundPath = trimmedPathname
}
return false
})
if (!foundPath) {
foundPath = trimmedPathname
}
cleanAndFindPathCache[trimmedPathname] = foundPath
return foundPath
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/query/pages-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const writePages = async () => {
})

if (matchPath) {
matchPaths[matchPath] = path
matchPaths.push({ matchPath, path })
}
})

Expand Down

0 comments on commit b5798bc

Please sign in to comment.