Skip to content

Commit 4303b74

Browse files
authored
feat(docs): edit file system route API docs page (#32212)
* feat(docs): edit file system route API docs page * Fix typo * cleanup * typo * Add missing braces * better explanation * remove not interesting example * anuder fix
1 parent d926682 commit 4303b74

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

docs/docs/reference/routing/file-system-route-api.md

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,49 @@
11
---
22
title: File System Route API
3+
examples:
4+
- label: Using the FS Routing API
5+
href: "https://github.com/gatsbyjs/gatsby/tree/master/examples/route-api"
36
---
47

5-
Use the File System Route API when you want to programmatically create pages from your GraphQL data, e.g. to create individual blog post pages for your blog. With this API you can control the file path and queried data by adding some extra notation to the names of your files without touching or creating `gatsby-node.js` whatsoever.
8+
Use the File System Route API when you want to create dynamic pages e.g. to create individual blog post pages for your blog.
69

7-
This page documents the APIs and conventions for using the file system as the primary way of creating pages. You should be able to accomplish most common tasks with this file-based API. If you want more control over the page creation you should use the [`createPages`](/docs/reference/config-files/gatsby-node#createPages) API.
10+
You should be able to accomplish most common tasks with this file-based API. If you want more control over the page creation you should use the [`createPages`](/docs/reference/config-files/gatsby-node#createPages) API.
811

9-
In short, these APIs enable you to programmatically create pages from Gatsby's [GraphQL data layer](/docs/conceptual/graphql-concepts/) and to create [client-only routes](/docs/how-to/routing/client-only-routes-and-user-authentication).
12+
Dynamic pages can be created from collections in Gatsby's [GraphQL data layer](/docs/conceptual/graphql-concepts/) and to create [client-only routes](/docs/how-to/routing/client-only-routes-and-user-authentication).
1013

11-
A complete example showcasing all options can be found in [Gatsby's "examples" folder](https://github.com/gatsbyjs/gatsby/tree/master/examples/route-api).
14+
A complete example showcasing all options can be found in [Gatsby's examples folder](https://github.com/gatsbyjs/gatsby/tree/master/examples/route-api).
1215

1316
## Collection routes
1417

15-
Imagine a Gatsby project that sources a `product.yaml` file and multiple Markdown blog posts. At build time, Gatsby will automatically [infer](/docs/glossary/#inference) the fields and create multiple [nodes](/docs/glossary#node) for both types (`Product` and `MarkdownRemark`). There are two ways you could query for that data:
18+
Imagine a Gatsby project that sources a `product.yaml` file and multiple Markdown blog posts. At build time, Gatsby will automatically [infer](/docs/glossary/#inference) the fields and create multiple [nodes](/docs/glossary#node) for both types (`Product` and `MarkdownRemark`).
1619

17-
- You can perform GraphQL queries like `allProduct` or `allMarkdownRemark` from inside a component, as usual.
18-
- You can use the File System Route API to access node information directly from the file path.
19-
20-
To use the File System Route API, use curly braces (`{ }`) in your filenames to signify dynamic URL segments that relate to a field within the node. Here are a few examples:
20+
To create collection routes, use curly braces (`{ }`) in your filenames to signify dynamic URL segments that relate to a field within the node. Here are a few examples:
2121

2222
- `src/pages/products/{Product.name}.js` will generate a route like `/products/burger`
2323
- `src/pages/products/{Product.fields__sku}.js` will generate a route like `/products/001923`
2424
- `src/pages/blog/{MarkdownRemark.parent__(File)__name}.js` will generate a route like `/blog/learning-gatsby`
2525

26-
At build time, Gatsby uses the content within the curly braces to generate GraphQL queries to retrieve the nodes that should be built for a given collection (collection here refers to all nodes of a given type, e.g. all Markdown files for `MarkdownRemark`). Gatsby then runs those queries to grab all the nodes and create a page for each of them. Gatsby also adds an `id` field to every query automatically, to simplify integration with page queries.
26+
Gatsby creates a page for each node in a collection route. So if you have three markdown files that are blog posts, Gatsby will create the three pages from a collection route. As you add and remove markdown files, Gatsby will add and remove pages.
27+
28+
Collection routes can be created for any GraphQL data type. Creating new collection routes in Gatsby is a process
29+
of adding a source plugin, use GraphiQL to identify the type and field to construct the route file name, and then code the route component.
2730

2831
### Syntax (collection routes)
2932

3033
There are some general syntax requirements when using collection routes:
3134

32-
- Filenames must start and end with curly braces (`{ }`).
33-
- Types can be both lowercase and uppercase (e.g. `MarkdownRemark` or `contentfulMyContentType`).
34-
- The initial type name must be followed by a period (`.`).
35+
- Dynamic segments of file paths must start and end with curly braces (`{ }`).
36+
- Types are case sensitive (e.g. `MarkdownRemark` or `contentfulMyContentType`). Check GraphiQL for the correct names.
37+
- Dynamic segments must include both a type and a field e.g. `{Type.field}` or `{BlogPost.slug}`.
3538

36-
> Note: To keep things consistent, only capitalized type names are used in the examples.
39+
### Nested routes
3740

38-
In addition to files, you can also name folders with this syntax. This allows you to create nested routes. For example:
41+
You can use dynamic segments multiple times in a path. For example, you might want to nest product names within its product category. For example:
3942

40-
- `src/pages/products/{Product.name}/{Product.color}.js` will generate a route like `/products/fidget-spinner/red`
41-
- `src/pages/products/{Product.name}/template.js` will generate a route like `/products/fidget-spinner/template`
43+
- `src/pages/products/{Product.category}/{Product.name}.js` will generate a route like `/products/toys/fidget-spinner`
44+
- `src/pages/products/{Product.category}/{Product.name}/{Product.color}.js` will generate a route like `/products/toys/fidget-spinner/red`
45+
46+
### Field syntax
4247

4348
#### Dot notation
4449

@@ -106,11 +111,14 @@ allMarkdownRemark {
106111
}
107112
```
108113

109-
### Component implementation
114+
### Collection Route Components
115+
116+
Collection route components are passed two dynamic variables. The `id` of the each page's node and the
117+
URL path as `param`. The param is passed to the component as `props.params` and the id as `props.context.id`.
110118

111-
Naming your file with the File System Route API will generate routes for each node and will automatically pass the field name via `props.params` to the React component and as a variable to the GraphQL query. As explained in [Querying data in pages with GraphQL](/docs/how-to/querying-data/page-query/) you can render data from a node on the page by filtering only for the given `id` or field name.
119+
Both are also passed as variables to the component's GraphQL query so you can query fields from the node. Page querying, including the use of variables, is explained in more depth in [querying data in pages with GraphQL](/docs/how-to/querying-data/page-query/).
112120

113-
For example, in the component itself (e.g. `src/pages/products/{Product.name}.js`) you're able to access the `name` via `props.params` and as a variable in the GraphQL query (as `$name`). However, we recommend filtering by `id` as this is the fastest way to filter.
121+
For example:
114122

115123
```js:title=src/pages/products/{Product.name}.js
116124
import React from "react"
@@ -135,13 +143,13 @@ export const query = graphql`
135143
`
136144
```
137145

138-
If you need to customize the query used for collecting the nodes (e.g. filtering out any product of type `"Food"`), you should use the [`createPages`](/docs/reference/config-files/gatsby-node#createPages) API instead as File System Route API doesn't support this at the moment.
146+
If you need to want to create pages for only some nodes in a collection (e.g. filtering out any product of type `"Food"`) or customize the variables passed to the query, you should use the [`createPages`](/docs/reference/config-files/gatsby-node#createPages) API instead as File System Route API doesn't support this at the moment.
139147

140148
### Routing and linking
141149

142150
Gatsby "slugifies" every route that gets created from collection pages (by using [`sindresorhus/slugify`](https://github.com/sindresorhus/slugify)). Or in other words: If you have a route called `src/pages/wholesome/{Animal.slogan}.js` where `slogan` is `I ♥ Dogs` the final URL will be `/wholesome/i-love-dogs`. Gatsby will convert the field into a human-readable URL format while stripping it of invalid characters.
143151

144-
When you want to link to one of those pages, it may not always be clear how to construct the URL from scratch.
152+
When you want to link to a collection route page, it may not always be clear how to construct the URL from scratch.
145153

146154
To address this issue, Gatsby automatically includes a `gatsbyPath` field on every type used by collection pages. The `gatsbyPath` field must take an argument of the `filePath` it is trying to resolve. This is necessary because it’s possible that one type is used in multiple collection pages.
147155

0 commit comments

Comments
 (0)