/
gatsby-node.js
191 lines (168 loc) · 4.37 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
const axios = require(`axios`)
const Queue = require(`fastq`)
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
const SCREENSHOT_ENDPOINT = `https://h7iqvn4842.execute-api.us-east-2.amazonaws.com/prod/screenshot`
const LAMBDA_CONCURRENCY_LIMIT = 50
const USE_PLACEHOLDER_IMAGE = process.env.GATSBY_SCREENSHOT_PLACEHOLDER
const screenshotQueue = Queue.promise(worker, LAMBDA_CONCURRENCY_LIMIT)
async function worker(input) {
// maxRetries: 3, retryDelay: 1000
for (let i = 0; i < 2; i++) {
try {
return await createScreenshotNode(input)
} catch (e) {
await new Promise(resolve => setTimeout(resolve, 1000))
}
}
return await createScreenshotNode(input)
}
exports.onPreBootstrap = (
{
store,
cache,
actions,
createNodeId,
getCache,
getNode,
getNodesByType,
createContentDigest,
reporter,
},
pluginOptions
) => {
const { createNode, touchNode } = actions
const screenshotNodes = getNodesByType(`Screenshot`)
if (screenshotNodes.length === 0) {
return null
}
let anyQueued = false
// Check for updated screenshots and placeholder flag
// and prevent Gatsby from garbage collecting remote file nodes
screenshotNodes.forEach(n => {
if (
(n.expires && new Date() >= new Date(n.expires)) ||
USE_PLACEHOLDER_IMAGE !== n.usingPlaceholder
) {
anyQueued = true
// Screenshot expired, re-run Lambda
screenshotQueue.push({
url: n.url,
parent: n.parent,
store,
cache,
createNode,
createNodeId,
getCache,
parentNodeId: n.id,
createContentDigest,
reporter,
})
} else {
// Screenshot hasn't yet expired, touch the image node
// to prevent garbage collection
touchNode(getNode(n.screenshotFile___NODE))
}
})
if (!anyQueued) {
return null
}
return new Promise(resolve => {
screenshotQueue.drain = () => {
resolve()
}
})
}
function shouldOnCreateNode({ node }, pluginOptions) {
/*
* Check if node is of a type we care about, and has a url field
* (originally only checked sites.yml, hence including by default)
*/
const validNodeTypes = [`SitesYaml`].concat(pluginOptions.nodeTypes || [])
return validNodeTypes.includes(node.internal.type) && node.url
}
exports.shouldOnCreateNode = shouldOnCreateNode
exports.onCreateNode = async (
{ node, actions, store, cache, createNodeId, createContentDigest, getCache },
pluginOptions
) => {
if (!shouldOnCreateNode({ node }, pluginOptions)) {
return
}
const { createNode, createParentChildLink } = actions
try {
const screenshotNode = await screenshotQueue.push({
url: node.url,
parent: node.id,
store,
cache,
createNode,
createNodeId,
getCache,
createContentDigest,
parentNodeId: node.id,
})
createParentChildLink({
parent: node,
child: screenshotNode,
})
} catch (e) {
return
}
}
const createScreenshotNode = async ({
url,
parent,
store,
cache,
createNode,
createNodeId,
getCache,
parentNodeId,
createContentDigest,
reporter,
}) => {
try {
let fileNode
let expires
if (USE_PLACEHOLDER_IMAGE) {
const getPlaceholderFileNode = require(`./placeholder-file-node`)
fileNode = await getPlaceholderFileNode({
createNode,
createNodeId,
})
expires = new Date(2999, 1, 1).getTime()
} else {
const screenshotResponse = await axios.post(SCREENSHOT_ENDPOINT, { url })
fileNode = await createRemoteFileNode({
url: screenshotResponse.data.url,
cache,
createNode,
createNodeId,
getCache,
parentNodeId,
})
expires = screenshotResponse.data.expires
if (!fileNode) {
throw new Error(`Remote file node is null`, screenshotResponse.data.url)
}
}
const screenshotNode = {
id: createNodeId(`${parent} >>> Screenshot`),
url,
expires,
parent,
children: [],
internal: {
type: `Screenshot`,
},
screenshotFile___NODE: fileNode.id,
usingPlaceholder: USE_PLACEHOLDER_IMAGE,
}
screenshotNode.internal.contentDigest = createContentDigest(screenshotNode)
createNode(screenshotNode)
return screenshotNode
} catch (e) {
console.log(`Failed to screenshot ${url}. Retrying...`)
throw e
}
}