/
gatsby-node.js
234 lines (200 loc) · 6.36 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
const path = require(`path`)
const fs = require(`fs-extra`)
let runNumber = parseInt(process.env.ARTIFACTS_RUN_SETUP, 10) || 1
let isFirstRun = runNumber === 1
let changedBrowserCompilationHash
let changedSsrCompilationHash
let regeneratedPages = []
let deletedPages = []
exports.onPreInit = ({ emitter }) => {
emitter.on(`SET_WEBPACK_COMPILATION_HASH`, action => {
changedBrowserCompilationHash = action.payload
})
emitter.on(`SET_SSR_WEBPACK_COMPILATION_HASH`, action => {
changedSsrCompilationHash = action.payload
})
emitter.on(`HTML_GENERATED`, action => {
for (const path of action.payload) {
regeneratedPages.push(path)
}
})
emitter.on(`HTML_REMOVED`, action => {
deletedPages.push(action.payload)
})
}
let previouslyCreatedNodes = new Map()
let didRemoveTrailingSlashForTestedPage = false
exports.sourceNodes = ({
actions,
createContentDigest,
webhookBody,
reporter,
}) => {
if (webhookBody && webhookBody.runNumber) {
runNumber = webhookBody.runNumber
isFirstRun = runNumber === 1
}
reporter.info(`Using test setup #${runNumber}`)
const currentlyCreatedNodes = new Map()
function createNodeHelper(type, nodePartial) {
const node = {
template: `default`,
...nodePartial,
internal: {
type,
contentDigest: createContentDigest(nodePartial),
},
}
actions.createNode(node)
currentlyCreatedNodes.set(node.id, node)
}
// used to create pages and queried by them
createNodeHelper(`DepPageQuery`, {
id: `page-query-stable`,
label: `Stable (always created)`,
})
// used to create pages and queried by them
createNodeHelper(`DepPageQuery`, {
id: `page-query-stable-alternative`,
label: `Stable (always created)`,
// this is just so we always create at least one page with alternative template to avoid changing compilation hash (async-requires change) (sigh: we should be able to mark module as template even without any pages to avoid that warning)
template: `alternative`,
})
createNodeHelper(`DepPageQuery`, {
id: `page-query-template-change`,
label: `Stable (always created)`,
// use default template in first run, but alternative in next ones
template: runNumber <= 1 ? `default` : `alternative`,
})
createNodeHelper(`DepPageQuery`, {
id: `page-query-changing-but-not-invalidating-html`,
label: `Stable (always created)`,
buildRun: runNumber, // important for test setup - this will invalidate page queries, but shouldn't invalidate html (if it's not queried)
})
createNodeHelper(`DepPageQuery`, {
id: `page-query-changing-data-but-not-id`,
label: `This is${isFirstRun ? `` : ` not`} a first run`, // this will be queried - we want to invalidate html here
})
createNodeHelper(`DepPageQuery`, {
id: `page-query-dynamic-${runNumber}`, // this should cause different page path
label: `This is run number {$runNumber}`,
})
// used by static queries
createNodeHelper(`DepStaticQuery`, {
id: `static-query-stable`,
label: `Stable (always created)`,
})
createNodeHelper(`DepStaticQuery`, {
id: `static-query-changing-but-not-invalidating-html`,
label: `Stable (always created)`,
buildRun: runNumber, // important for test setup - this will invalidate static query, but shouldn't invalidate html (if it's not queried)
})
createNodeHelper(`DepStaticQuery`, {
id: `static-query-changing-data-but-not-id`,
label: `This is${isFirstRun ? `` : ` not`} a first run`, // this will be queried - we want to invalidate html here
})
for (const prevNode of previouslyCreatedNodes.values()) {
if (!currentlyCreatedNodes.has(prevNode.id)) {
actions.deleteNode({ node: prevNode })
}
}
previouslyCreatedNodes = currentlyCreatedNodes
}
exports.createPages = async ({ actions, graphql }) => {
// testing if expected html/page-data files exist OR don't exist (if stale artifacts are removed)
function createPageHelper(dummyId) {
actions.createPage({
path: `/stale-pages/${dummyId}`,
component: require.resolve(`./src/templates/dummy`),
context: {
dummyId,
},
})
}
// stable page that always gets created
createPageHelper(`stable`)
if (isFirstRun) {
// page exists only in first run
createPageHelper(`only-in-first`)
} else {
// page exists in any run other than first
createPageHelper(`only-not-in-first`)
}
createPageHelper(
`sometimes-i-have-trailing-slash-sometimes-i-dont${
runNumber % 2 === 0 ? `/` : ``
}`
)
const { data } = await graphql(`
{
allDepPageQuery {
nodes {
id
template
}
}
}
`)
for (const depPageQueryNode of data.allDepPageQuery.nodes) {
actions.createPage({
path: `/${depPageQueryNode.id}/`,
component:
depPageQueryNode.template === `alternative`
? require.resolve(`./src/templates/deps-page-query-alternative`)
: require.resolve(`./src/templates/deps-page-query`),
context: {
id: depPageQueryNode.id,
},
})
}
}
exports.onPreBuild = () => {
console.log(`[test] onPreBuild`)
changedBrowserCompilationHash = `not-changed`
changedSsrCompilationHash = `not-changed`
regeneratedPages = []
deletedPages = []
}
let counter = 1
exports.onPostBuild = async ({ graphql }) => {
console.log(`[test] onPostBuild`)
if (!didRemoveTrailingSlashForTestedPage) {
throw new Error(
`Test setup failed - didn't remove trailing slash for /pages-that-will-have-trailing-slash-removed/ page`
)
}
const { data } = await graphql(`
{
allSitePage(filter: { path: { ne: "/dev-404-page/" } }) {
nodes {
path
}
}
}
`)
fs.writeJSONSync(
path.join(
process.cwd(),
`.cache`,
`build-manifest-for-test-${counter++}.json`
),
{
allPages: data.allSitePage.nodes.map(node => node.path),
changedBrowserCompilationHash,
changedSsrCompilationHash,
generated: regeneratedPages,
removed: deletedPages,
}
)
}
// simulating "gatsby-plugin-remove-trailing-slashes" scenario
exports.onCreatePage = ({ page, actions }) => {
if (page.path === `/page-that-will-have-trailing-slash-removed/`) {
actions.deletePage(page)
actions.createPage({
...page,
path: `/page-that-will-have-trailing-slash-removed`,
})
didRemoveTrailingSlashForTestedPage = true
}
}