-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-node.js
123 lines (107 loc) · 3.14 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
const commandName = process.env.NODE_ENV === `development` ? `develop` : `build`
const DEFAULT_MAX_DAYS_OLD = 30
const createManifestId = nodeId => `${commandName}-${nodeId}`
exports.sourceNodes = ({ actions }) => {
// template nodes
for (let id = 1; id < 6; id++) {
const node = {
id: `${id}`,
internal: {
type: `TestNode`,
contentDigest: `${id}`,
},
}
if (id === 5) {
node.slug = `test-slug`
}
actions.createNode(node)
actions.unstable_createNodeManifest({
manifestId: createManifestId(id),
node,
})
}
// filesystem route api node
const node = {
id: `filesystem-1`,
internal: {
type: `TestFSRouteType`,
contentDigest: `1`,
},
}
actions.createNode(node)
actions.unstable_createNodeManifest({
manifestId: createManifestId(node.id),
node,
})
const today = new Date()
const nodeTooOldToGetManifestCreated = new Date(
new Date().setDate(today.getDate() - (DEFAULT_MAX_DAYS_OLD + 1))
).toISOString()
const nodeUpdated1 = {
id: `updatedAt-1`,
internal: {
type: `TestUpdatedAtType`,
contentDigest: `1`,
},
updatedAt: today.toISOString(),
}
const nodeUpdated2 = {
id: `updatedAt-2`,
internal: {
type: `TestUpdatedAtType`,
contentDigest: `1`,
},
updatedAt: nodeTooOldToGetManifestCreated,
}
actions.createNode(nodeUpdated1)
actions.createNode(nodeUpdated2)
actions.unstable_createNodeManifest({
manifestId: createManifestId(nodeUpdated1.id),
node: nodeUpdated1,
updatedAtUTC: nodeUpdated1.updatedAt,
})
actions.unstable_createNodeManifest({
manifestId: createManifestId(nodeUpdated2.id),
node: nodeUpdated2,
updatedAtUTC: nodeUpdated2.updatedAt,
})
}
/**
* The main way node manifests are mapped to pages is via the ownerNodeId argument to createPage.
* If it doesn't exist we either grab the first page with `id` in pageContext that matches to our nodeManifest's node id, or if that doesn't exist, the first page query where our nodeManifest's node id is queried. Node 2 and 3 are used to test this behaviour (in that order).
*
* Note that there's no page created for our node with an id of `4`. The Node Manifest API should account for the situation where the user didn't create a page for a node that a manifest was created for by a source plugin.
*/
exports.createPages = ({ actions }) => {
actions.createPage({
path: `one`,
ownerNodeId: `1`,
component: require.resolve(`./src/templates/one.js`),
})
actions.createPage({
path: `two`,
component: require.resolve(`./src/templates/two.js`),
context: {
id: `2`,
},
})
actions.createPage({
path: `two-alternative`,
component: require.resolve(`./src/templates/two.js`),
})
actions.createPage({
path: `three`,
component: require.resolve(`./src/templates/three.js`),
})
actions.createPage({
path: `three-alternative`,
component: require.resolve(`./src/templates/three.js`),
})
actions.createPage({
path: `slug-test-path`,
context: {
slug: `test-slug`,
},
component: require.resolve(`./src/templates/four.js`),
})
}