-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-node.js
42 lines (35 loc) · 1.17 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Prefixes should be globs (i.e. of the form "/*" or "/foo/*")
const validatePrefixEntry = prefix => {
if (!prefix.match(/^\//) || !prefix.match(/\/\*$/)) {
throw Error(
`Plugin "gatsby-plugin-client-only-paths" found invalid prefix pattern: ${prefix}`
)
}
}
exports.onCreatePage = ({ page, store, actions }, { prefixes }) => {
const { createPage } = actions
const re = {}
prefixes.forEach(validatePrefixEntry)
return new Promise(resolve => {
// Don't set matchPath again if it's already been set.
if (page.matchPath || page.path.match(/dev-404-page/)) {
return resolve()
}
prefixes.some(prefix => {
if (!re[prefix]) {
// Remove the * from the prefix and memoize
const trimmedPrefix = prefix.replace(/\*$/, ``)
re[prefix] = new RegExp(`^${trimmedPrefix}`)
}
// Ensure that the path ends in a trailing slash, since it can be removed.
const path = page.path.match(/\/$/) ? page.path : `${page.path}/`
if (path.match(re[prefix])) {
page.matchPath = prefix.replace(/\*$/, `*`)
createPage(page)
return true
}
return false
})
return resolve()
})
}