-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-node.js
40 lines (34 loc) · 954 Bytes
/
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
const fs = require(`fs`)
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
const NUM_IMAGES = parseInt(process.env.NUM_IMAGES || 100, 10)
const urls = JSON.parse(
fs.readFileSync(`${__dirname}/urls.json`, `utf-8`)
).slice(0, NUM_IMAGES)
exports.sourceNodes = ({ actions, createNodeId, store, cache }) =>
Promise.all(
urls.map(async url => {
let fileNode
const nodeId = createNodeId(url)
try {
fileNode = await createRemoteFileNode({
url: url,
parentNodeId: nodeId,
cache,
createNode: actions.createNode,
createNodeId,
})
} catch (e) {
console.log(e)
// Ignore
}
const node = {
id: nodeId,
remoteUrl: url,
internal: { type: `RemoteImage`, contentDigest: url },
}
if (fileNode) {
node.file___NODE = fileNode.id
}
actions.createNode(node)
})
)