-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-node.js
69 lines (63 loc) · 2.27 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
const { fetchRemoteFile } = require("gatsby-core-utils/fetch-remote-file")
const { slash } = require("gatsby-core-utils")
const path = require("path")
const fs = require("fs-extra")
/** @type{import('gatsby').createSchemaCustomization} */
exports.createSchemaCustomization = ({ actions, schema, cache, reporter }) => {
actions.createTypes(
schema.buildObjectType({
name: "MyRemoteFile",
fields: {
url: "String!",
publicUrl: {
type: "String!",
async resolve(source) {
const filePath = await fetchRemoteFile({
name: path.basename(source.name, path.extname(source.name)),
ext: path.extname(source.name),
url: source.url,
directory: "./public/images",
})
const dir = path.join(global.__GATSBY.root, ".cache", "workers")
await fs.ensureDir(dir)
await fs.createFile(
`${path.join(dir, `worker-${process.env.GATSBY_WORKER_ID}`)}`
)
const workers = (await cache.get("workers")) ?? []
workers.push(process.env.GATSBY_WORKER_ID)
return `${slash(filePath.replace(/^public/, ""))}`
},
},
},
interfaces: ["Node"],
})
)
}
/** @type {imporg('gatsby').sourceNodes} */
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }) => {
const items = [
{
name: "photoA.jpg",
url: "https://images.unsplash.com/photo-1517849845537-4d257902454a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=300&q=80",
},
{
name: "photoB.jpg",
url: "https://images.unsplash.com/photo-1552053831-71594a27632d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=300&q=80",
},
{
name: "photoC.jpg",
url: "https://images.unsplash.com/photo-1561037404-61cd46aa615b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=300&q=80",
},
]
items.forEach((item, index) => {
actions.createNode({
id: createNodeId(`remote-file-${index}`),
name: item.name,
url: item.url,
internal: {
type: "MyRemoteFile",
contentDigest: createContentDigest(item.url),
},
})
})
}