-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-node.js
97 lines (83 loc) · 2.99 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import WebpackAssetsManifest from "webpack-assets-manifest"
import makePluginData from "./plugin-data"
import buildHeadersProgram from "./build-headers-program"
import copyFunctionsManifest from "./copy-functions-manifest"
import createRedirects from "./create-redirects"
import { readJSON } from "fs-extra"
import { joinPath } from "gatsby-core-utils"
import { DEFAULT_OPTIONS, BUILD_HTML_STAGE, BUILD_CSS_STAGE } from "./constants"
const assetsManifest = {}
process.env.GATSBY_PREVIEW_INDICATOR_ENABLED =
process.env.GATSBY_PREVIEW_INDICATOR_ENABLED || `false`
// Inject a webpack plugin to get the file manifests so we can translate all link headers
exports.onCreateWebpackConfig = ({ actions, stage }) => {
if (stage !== BUILD_HTML_STAGE && stage !== BUILD_CSS_STAGE) {
return
}
actions.setWebpackConfig({
plugins: [
new WebpackAssetsManifest({
assets: assetsManifest, // mutates object with entries
merge: true,
}),
],
})
}
exports.onPostBuild = async (
{ store, pathPrefix, reporter },
userPluginOptions
) => {
const pluginData = makePluginData(store, assetsManifest, pathPrefix)
const pluginOptions = { ...DEFAULT_OPTIONS, ...userPluginOptions }
const { redirects } = store.getState()
let rewrites = []
if (pluginOptions.generateMatchPathRewrites) {
const matchPathsFile = joinPath(
pluginData.program.directory,
`.cache`,
`match-paths.json`
)
const matchPaths = await readJSON(matchPathsFile)
rewrites = matchPaths.map(({ matchPath, path }) => {
return {
fromPath: matchPath,
toPath: path,
}
})
}
await Promise.all([
buildHeadersProgram(pluginData, pluginOptions, reporter),
createRedirects(pluginData, redirects, rewrites),
copyFunctionsManifest(pluginData),
])
}
const MATCH_ALL_KEYS = /^/
const pluginOptionsSchema = function ({ Joi }) {
const headersSchema = Joi.object()
.pattern(MATCH_ALL_KEYS, Joi.array().items(Joi.string()))
.description(`Add more headers to specific pages`)
return Joi.object({
headers: headersSchema,
allPageHeaders: Joi.array()
.items(Joi.string())
.description(`Add more headers to all the pages`),
mergeSecurityHeaders: Joi.boolean().description(
`When set to false, turns off the default security headers`
),
mergeLinkHeaders: Joi.boolean().description(
`When set to false, turns off the default gatsby js headers`
),
mergeCachingHeaders: Joi.boolean().description(
`When set to false, turns off the default caching headers`
),
transformHeaders: Joi.function()
.maxArity(2)
.description(
`Transform function for manipulating headers under each path (e.g.sorting), etc. This should return an object of type: { key: Array<string> }`
),
generateMatchPathRewrites: Joi.boolean().description(
`When set to false, turns off automatic creation of redirect rules for client only paths`
),
})
}
exports.pluginOptionsSchema = pluginOptionsSchema