-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-node.js
80 lines (71 loc) · 1.76 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
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
import path from "path"
import sitemap from "sitemap"
import {
defaultOptions,
filterQuery,
writeFile,
renameFile,
withoutTrailingSlash,
} from "./internals"
const publicPath = `./public`
exports.onPostBuild = async (
{ graphql, pathPrefix, basePath = pathPrefix },
pluginOptions
) => {
const options = { ...pluginOptions }
delete options.plugins
delete options.createLinkInHead
const {
query,
serialize,
output,
exclude,
hostname,
resolveSiteUrl,
...rest
} = {
...defaultOptions,
...options,
}
const saved = path.join(publicPath, output)
// Paths we're excluding...
const excludeOptions = exclude.concat(defaultOptions.exclude)
const queryRecords = await graphql(query)
const filteredRecords = filterQuery(
queryRecords,
excludeOptions,
basePath,
resolveSiteUrl
)
const urls = serialize(filteredRecords)
if (!rest.sitemapSize || urls.length <= rest.sitemapSize) {
const map = sitemap.createSitemap(rest)
urls.forEach(u => map.add(u))
return writeFile(saved, map.toString())
}
const {
site: {
siteMetadata: { siteUrl },
},
} = filteredRecords
return new Promise(resolve => {
// sitemap-index.xml is default file name. (https://git.io/fhNgG)
const indexFilePath = path.join(
publicPath,
`${rest.sitemapName || `sitemap`}-index.xml`
)
const sitemapIndexOptions = {
...rest,
hostname:
(hostname || withoutTrailingSlash(siteUrl)) +
withoutTrailingSlash(pathPrefix || ``),
targetFolder: publicPath,
urls,
callback: error => {
if (error) throw new Error(error)
renameFile(indexFilePath, saved).then(resolve)
},
}
sitemap.createSitemapIndex(sitemapIndexOptions)
})
}