-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
actions.js
973 lines (883 loc) · 27.2 KB
/
actions.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
// @flow
import Joi from "joi"
import chalk from "chalk"
const _ = require(`lodash`)
const { bindActionCreators } = require(`redux`)
const { stripIndent } = require(`common-tags`)
const report = require(`gatsby-cli/lib/reporter`)
const glob = require(`glob`)
const path = require(`path`)
const fs = require(`fs`)
const { joinPath } = require(`../utils/path`)
const { hasNodeChanged, getNode } = require(`./index`)
const { trackInlineObjectsInRootNode } = require(`../schema/node-tracking`)
const { store } = require(`./index`)
import * as joiSchemas from "../joi-schemas/joi"
import { generateComponentChunkName } from "../utils/js-chunk-names"
const actions = {}
const findChildrenRecursively = (children = []) => {
children = children.concat(
...children.map(child => {
const newChildren = getNode(child).children
if (_.isArray(newChildren) && newChildren.length > 0) {
return findChildrenRecursively(newChildren)
} else {
return []
}
})
)
return children
}
type Job = {
id: string,
}
type PageInput = {
path: string,
component: string,
layout?: string,
context?: Object,
}
type LayoutInput = {
id?: string,
machineId?: string,
component: string,
layout?: string,
context?: Object,
}
type Page = {
path: string,
matchPath: ?string,
component: string,
context: Object,
internalComponentName: string,
jsonName: string,
componentChunkName: string,
layout: ?string,
updatedAt: number,
}
type Layout = {
id: any,
context: Object,
component: string,
componentWrapperPath: string,
componentChunkName: string,
internalComponentName: string,
jsonName: string,
isLayout: true,
}
type Plugin = {
name: string,
}
/**
* Delete a page
* @param {Object} page a page object with at least the path set
* @param {string} page.path The path of the page
* @param {string} page.component The absolute path to the page component
* @example
* deletePage(page)
*/
actions.deletePage = (page: PageInput) => {
return {
type: `DELETE_PAGE`,
payload: page,
}
}
const pascalCase = _.flow(_.camelCase, _.upperFirst)
const hasWarnedForPageComponent = new Set()
/**
* Create a page. See [the guide on creating and modifying pages](/docs/creating-and-modifying-pages/)
* for detailed documenation about creating pages.
* @param {Object} page a page object
* @param {string} page.path Any valid URL. Must start with a forward slash
* @param {string} page.component The absolute path to the component for this page
* @param {string} page.layout The name of the layout for this page. By default
* `'index'` layout is used
* @param {Object} page.context Context data for this page. Passed as props
* to the component `this.props.pathContext` as well as to the graphql query
* as graphql arguments.
* @example
* createPage({
* path: `/my-sweet-new-page/`,
* component: path.resolve(`./src/templates/my-sweet-new-page.js`),
* // If you have a layout component at src/layouts/blog-layout.js
* layout: `blog-layout`,
* // The context is passed as props to the component as well
* // as into the component's GraphQL query.
* context: {
* id: `123456`,
* },
* })
*/
actions.createPage = (page: PageInput, plugin?: Plugin, traceId?: string) => {
let noPageOrComponent = false
let name = `The plugin "${plugin.name}"`
if (plugin.name === `default-site-plugin`) {
name = `Your site's "gatsby-node.js"`
}
if (!page.path) {
const message = `${name} must set the page path when creating a page`
// Don't log out when testing
if (process.env.NODE_ENV !== `test`) {
console.log(chalk.bold.red(message))
console.log(``)
console.log(page)
} else {
return message
}
noPageOrComponent = true
}
// Validate that the context object doesn't overlap with any core page fields
// as this will cause trouble when running graphql queries.
if (_.isObject(page.context)) {
const reservedFields = [
`path`,
`matchPath`,
`component`,
`componentChunkName`,
`pluginCreator___NODE`,
`pluginCreatorName`,
]
const invalidFields = Object.keys(_.pick(page.context, reservedFields))
const singularMessage = `${name} used a reserved field name in the context object when creating a page:`
const pluralMessage = `${name} used reserved field names in the context object when creating a page:`
if (invalidFields.length > 0) {
const error = `${
invalidFields.length === 1 ? singularMessage : pluralMessage
}
${invalidFields.map(f => ` * "${f}"`).join(`\n`)}
${JSON.stringify(page, null, 4)}
Data in "context" is passed to GraphQL as potential arguments when running the
page query.
When arguments for GraphQL are constructed, the context object is combined with
the page object so *both* page object and context data are available as
arguments. So you don't need to add the page "path" to the context as it's
already available in GraphQL. If a context field duplicates a field already
used by the page object, this can break functionality within Gatsby so must be
avoided.
Please choose another name for the conflicting fields.
The following fields are used by the page object and should be avoided.
${reservedFields.map(f => ` * "${f}"`).join(`\n`)}
`
if (process.env.NODE_ENV === `test`) {
return error
// Only error if the context version is different than the page
// version. People in v1 often thought that they needed to also pass
// the path to context for it to be available in GraphQL
} else if (invalidFields.some(f => page.context[f] !== page[f])) {
report.panic(error)
} else {
if (!hasWarnedForPageComponent.has(page.component)) {
report.warn(error)
hasWarnedForPageComponent.add(page.component)
}
}
}
}
// Don't check if the component exists during tests as we use a lot of fake
// component paths.
if (process.env.NODE_ENV !== `test`) {
if (!fs.existsSync(page.component)) {
const message = `${name} created a page with a component that doesn't exist`
console.log(``)
console.log(chalk.bold.red(message))
console.log(``)
console.log(page)
noPageOrComponent = true
}
}
if (!page.component || !path.isAbsolute(page.component)) {
const message = `${name} must set the absolute path to the page component when create creating a page`
// Don't log out when testing
if (process.env.NODE_ENV !== `test`) {
console.log(``)
console.log(chalk.bold.red(message))
console.log(``)
console.log(page)
} else {
return message
}
noPageOrComponent = true
}
if (noPageOrComponent) {
console.log(``)
console.log(
`See the documentation for createPage https://www.gatsbyjs.org/docs/bound-action-creators/#createPage`
)
console.log(``)
process.exit(1)
}
let jsonName = `${_.kebabCase(page.path)}.json`
let internalComponentName = `Component${pascalCase(page.path)}`
if (jsonName === `.json`) {
jsonName = `index.json`
internalComponentName = `ComponentIndex`
}
let layout = page.layout || null
// If no layout is set we try fallback to `/src/layouts/index`.
if (
!layout &&
glob.sync(
joinPath(store.getState().program.directory, `src/layouts/index.*`)
).length
) {
layout = `index`
}
let internalPage: Page = {
layout,
jsonName,
internalComponentName,
path: page.path,
matchPath: page.matchPath,
component: page.component,
componentChunkName: generateComponentChunkName(page.component),
// Ensure the page has a context object
context: page.context || {},
updatedAt: Date.now(),
}
// If the path doesn't have an initial forward slash, add it.
if (internalPage.path[0] !== `/`) {
internalPage.path = `/${internalPage.path}`
}
const result = Joi.validate(internalPage, joiSchemas.pageSchema)
if (result.error) {
console.log(chalk.blue.bgYellow(`The upserted page didn't pass validation`))
console.log(chalk.bold.red(result.error))
console.log(internalPage)
return null
}
// Validate that the page component imports React and exports something
// (hopefully a component).
if (!internalPage.component.includes(`/.cache/`)) {
const fileContent = fs.readFileSync(internalPage.component, `utf-8`)
let notEmpty = true
let includesDefaultExport = true
if (fileContent === ``) {
notEmpty = false
}
if (
!fileContent.includes(`export default`) &&
!fileContent.includes(`module.exports`) &&
!fileContent.includes(`exports.default`)
) {
includesDefaultExport = false
}
if (!notEmpty || !includesDefaultExport) {
const relativePath = path.relative(
store.getState().program.directory,
internalPage.component
)
if (!notEmpty) {
console.log(``)
console.log(
`You have an empty file in the "src/pages" directory at "${relativePath}". Please remove it or make it a valid component`
)
console.log(``)
// TODO actually do die during builds.
// process.exit(1)
}
if (!includesDefaultExport) {
console.log(``)
console.log(
`The page component must export a React component for it to be valid`
)
console.log(``)
}
// TODO actually do die during builds.
// process.exit(1)
}
}
return {
type: `CREATE_PAGE`,
plugin,
traceId,
payload: internalPage,
}
}
/**
* Delete a layout
* @param {string} layout a layout object with at least the name set
* @example
* deleteLayout(layout)
*/
actions.deleteLayout = (layout: Layout, plugin?: Plugin) => {
return {
type: `DELETE_LAYOUT`,
payload: layout,
}
}
/**
* Create a layout. Generally layouts are created automatically by placing a
* React component in the `src/layouts/` directory. This action should be used
* if loading layouts from an NPM package or from a non-standard location.
* @param {Object} layout a layout object
* @param {string} layout.component The absolute path to the component for this layout
* @example
* createLayout({
* component: path.resolve(`./src/templates/myNewLayout.js`),
* id: 'custom-id', // If no id is provided, the filename will be used as id.
* context: {
* title: `My New Layout`
* }
* })
*/
actions.createLayout = (
layout: LayoutInput,
plugin?: Plugin,
traceId?: string
) => {
let id = layout.id || path.parse(layout.component).name
// Add a "machine" id as a universal ID to differentiate layout from
// page components.
const machineId = `layout---${id}`
let componentWrapperPath = joinPath(
store.getState().program.directory,
`.cache`,
`layouts`,
`${id}.js`
)
let internalLayout: Layout = {
id,
machineId,
componentWrapperPath,
isLayout: true,
jsonName: `layout-${_.kebabCase(id)}.json`,
internalComponentName: `Component-layout-${pascalCase(id)}`,
component: layout.component,
componentChunkName: generateComponentChunkName(layout.component),
// Ensure the page has a context object
context: layout.context || {},
}
const result = Joi.validate(internalLayout, joiSchemas.layoutSchema)
if (result.error) {
console.log(
chalk.blue.bgYellow(`The upserted layout didn't pass validation`)
)
console.log(chalk.bold.red(result.error))
console.log(internalLayout)
return null
}
return {
type: `CREATE_LAYOUT`,
plugin,
traceId,
payload: internalLayout,
}
}
/**
* Delete a node
* @param {string} nodeId a node id
* @param {object} node the node object
* @example
* deleteNode(node.id, node)
*/
actions.deleteNode = (nodeId: string, node: any, plugin: Plugin) => {
// Also delete any nodes transformed from this one.
let deleteDescendantsActions
const descendantNodes = findChildrenRecursively(node.children)
if (descendantNodes.length > 0) {
deleteDescendantsActions = descendantNodes.map(n =>
actions.deleteNode(n, getNode(n), plugin)
)
}
const deleteAction = {
type: `DELETE_NODE`,
plugin,
node,
payload: nodeId,
}
if (deleteDescendantsActions) {
return [...deleteDescendantsActions, deleteAction]
} else {
return deleteAction
}
}
/**
* Batch delete nodes
* @param {Array} nodes an array of node ids
* @example
* deleteNodes([`node1`, `node2`])
*/
actions.deleteNodes = (nodes: any[], plugin: Plugin) => {
// Also delete any nodes transformed from these.
const descendantNodes = _.flatten(
nodes.map(n => findChildrenRecursively(getNode(n).children))
)
let deleteDescendantsActions
if (descendantNodes.length > 0) {
deleteDescendantsActions = descendantNodes.map(n =>
actions.deleteNode(n, getNode(n), plugin)
)
}
const deleteNodesAction = {
type: `DELETE_NODES`,
plugin,
payload: nodes,
}
if (deleteDescendantsActions) {
return [...deleteDescendantsActions, deleteNodesAction]
} else {
return deleteNodesAction
}
}
const typeOwners = {}
/**
* Create a new node.
* @param {Object} node a node object
* @param {string} node.id The node's ID. Must be globally unique.
* @param {string} node.parent The ID of the parent's node. If the node is
* derived from another node, set that node as the parent. Otherwise it can
* just be an empty string.
* @param {Array} node.children An array of children node IDs. If you're
* creating the children nodes while creating the parent node, add the
* children node IDs here directly. If you're adding a child node to a
* parent node created by a plugin, you can't mutate this value directly
* to add your node id, instead use the action creator `createParentChildLink`.
* @param {Object} node.internal node fields that aren't generally
* interesting to consumers of node data but are very useful for plugin writers
* and Gatsby core.
* @param {string} node.internal.mediaType An optional field to indicate to
* transformer plugins that your node has raw content they can transform.
* Use either an official media type (we use mime-db as our source
* (https://www.npmjs.com/package/mime-db) or a made-up one if your data
* doesn't fit in any existing bucket. Transformer plugins use node media types
* for deciding if they should transform a node into a new one. E.g.
* markdown transformers look for media types of
* `text/markdown`.
* @param {string} node.internal.type An arbitrary globally unique type
* choosen by the plugin creating the node. Should be descriptive of the
* node as the type is used in forming GraphQL types so users will query
* for nodes based on the type choosen here. Nodes of a given type can
* only be created by one plugin.
* @param {string} node.internal.content An optional field. The raw content
* of the node. Can be excluded if it'd require a lot of memory to load in
* which case you must define a `loadNodeContent` function for this node.
* @param {string} node.internal.contentDigest the digest for the content
* of this node. Helps Gatsby avoid doing extra work on data that hasn't
* changed.
* @example
* createNode({
* // Data for the node.
* field1: `a string`,
* field2: 10,
* field3: true,
* ...arbitraryOtherData,
*
* // Required fields.
* id: `a-node-id`,
* parent: `the-id-of-the-parent-node`, // or null if it's a source node without a parent
* children: [],
* internal: {
* type: `CoolServiceMarkdownField`,
* contentDigest: crypto
* .createHash(`md5`)
* .update(JSON.stringify(fieldData))
* .digest(`hex`),
* mediaType: `text/markdown`, // optional
* content: JSON.stringify(fieldData), // optional
* }
* })
*/
actions.createNode = (node: any, plugin?: Plugin, traceId?: string) => {
if (!_.isObject(node)) {
return console.log(
chalk.bold.red(
`The node passed to the "createNode" action creator must be an object`
)
)
}
// Ensure the new node has an internals object.
if (!node.internal) {
node.internal = {}
}
// Tell user not to set the owner name themself.
if (node.internal.owner) {
console.log(JSON.stringify(node, null, 4))
console.log(
chalk.bold.red(
`The node internal.owner field is set automatically by Gatsby and not by plugin`
)
)
process.exit(1)
}
// Add the plugin name to the internal object.
if (plugin) {
node.internal.owner = plugin.name
}
const result = Joi.validate(node, joiSchemas.nodeSchema)
if (result.error) {
console.log(chalk.bold.red(`The new node didn't pass validation`))
console.log(chalk.bold.red(result.error))
console.log(node)
return { type: `VALIDATION_ERROR`, error: true }
}
// Ensure node isn't directly setting fields.
if (node.fields) {
throw new Error(
stripIndent`
Plugins creating nodes can not set data on the reserved field "fields"
as this is reserved for plugins which wish to extend your nodes.
If your plugin didn't add "fields" you're probably seeing this
error because you're reusing an old node object.
Node:
${JSON.stringify(node, null, 4)}
Plugin that created the node:
${JSON.stringify(plugin, null, 4)}
`
)
}
trackInlineObjectsInRootNode(node)
const oldNode = getNode(node.id)
// Ensure the plugin isn't creating a node type owned by another
// plugin. Type "ownership" is first come first served.
if (plugin) {
let pluginName = plugin.name
if (!typeOwners[node.internal.type])
typeOwners[node.internal.type] = pluginName
else if (typeOwners[node.internal.type] !== pluginName)
throw new Error(stripIndent`
The plugin "${pluginName}" created a node of a type owned by another plugin.
The node type "${node.internal.type}" is owned by "${
typeOwners[node.internal.type]
}".
If you copy and pasted code from elsewhere, you'll need to pick a new type name
for your new node(s).
The node object passed to "createNode":
${JSON.stringify(node, null, 4)}
The plugin creating the node:
${JSON.stringify(plugin, null, 4)}
`)
// If the node has been created in the past, check that
// the current plugin is the same as the previous.
if (oldNode && oldNode.internal.owner !== pluginName) {
throw new Error(
stripIndent`
Nodes can only be updated by their owner. Node "${node.id}" is
owned by "${oldNode.internal.owner}" and another plugin "${pluginName}"
tried to update it.
`
)
}
}
let deleteAction
let updateNodeAction
// Check if the node has already been processed.
if (oldNode && !hasNodeChanged(node.id, node.internal.contentDigest)) {
updateNodeAction = {
type: `TOUCH_NODE`,
plugin,
traceId,
payload: node.id,
}
} else {
// Remove any previously created descendant nodes as they're all due
// to be recreated.
if (oldNode) {
const descendantNodes = findChildrenRecursively(oldNode.children)
if (descendantNodes.length > 0) {
deleteAction = actions.deleteNodes(descendantNodes)
}
}
updateNodeAction = {
type: `CREATE_NODE`,
plugin,
traceId,
payload: node,
}
}
if (deleteAction) {
return [deleteAction, updateNodeAction]
} else {
return updateNodeAction
}
}
/**
* "Touch" a node. Tells Gatsby a node still exists and shouldn't
* be garbage collected. Primarily useful for source plugins fetching
* nodes from a remote system that can return only nodes that have
* updated. The source plugin then touches all the nodes that haven't
* updated but still exist so Gatsby knows to keep them.
* @param {string} nodeId The id of a node.
* @example
* touchNode(`a-node-id`)
*/
actions.touchNode = (nodeId: string, plugin?: Plugin) => {
return {
type: `TOUCH_NODE`,
plugin,
payload: nodeId,
}
}
type CreateNodeInput = {
node: Object,
fieldName?: string,
fieldValue?: string,
name?: string,
value: any,
}
/**
* Extend another node. The new node field is placed under the `fields`
* key on the extended node object.
*
* Once a plugin has claimed a field name the field name can't be used by
* other plugins. Also since nodes are immutable, you can't mutate the node
* directly. So to extend another node, use this.
* @param {Object} $0
* @param {Object} $0.node the target node object
* @param {string} $0.fieldName [deprecated] the name for the field
* @param {string} $0.fieldValue [deprecated] the value for the field
* @param {string} $0.name the name for the field
* @param {string} $0.value the value for the field
* @example
* createNodeField({
* node,
* name: `happiness`,
* value: `is sweet graphql queries`
* })
*
* // The field value is now accessible at node.fields.happiness
*/
actions.createNodeField = (
{ node, name, value, fieldName, fieldValue }: CreateNodeInput,
plugin: Plugin,
traceId?: string
) => {
if (fieldName) {
console.warn(
`Calling "createNodeField" with "fieldName" is deprecated. Use "name" instead`
)
if (!name) {
name = fieldName
}
}
if (fieldValue) {
console.warn(
`Calling "createNodeField" with "fieldValue" is deprecated. Use "value" instead`
)
if (!value) {
value = fieldValue
}
}
// Ensure required fields are set.
if (!node.internal.fieldOwners) {
node.internal.fieldOwners = {}
}
if (!node.fields) {
node.fields = {}
}
// Check that this field isn't owned by another plugin.
const fieldOwner = node.internal.fieldOwners[name]
if (fieldOwner && fieldOwner !== plugin.name) {
throw new Error(
stripIndent`
A plugin tried to update a node field that it doesn't own:
Node id: ${node.id}
Plugin: ${plugin.name}
name: ${name}
value: ${value}
`
)
}
// Update node
node.fields[name] = value
node.internal.fieldOwners[name] = plugin.name
return {
type: `ADD_FIELD_TO_NODE`,
plugin,
traceId,
payload: node,
}
}
/**
* Creates a link between a parent and child node. This is used when you
* transform content from a node creating a new child node. You need to add
* this new child node to the `children` array of the parent but since you
* don't have direct access to the immutable parent node, use this action
* instead.
* @param {Object} $0
* @param {Object} $0.parent the parent node object
* @param {Object} $0.child the child node object
* @example
* createParentChildLink({ parent: parentNode, child: childNode })
*/
actions.createParentChildLink = (
{ parent, child }: { parent: any, child: any },
plugin?: Plugin
) => {
// Update parent
parent.children.push(child.id)
parent.children = _.uniq(parent.children)
return {
type: `ADD_CHILD_NODE_TO_PARENT_NODE`,
plugin,
payload: parent,
}
}
/**
* Create a dependency between a page and data. Probably for
* internal use only.
* @param {Object} $0
* @param {string} $0.path the path to the page
* @param {string} $0.nodeId A node ID
* @param {string} $0.connection A connection type
* @private
*/
actions.createPageDependency = (
{
path,
nodeId,
connection,
}: { path: string, nodeId: string, connection: string },
plugin: string = ``
) => {
return {
type: `CREATE_COMPONENT_DEPENDENCY`,
plugin,
payload: {
path,
nodeId,
connection,
},
}
}
/**
* Delete dependencies between an array of pages and data. Probably for
* internal use only. Used when deleting pages.
* @param {Array} paths the paths to delete.
* @private
*/
actions.deleteComponentsDependencies = (paths: string[]) => {
return {
type: `DELETE_COMPONENTS_DEPENDENCIES`,
payload: {
paths,
},
}
}
/**
* When the query watcher extracts a graphq query, it calls
* this to store the query with its component.
* @private
*/
actions.replaceComponentQuery = ({
query,
componentPath,
}: {
query: string,
componentPath: string,
}) => {
return {
type: `REPLACE_COMPONENT_QUERY`,
payload: {
query,
componentPath,
},
}
}
/**
* Create a "job". This is a long-running process that are generally
* started as side-effects to GraphQL queries.
* [`gatsby-plugin-sharp`](/packages/gatsby-plugin-sharp/) uses this for
* example.
*
* Gatsby doesn't finish its bootstrap until all jobs are ended.
* @param {Object} job A job object with at least an id set
* @param {id} job.id The id of the job
* @example
* createJob({ id: `write file id: 123`, fileName: `something.jpeg` })
*/
actions.createJob = (job: Job, plugin?: ?Plugin = null) => {
return {
type: `CREATE_JOB`,
plugin,
payload: job,
}
}
/**
* Set (update) a "job". Sometimes on really long running jobs you want
* to update the job as it continues.
*
* @param {Object} job A job object with at least an id set
* @param {id} job.id The id of the job
* @example
* setJob({ id: `write file id: 123`, progress: 50 })
*/
actions.setJob = (job: Job, plugin?: ?Plugin = null) => {
return {
type: `SET_JOB`,
plugin,
payload: job,
}
}
/**
* End a "job".
*
* Gatsby doesn't finish its bootstrap until all jobs are ended.
* @param {Object} job A job object with at least an id set
* @param {id} job.id The id of the job
* @example
* endJob({ id: `write file id: 123` })
*/
actions.endJob = (job: Job, plugin?: ?Plugin = null) => {
return {
type: `END_JOB`,
plugin,
payload: job,
}
}
/**
* Set plugin status. A plugin can use this to save status keys e.g. the last
* it fetched something. These values are persisted between runs of Gatsby.
*
* @param {Object} status An object with arbitrary values set
* @example
* setPluginStatus({ lastFetched: Date.now() })
*/
actions.setPluginStatus = (
status: { [key: string]: mixed },
plugin: Plugin
) => {
return {
type: `SET_PLUGIN_STATUS`,
plugin,
payload: status,
}
}
/**
* Create a redirect from one page to another. Server redirects don't work out
* of the box. You must have a plugin setup to integrate the redirect data with
* your hosting technology e.g. the [Netlify
* plugin](/packages/gatsby-plugin-netlify/)).
*
* @param {Object} redirect Redirect data
* @param {string} redirect.fromPath Any valid URL. Must start with a forward slash
* @param {boolean} redirect.isPermanent This is a permanent redirect; defaults to temporary
* @param {string} redirect.toPath URL of a created page (see `createPage`)
* @param {boolean} redirect.redirectInBrowser Redirects are generally for redirecting legacy URLs to their new configuration. If you can't update your UI for some reason, set `redirectInBrowser` to true and Gatsby will handle redirecting in the client as well.
* @example
* createRedirect({ fromPath: '/old-url', toPath: '/new-url', isPermanent: true })
* createRedirect({ fromPath: '/url', toPath: '/zn-CH/url', Language: 'zn' })
*/
actions.createRedirect = ({
fromPath,
isPermanent = false,
redirectInBrowser = false,
toPath,
...rest
}) => {
let pathPrefix = ``
if (store.getState().program.prefixPaths) {
pathPrefix = store.getState().config.pathPrefix
}
return {
type: `CREATE_REDIRECT`,
payload: {
fromPath: `${pathPrefix}${fromPath}`,
isPermanent,
redirectInBrowser,
toPath: `${pathPrefix}${toPath}`,
...rest,
},
}
}
exports.actions = actions
exports.boundActionCreators = bindActionCreators(actions, store.dispatch)