-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgatsby-node.js
120 lines (111 loc) · 3.77 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// https://www.netlify.com/docs/headers-and-basic-auth/
import WebpackAssetsManifest from "webpack-assets-manifest"
import { generatePageDataPath } from "gatsby-core-utils"
import makePluginData from "./plugin-data"
import buildHeadersProgram from "./build-headers-program"
import createRedirects from "./create-redirects"
import {
DEFAULT_OPTIONS,
BUILD_HTML_STAGE,
BUILD_CSS_STAGE,
PAGE_COUNT_WARN,
} from "./constants"
const assetsManifest = {}
// 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, pages } = store.getState()
if (
pages.size > PAGE_COUNT_WARN &&
(pluginOptions.mergeCachingHeaders || pluginOptions.mergeLinkHeaders)
) {
reporter.warn(
`[gatsby-plugin-netlify] Your site has ${pages.size} pages, which means that the generated headers file could become very large. Consider disabling "mergeCachingHeaders" and "mergeLinkHeaders" in your plugin config`
)
}
reporter.info(`[gatsby-plugin-netlify] Creating SSR redirects...`)
let count = 0
const rewrites = []
Array.from(pages.values()).forEach(page => {
const { mode, matchPath, path } = page
if (mode === `SSR`) {
count++
rewrites.push(
{
fromPath: matchPath ?? path,
toPath: `/.netlify/functions/__ssr`,
},
{
fromPath: generatePageDataPath(`/`, matchPath ?? path),
toPath: `/.netlify/functions/__ssr`,
}
)
}
if (
pluginOptions.generateMatchPathRewrites &&
page.matchPath !== page.path
) {
rewrites.push({
fromPath: page.matchPath,
toPath: page.path,
})
}
})
reporter.info(
`[gatsby-plugin-netlify] Created ${count} SSR redirect${
count === 1 ? `` : `s`
}...`
)
await Promise.all([
buildHeadersProgram(pluginData, pluginOptions, reporter),
createRedirects(pluginData, redirects, rewrites),
])
}
const MATCH_ALL_KEYS = /^/
const pluginOptionsSchema = function ({ Joi }) {
// headers is a specific type used by Netlify: https://www.gatsbyjs.com/plugins/gatsby-plugin-netlify/#headers
const headersSchema = Joi.object()
.pattern(MATCH_ALL_KEYS, Joi.array().items(Joi.string()))
.description(`Add more Netlify 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