Skip to content

Commit 8867ba9

Browse files
committed
docs(gatsby-source-filesystem): add warning about parentNodeId for util functions
1 parent fa5679d commit 8867ba9

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

packages/gatsby-source-filesystem/README.md

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ While downloading the assets, special characters (regex: `/:|\/|\*|\?|"|<|>|\||\
166166

167167
**Warning**: Please make sure to pass in the `parentNodeId`. Otherwise if the parent of the created file node is loaded from the cache, the linked file node won't be recreated which means it will be garbage collected and your file is set to null.
168168

169-
170169
```javascript
171170
createRemoteFileNode({
172171
// The source url of the remote file
@@ -268,72 +267,70 @@ The `createFileNodeFromBuffer` helper accepts a `Buffer`, caches its contents to
268267

269268
**Warning**: Please make sure to pass in the `parentNodeId`. Otherwise if the parent of the created file node is loaded from the cache, the linked file node won't be recreated which means it will be garbage collected and your file is set to null.
270269

271-
272270
#### Example usage
273271

274272
The following example is a common case where you want to create a sharing image dynamically, instead of loading an existing image.
275273

276274
```js
277275
// gatsby-node.js
278276
exports.onCreateNode = async ({ node, actions, getCache, createNodeId }) => {
279-
const { createNode, createNodeField } = actions;
277+
const { createNode, createNodeField } = actions
280278
/**
281279
* For every incoming Markdown node we want to generate a social card
282280
* and attach it to the the node.
283281
*/
284-
if (node.internal.type === 'MarkdownRemark') {
285-
const title = node.frontmatter.title;
282+
if (node.internal.type === "MarkdownRemark") {
283+
const title = node.frontmatter.title
286284
// we need the title in order to generate anything
287285
if (!title) {
288-
return;
286+
return
289287
}
290288

291289
// this some function that generates your image as a buffer
292290
// use `node-canvas` (https://www.npmjs.com/package/canvas) or similar.
293-
const imageBuffer = await generateSomeImage(title);
291+
const imageBuffer = await generateSomeImage(title)
294292

295293
const fileNode = await createFileNodeFromBuffer({
296-
name: 'social-card',
294+
name: "social-card",
297295
buffer: imageBuffer,
298296
getCache,
299297
createNode,
300298
createNodeId,
301299
// make sure to always pass in the parent node otherwise it's lost when loaded from cache
302-
parentNodeId: node.id
303-
});
300+
parentNodeId: node.id,
301+
})
304302

305303
if (fileNode) {
306304
createNodeField({
307305
node,
308306
name: `socialCard`,
309307
value: fileNode.id,
310-
});
308+
})
311309
}
312310
}
313-
};
314-
311+
}
315312

316-
exports.createSchemaCustomization = ({ actions: { createTypes }}) => {
313+
exports.createSchemaCustomization = ({ actions: { createTypes } }) => {
317314
const typeDefs = [
318-
`type SyPersonioJob implements Node {
315+
`type MarkdownRemark implements Node {
319316
socialCardFile: File @link(from: "fields.socialCard")
320317
}`,
321-
];
322-
323-
createTypes(typeDefs);
324-
};
318+
]
325319

320+
createTypes(typeDefs)
321+
}
326322
```
327323

328324
## Troubleshooting
329325

330326
### File nodes are null after starting the server a second time
331-
In case you see yourself running `gatsby clean` frequently to files that are set to `null` you might might have forgotten to pass in `parentNodeId` to `createFileNodeFromBuffer` or `createRemoteFileNode`. Make sure you always provide a parent reference otherwise the files won't be recreated which means it will be garbage collected and show up as `null` in your queried data.
327+
328+
In case you see yourself running `gatsby clean` frequently to files that are set to `null` you might might have forgotten to pass in `parentNodeId` to `createFileNodeFromBuffer` or `createRemoteFileNode`. Make sure you always provide a parent reference otherwise the files won't be recreated which means it will be garbage collected and show up as `null` in your queried data.
332329

333330
### Spotty network
331+
334332
In case that due to spotty network, or slow connection, some remote files fail to download. Even after multiple retries and adjusting concurrent downloads, you can adjust timeout and retry settings with these environment variables:
335333

336334
- `GATSBY_STALL_RETRY_LIMIT`, default: `3`
337335
- `GATSBY_STALL_TIMEOUT`, default: `30000`
338336
- `GATSBY_CONNECTION_TIMEOUT`, default: `30000`
339-

packages/gatsby-source-filesystem/src/create-file-node-from-buffer.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,10 @@ module.exports = ({
148148
)
149149
}
150150

151-
152-
if (typeof parentNodeId === null) {
153-
console.warn(`It seems that you forgot to pass in 'parentNodeId' for a file you try to create with 'create-file-node-from-buffer'. Not doing this is causing problems as a) when the server is restarted, if the parent of the created file node is loaded from the cache, the linked file node won't be recreated which means it will be garbage collected and b) if a parent node is deleted, the linked file node won't also be deleted.`)
151+
if (parentNodeId === null) {
152+
console.warn(
153+
`It seems that you forgot to pass in 'parentNodeId' for a file you try to create with 'createFileNodeFromBuffer'. Not doing this is causing problems as a) when the server is restarted, if the parent of the created file node is loaded from the cache, the linked file node won't be recreated which means it will be garbage collected and b) if a parent node is deleted, the linked file node won't also be deleted.`
154+
)
154155
}
155156

156157
if (!buffer) {

packages/gatsby-source-filesystem/src/create-remote-file-node.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,10 @@ module.exports = function createRemoteFileNode({
237237
)
238238
}
239239

240-
if (typeof parentNodeId === null) {
241-
console.warn(`It seems that you forgot to pass in 'parentNodeId' for a file you try to create with 'create-remote-file-node'. Not doing this is causing problems as a) when the server is restarted, if the parent of the created file node is loaded from the cache, the linked file node won't be recreated which means it will be garbage collected and b) if a parent node is deleted, the linked File node won't also be deleted.`)
240+
if (parentNodeId === null) {
241+
console.warn(
242+
`It seems that you forgot to pass in 'parentNodeId' for a file you try to create with 'createRemoteFileNode'. Not doing this is causing problems as a) when the server is restarted, if the parent of the created file node is loaded from the cache, the linked file node won't be recreated which means it will be garbage collected and b) if a parent node is deleted, the linked File node won't also be deleted.`
243+
)
242244
}
243245

244246
// Check if we already requested node for this remote file

0 commit comments

Comments
 (0)