You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/gatsby-source-filesystem/README.md
+68-86
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
# gatsby-source-filesystem
2
2
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.
4
4
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.
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`.
16
16
17
17
```js:title=gatsby-config.js
18
18
module.exports= {
@@ -41,6 +41,8 @@ module.exports = {
41
41
}
42
42
```
43
43
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
+
44
46
## Options
45
47
46
48
### name
@@ -82,43 +84,41 @@ By default, `gatsby-source-filesystem` creates an MD5 hash of each file to deter
82
84
83
85
### Environment variables
84
86
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`.
86
88
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:
88
90
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`)
92
94
93
95
## How to query
94
96
95
-
You can query file nodes like the following:
97
+
You can query the `File`nodes as following:
96
98
97
99
```graphql
98
100
{
99
101
allFile {
100
-
edges {
101
-
node {
102
-
extension
103
-
dir
104
-
modifiedTime
105
-
}
102
+
nodes {
103
+
extension
104
+
dir
105
+
modifiedTime
106
106
}
107
107
}
108
108
}
109
109
```
110
110
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`:
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.
138
138
139
139
```javascript
140
140
createFilePath({
141
141
// 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.
143
143
node,
144
144
// Method used to get a node
145
145
// The parameter from `onCreateNode` should be passed in here
146
146
getNode,
147
147
// 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`.
149
149
// Defaults to `src/pages`. For the example above, you'd use `src/content`.
150
150
basePath,
151
151
// Whether you want your file paths to contain a trailing `/` slash
// 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`
167
167
constrelativeFilePath=createFilePath({
168
168
node,
169
169
getNode,
170
-
basePath:"data/faqs/",
170
+
basePath:"src/content",
171
171
})
172
172
173
173
// Creates new query'able field with name of 'slug'
174
174
createNodeField({
175
175
node,
176
176
name:"slug",
177
-
value:`/faqs${relativeFilePath}`,
177
+
value:`/blog${relativeFilePath}`,
178
178
})
179
179
}
180
180
}
181
181
```
182
182
183
-
### createRemoteFileNode
183
+
### `createRemoteFileNode`
184
184
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.
186
186
187
187
The `createRemoteFileNode` helper makes it easy to download remote files and add them to your site's GraphQL schema.
188
188
@@ -192,80 +192,63 @@ While downloading the assets, special characters (regex: `/:|\/|\*|\?|"|<|>|\||\
192
192
createRemoteFileNode({
193
193
// The source url of the remote file
194
194
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)
197
196
parentNodeId,
198
-
199
197
// Gatsby's cache which the helper uses to check if the file has been downloaded already. It's passed to all Node APIs.
200
198
getCache,
201
-
202
199
// The action used to create nodes
203
200
createNode,
204
-
205
201
// A helper function for creating node Ids
206
202
createNodeId,
207
-
208
203
// OPTIONAL
209
204
// Adds htaccess authentication to the download request if passed in.
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.
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
-
266
249
#### Retrieving the remote file name and extension
267
250
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:
269
252
270
253
```javascript
271
254
createRemoteFileNode({
@@ -276,25 +259,24 @@ createRemoteFileNode({
276
259
createNode,
277
260
createNodeId,
278
261
// if necessary!
279
-
ext:".jpg",
280
-
name:"image",
262
+
ext:`.jpg`,
263
+
name:`image`,
281
264
})
282
265
```
283
266
284
-
### createFileNodeFromBuffer
267
+
### `createFileNodeFromBuffer`
285
268
286
269
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.
287
270
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.
289
272
290
273
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.
291
274
292
-
#### Example usage
275
+
#### Example
293
276
294
277
The following example is adapted from the source of [`gatsby-source-mysql`](https://github.com/malcolm-kee/gatsby-source-mysql):
0 commit comments