Skip to content

Commit ade9379

Browse files
committed
fix: persist nodes over reruns
1 parent e683066 commit ade9379

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

source/node.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type NormalisedEntity<E extends FullEntity = FullEntity> = E extends any
4747
export class NodeManager {
4848
private createNode: NodePluginArgs['actions']['createNode'];
4949
private deleteNode: NodePluginArgs['actions']['deleteNode'];
50+
private touchNode: NodePluginArgs['actions']['touchNode'];
5051
private createNodeId: NodePluginArgs['createNodeId'];
5152
private createContentDigest: NodePluginArgs['createContentDigest'];
5253
private cache: NodePluginArgs['cache'];
@@ -59,7 +60,7 @@ export class NodeManager {
5960
constructor(args: NodePluginArgs) {
6061
/* eslint-disable @typescript-eslint/unbound-method */
6162
const {
62-
actions: { createNode, deleteNode },
63+
actions: { createNode, deleteNode, touchNode },
6364
cache,
6465
createContentDigest,
6566
createNodeId,
@@ -70,6 +71,7 @@ export class NodeManager {
7071
this.cache = cache;
7172
this.createNode = createNode;
7273
this.deleteNode = deleteNode;
74+
this.touchNode = touchNode;
7375
this.createNodeId = createNodeId;
7476
this.createContentDigest = createContentDigest;
7577
this.reporter = reporter;
@@ -88,7 +90,7 @@ export class NodeManager {
8890

8991
// for the usage of createNode
9092
// see https://www.gatsbyjs.com/docs/reference/config-files/actions/#createNode
91-
this.addNodes(this.findNewEntities(oldMap, newMap));
93+
await this.addNodes(this.findNewEntities(oldMap, newMap));
9294
this.updateNodes(this.findUpdatedEntities(oldMap, newMap));
9395
this.removeNodes(this.findRemovedEntities(oldMap, newMap));
9496

@@ -99,9 +101,26 @@ export class NodeManager {
99101
* add new nodes
100102
* @param added new nodes to be added
101103
*/
102-
private addNodes(added: NormalisedEntity[]): void {
104+
private async addNodes(added: NormalisedEntity[]): Promise<void> {
103105
for (const entity of added) {
104-
this.createNode(this.nodifyEntity(entity));
106+
const node = this.nodifyEntity(entity);
107+
108+
// DEBT: disable a false alarm from eslint as currently Gatsby is exporting an incorrect type
109+
// this should be removed when https://github.com/gatsbyjs/gatsby/pull/32522 is merged
110+
/* eslint-disable @typescript-eslint/await-thenable */
111+
// create the node
112+
await this.createNode(node);
113+
/* eslint-enable */
114+
115+
// make sure that the node will remain in the cache
116+
this.touchNode({
117+
// since createNode mutates node, reconstruct the node input again here
118+
id: node.id,
119+
internal: {
120+
type: node.internal.type,
121+
contentDigest: node.internal.contentDigest,
122+
},
123+
});
105124
}
106125

107126
// don't be noisy if there's nothing new happen

spec/node.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,12 @@ describe('cl:NodeManager', () => {
202202
it('always keep gatsby synced', async () => {
203203
const createNode = jest.fn();
204204
const deleteNode = jest.fn();
205+
const touchNode = jest.fn();
205206
const createContentDigest = jest.fn(hashFn);
206207
const createNodeId = jest.fn((id) => id);
207208

208209
const manager = new NodeManager({
209-
actions: { createNode, deleteNode },
210+
actions: { createNode, deleteNode, touchNode },
210211
cache: caching({ store: 'memory', ttl: 0 }),
211212
createContentDigest,
212213
createNodeId,
@@ -230,10 +231,12 @@ describe('cl:NodeManager', () => {
230231
await manager.update([originalDatabase, ...originalDatabase.pages]);
231232

232233
expect(createNode).toBeCalledTimes(3);
234+
expect(touchNode).toBeCalledTimes(3);
233235
expect(deleteNode).toBeCalledTimes(0);
234236

235237
// reset
236238
createNode.mockClear();
239+
touchNode.mockClear();
237240
deleteNode.mockClear();
238241

239242
// second call with data updated
@@ -250,6 +253,7 @@ describe('cl:NodeManager', () => {
250253
});
251254
await manager.update([updatedDatabase, ...updatedDatabase.pages]);
252255
expect(createNode).toBeCalledTimes(2);
256+
expect(touchNode).toBeCalledTimes(0);
253257
expect(deleteNode).toBeCalledTimes(1);
254258
});
255259
});

0 commit comments

Comments
 (0)