forked from gatsbyjs/gatsby
/
internals.js
83 lines (73 loc) · 2.02 KB
/
internals.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import fs from "fs"
import pify from "pify"
import minimatch from "minimatch"
export const withoutTrailingSlash = path =>
path === `/` ? path : path.replace(/\/$/, ``)
export const writeFile = pify(fs.writeFile)
export const renameFile = pify(fs.rename)
export const runQuery = (handler, query, excludes, pathPrefix) =>
handler(query).then(r => {
if (r.errors) {
throw new Error(r.errors.join(`, `))
}
// Removing excluded paths
r.data.allSitePage.edges = r.data.allSitePage.edges.filter(
page =>
!excludes.some(excludedRoute =>
minimatch(withoutTrailingSlash(page.node.path), excludedRoute)
)
)
// Add path prefix
r.data.allSitePage.edges = r.data.allSitePage.edges.map(page => {
page.node.path = (pathPrefix + page.node.path).replace(/^\/\//g, `/`)
return page
})
// siteUrl Validation
if (
!r.data.site.siteMetadata.siteUrl ||
r.data.site.siteMetadata.siteUrl == null ||
r.data.site.siteMetadata.siteUrl.trim().length == 0
) {
throw new Error(
`SiteMetaData 'siteUrl' property is required and cannot be left empty. Check out the documentation to see a working example: https://www.gatsbyjs.org/packages/gatsby-plugin-sitemap/#how-to-use`
)
}
// remove trailing slash of siteUrl
r.data.site.siteMetadata.siteUrl = withoutTrailingSlash(
r.data.site.siteMetadata.siteUrl
)
return r.data
})
export const defaultOptions = {
query: `
{
site {
siteMetadata {
siteUrl
}
}
allSitePage {
edges {
node {
path
}
}
}
}`,
output: `/sitemap.xml`,
exclude: [
`/dev-404-page`,
`/404`,
`/404.html`,
`/offline-plugin-app-shell-fallback`,
],
createLinkInHead: true,
serialize: ({ site, allSitePage }) =>
allSitePage.edges.map(edge => {
return {
url: site.siteMetadata.siteUrl + edge.node.path,
changefreq: `daily`,
priority: 0.7,
}
}),
}