Skip to content

Commit 16685a6

Browse files
authored
chore(gatsby-source-filesystem): Improve README (#37480)
1 parent 8a718e3 commit 16685a6

File tree

1 file changed

+68
-86
lines changed

1 file changed

+68
-86
lines changed

packages/gatsby-source-filesystem/README.md

+68-86
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# gatsby-source-filesystem
22

3-
A Gatsby source plugin for sourcing data into your Gatsby application from your local filesystem.
3+
A Gatsby plugin for sourcing data into your Gatsby application from your local filesystem.
44

5-
The plugin creates `File` nodes from files. The various "transformer" plugins can transform `File` nodes into various other types of data e.g. [`gatsby-transformer-json`](https://www.gatsbyjs.com/plugins/gatsby-transformer-json/) transforms JSON files into JSON data nodes and [`gatsby-transformer-remark`](https://www.gatsbyjs.com/plugins/gatsby-transformer-remark/) transforms markdown files into `MarkdownRemark` nodes from which you can query an HTML representation of the markdown.
5+
The plugin creates `File` nodes from files. The various [transformer plugins](https://www.gatsbyjs.com/plugins/?=gatsby-transformer) can transform `File` nodes into other types of data e.g. [`gatsby-transformer-json`](https://www.gatsbyjs.com/plugins/gatsby-transformer-json/) transforms JSON files into `JSON` nodes and [`gatsby-transformer-remark`](https://www.gatsbyjs.com/plugins/gatsby-transformer-remark/) transforms markdown files into `MarkdownRemark` nodes.
66

77
## Install
88

@@ -12,7 +12,7 @@ npm install gatsby-source-filesystem
1212

1313
## How to use
1414

15-
You can have multiple instances of this plugin to read source nodes from different locations on your filesystem. Be sure to give each instance a unique `name`.
15+
You can have multiple instances of this plugin in your `gatsby-config` to read files from different locations on your filesystem. Be sure to give each instance a unique `name`.
1616

1717
```js:title=gatsby-config.js
1818
module.exports = {
@@ -41,6 +41,8 @@ module.exports = {
4141
}
4242
```
4343

44+
In the above example every file under `src/pages` and `src/data` will be made available as a `File` node inside GraphQL. You don't need to set up another instance of `gatsby-source-filesystem` for e.g. `src/data/images` (since those files are already sourced). However, if you want to be able to filter your files you can set up a new instance and later use the `sourceInstanceName`.
45+
4446
## Options
4547

4648
### name
@@ -82,43 +84,41 @@ By default, `gatsby-source-filesystem` creates an MD5 hash of each file to deter
8284

8385
### Environment variables
8486

85-
To prevent concurrent requests overload of `processRemoteNode`, you can adjust the `200` default concurrent downloads, with `GATSBY_CONCURRENT_DOWNLOAD` environment variable.
87+
- `GATSBY_CONCURRENT_DOWNLOAD` (default: `200`). To prevent concurrent requests you can configure the concurrency of `processRemoteNode`.
8688

87-
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:
89+
If you have a spotty network or slow connection, you can adjust the retries and timeouts:
8890

89-
- `GATSBY_STALL_RETRY_LIMIT`, default: `3`
90-
- `GATSBY_STALL_TIMEOUT`, default: `30000`
91-
- `GATSBY_CONNECTION_TIMEOUT`, default: `30000`
91+
- `GATSBY_STALL_RETRY_LIMIT` (default: `3`)
92+
- `GATSBY_STALL_TIMEOUT` (default: `30000`)
93+
- `GATSBY_CONNECTION_TIMEOUT` (default: `30000`)
9294

9395
## How to query
9496

95-
You can query file nodes like the following:
97+
You can query the `File` nodes as following:
9698

9799
```graphql
98100
{
99101
allFile {
100-
edges {
101-
node {
102-
extension
103-
dir
104-
modifiedTime
105-
}
102+
nodes {
103+
extension
104+
dir
105+
modifiedTime
106106
}
107107
}
108108
}
109109
```
110110

111-
To filter by the `name` you specified in the config, use `sourceInstanceName`:
111+
Use [GraphiQL](https://www.gatsbyjs.com/docs/how-to/querying-data/running-queries-with-graphiql/) to explore all available keys.
112+
113+
To filter by the `name` you specified in the `gatsby-config`, use `sourceInstanceName`:
112114

113115
```graphql
114116
{
115117
allFile(filter: { sourceInstanceName: { eq: "data" } }) {
116-
edges {
117-
node {
118-
extension
119-
dir
120-
modifiedTime
121-
}
118+
nodes {
119+
extension
120+
dir
121+
modifiedTime
122122
}
123123
}
124124
}
@@ -128,24 +128,24 @@ To filter by the `name` you specified in the config, use `sourceInstanceName`:
128128

129129
`gatsby-source-filesystem` exports three helper functions:
130130

131-
- `createFilePath`
132-
- `createRemoteFileNode`
133-
- `createFileNodeFromBuffer`
131+
- [`createFilePath`](#createfilepath)
132+
- [`createRemoteFileNode`](#createremotefilenode)
133+
- [`createFileNodeFromBuffer`](#createfilenodefrombuffer)
134134

135-
### createFilePath
135+
### `createFilePath`
136136

137-
When building pages from files, you often want to create a URL from a file's path on the file system. E.g. if you have a markdown file at `src/content/2018-01-23-an-exploration-of-the-nature-of-reality/index.md`, you might want to turn that into a page on your site at `example.com/2018-01-23-an-exploration-of-the-nature-of-reality/`. `createFilePath` is a helper function to make this task easier.
137+
When building pages from files, you often want to create a URL from a file's path on the filesystem. For example, if you have a markdown file at `src/content/2018-01-23-my-blog-post/index.md`, you might want to turn that into a page on your site at `example.com/blog/2018-01-23-my-blog-post/`. `createFilePath` is a helper function to make this task easier.
138138

139139
```javascript
140140
createFilePath({
141141
// The node you'd like to convert to a path
142-
// e.g. from a markdown, JSON, YAML file, etc
142+
// e.g. from a markdown, JSON, YAML file, etc.
143143
node,
144144
// Method used to get a node
145145
// The parameter from `onCreateNode` should be passed in here
146146
getNode,
147147
// The base path for your files.
148-
// It is relative to the `options.path` setting in the `gatsby-source-filesystem` entries of your `gatsby-config.js`.
148+
// It is relative to the `options.path` setting in the `gatsby-source-filesystem` entries of your `gatsby-config`.
149149
// Defaults to `src/pages`. For the example above, you'd use `src/content`.
150150
basePath,
151151
// Whether you want your file paths to contain a trailing `/` slash
@@ -154,35 +154,35 @@ createFilePath({
154154
})
155155
```
156156

157-
#### Example usage
157+
#### Example
158158

159-
```javascript
159+
```js:title=gatsby-node.js
160160
const { createFilePath } = require(`gatsby-source-filesystem`)
161161

162162
exports.onCreateNode = ({ node, getNode, actions }) => {
163163
const { createNodeField } = actions
164164
// Ensures we are processing only markdown files
165165
if (node.internal.type === "MarkdownRemark") {
166-
// Use `createFilePath` to turn markdown files in our `data/faqs` directory into `/faqs/slug`
166+
// Use `createFilePath` to turn markdown files in our `src/content` directory into `/blog/slug`
167167
const relativeFilePath = createFilePath({
168168
node,
169169
getNode,
170-
basePath: "data/faqs/",
170+
basePath: "src/content",
171171
})
172172

173173
// Creates new query'able field with name of 'slug'
174174
createNodeField({
175175
node,
176176
name: "slug",
177-
value: `/faqs${relativeFilePath}`,
177+
value: `/blog${relativeFilePath}`,
178178
})
179179
}
180180
}
181181
```
182182

183-
### createRemoteFileNode
183+
### `createRemoteFileNode`
184184

185-
When building source plugins for remote data sources such as headless CMSs, their data will often link to files stored remotely that are often convenient to download so you can work with them locally.
185+
When building source plugins for remote data sources (Headless CMSs, APIs, etc.), their data will often link to files stored remotely that are often convenient to download so you can work with them locally.
186186

187187
The `createRemoteFileNode` helper makes it easy to download remote files and add them to your site's GraphQL schema.
188188

@@ -192,80 +192,63 @@ While downloading the assets, special characters (regex: `/:|\/|\*|\?|"|<|>|\||\
192192
createRemoteFileNode({
193193
// The source url of the remote file
194194
url: `https://example.com/a-file.jpg`,
195-
196-
// The id of the parent node (i.e. the node to which the new remote File node will be linked to.
195+
// The id of the parent node (i.e. the node to which the new remote File node will be linked to)
197196
parentNodeId,
198-
199197
// Gatsby's cache which the helper uses to check if the file has been downloaded already. It's passed to all Node APIs.
200198
getCache,
201-
202199
// The action used to create nodes
203200
createNode,
204-
205201
// A helper function for creating node Ids
206202
createNodeId,
207-
208203
// OPTIONAL
209204
// Adds htaccess authentication to the download request if passed in.
210205
auth: { htaccess_user: `USER`, htaccess_pass: `PASSWORD` },
211-
212206
// OPTIONAL
213207
// Adds extra http headers to download request if passed in.
214208
httpHeaders: { Authorization: `Bearer someAccessToken` },
215-
216209
// OPTIONAL
217210
// Sets the file extension
218-
ext: ".jpg",
211+
ext: `.jpg`,
219212
})
220213
```
221214

222-
#### Example usage
215+
#### Example
223216

224-
The following example is pulled from [gatsby-source-wordpress](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-wordpress). Downloaded files are created as `File` nodes and then linked to the WordPress Media node, so it can be queried both as a regular `File` node and from the `localFile` field in the Media node.
217+
The following example is pulled from the [Preprocessing External Images guide](https://www.gatsbyjs.com/docs/how-to/images-and-media/preprocessing-external-images/). Downloaded files are created as `File` nodes and then linked to the `MarkdownRemark` node, so it can be used with e.g. [`gatsby-plugin-image`](https://www.gatsbyjs.com/docs/how-to/images-and-media/using-gatsby-plugin-image/). The file node can then be queried using GraphQL.
225218

226-
```javascript
227-
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
219+
```js:title=gatsby-node.js
220+
const { createRemoteFileNode } = require("gatsby-source-filesystem")
228221

229-
exports.downloadMediaFiles = ({
230-
nodes,
231-
getCache,
232-
createNode,
222+
exports.onCreateNode = async ({
223+
node,
224+
actions: { createNode, createNodeField },
233225
createNodeId,
234-
_auth,
226+
getCache,
235227
}) => {
236-
nodes.map(async node => {
237-
let fileNode
238-
// Ensures we are only processing Media Files
239-
// `wordpress__wp_media` is the media file type name for WordPress
240-
if (node.__type === `wordpress__wp_media`) {
241-
try {
242-
fileNode = await createRemoteFileNode({
243-
url: node.source_url,
244-
parentNodeId: node.id,
245-
getCache,
246-
createNode,
247-
createNodeId,
248-
auth: _auth,
249-
})
250-
} catch (e) {
251-
// Ignore
252-
}
253-
}
228+
// For all MarkdownRemark nodes that have a featured image url, call createRemoteFileNode
229+
if (
230+
node.internal.type === "MarkdownRemark" &&
231+
node.frontmatter.featuredImgUrl !== null
232+
) {
233+
const fileNode = await createRemoteFileNode({
234+
url: node.frontmatter.featuredImgUrl, // string that points to the URL of the image
235+
parentNodeId: node.id, // id of the parent node of the fileNode you are going to create
236+
createNode, // helper function in gatsby-node to generate the node
237+
createNodeId, // helper function in gatsby-node to generate the node id
238+
getCache,
239+
})
254240

255-
// Adds a field `localFile` to the node
256-
// ___NODE appendix tells Gatsby that this field will link to another node
241+
// if the file was created, extend the node with "localFile"
257242
if (fileNode) {
258-
node.localFile___NODE = fileNode.id
243+
createNodeField({ node, name: "localFile", value: fileNode.id })
259244
}
260-
})
245+
}
261246
}
262247
```
263248

264-
The file node can then be queried using GraphQL. See an example of this in the [gatsby-source-wordpress README](/plugins/gatsby-source-wordpress/#image-processing) where downloaded images are queried using [gatsby-transformer-sharp](/plugins/gatsby-transformer-sharp/) to use in the component [gatsby-image](/plugins/gatsby-image/).
265-
266249
#### Retrieving the remote file name and extension
267250

268-
The helper tries first to retrieve the file name and extension by parsing the url and the path provided (e.g. if the url is `https://example.com/image.jpg`, the extension will be inferred as `.jpg` and the name as `image`). If the url does not contain an extension, we use the [`file-type`](https://www.npmjs.com/package/file-type) package to infer the file type. Finally, the name and the extension _can_ be explicitly passed, like so:
251+
The helper first tries to retrieve the file name and extension by parsing the url and the path provided (e.g. if the url is `https://example.com/image.jpg`, the extension will be inferred as `.jpg` and the name as `image`). If the url does not contain an extension, `createRemoteFileNode` use the [`file-type`](https://www.npmjs.com/package/file-type) package to infer the file type. Finally, the name and the extension _can_ be explicitly passed, like so:
269252

270253
```javascript
271254
createRemoteFileNode({
@@ -276,25 +259,24 @@ createRemoteFileNode({
276259
createNode,
277260
createNodeId,
278261
// if necessary!
279-
ext: ".jpg",
280-
name: "image",
262+
ext: `.jpg`,
263+
name: `image`,
281264
})
282265
```
283266

284-
### createFileNodeFromBuffer
267+
### `createFileNodeFromBuffer`
285268

286269
When working with data that isn't already stored in a file, such as when querying binary/blob fields from a database, it's helpful to cache that data to the filesystem in order to use it with other transformers that accept files as input.
287270

288-
The `createFileNodeFromBuffer` helper accepts a `Buffer`, caches its contents to disk, and creates a file node that points to it.
271+
The `createFileNodeFromBuffer` helper accepts a `Buffer`, caches its contents to disk, and creates a `File` node that points to it.
289272

290273
The name of the file can be passed to the `createFileNodeFromBuffer` helper. If no name is given, the content hash will be used to determine the name.
291274

292-
#### Example usage
275+
#### Example
293276

294277
The following example is adapted from the source of [`gatsby-source-mysql`](https://github.com/malcolm-kee/gatsby-source-mysql):
295278

296-
```js
297-
// gatsby-node.js
279+
```js:title=gatsby-node.js
298280
const createMySqlNodes = require(`./create-nodes`)
299281

300282
exports.sourceNodes = async ({ actions, createNodeId, getCache }, config) => {

0 commit comments

Comments
 (0)