Skip to content

Commit dd6550c

Browse files
authored
RFC: File System Routing Improvements (#24463)
* RFC: Secret * Update 0013-secret.md * Update 0013-secret.md * rename rfc
1 parent 07085fa commit dd6550c

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
- Start Date: 2020-06-18
2+
- RFC PR: https://github.com/gatsbyjs/gatsby/pull/24463
3+
4+
# Summary
5+
6+
The file system is the definitive way to create routes with the `src/pages` directory. Previously only standalone and static pages like `/about` could be created there. This proposal is to enable client routes and collection routes to be created from the filesystem as well. Common patterns like creating pages for each blog post or paginated index pages are much simpler and now you can easily see your sites routes by looking at your file system. Page components now have all the logic they need to query data, create routes, and render. This makes them very easy to reuse on multiple sites and distribute through Gatsby Themes.
7+
8+
# Basic example
9+
10+
- `/src/pages/products/{Product.name}.js`
11+
- `/src/pages/users/[id].js`
12+
13+
The `{}` delimiter signifies a collection route. Where the first segment is the Model name, and the second segment is the field to use for the url part.
14+
The `[]` delimiter signifies a client side route. This will create a page at `users/:id`
15+
16+
# Motivation
17+
18+
A core part of building a site is crafting the routing structure. What pages exist and how are they built. Gatsby v1 introduced a low-level API createPage to accomplish this task. While this API is very powerful and flexible, we've long wanted a higher-level API that helps developers create sites faster.
19+
20+
# Detailed design
21+
22+
Client only routes:
23+
24+
```js
25+
// src/pages/past-order/[id].js
26+
27+
// id from the url, generates a url of `past-order/:id`, which when a user visits
28+
// the field will be passed to the component here.
29+
export default function PastOrder({ params }) {
30+
// if the user visits `/past-order/123`, then id here is `123`
31+
const id = params.id
32+
}
33+
```
34+
35+
Collection routes:
36+
37+
```js
38+
// src/pages/product/{Product.name}.js
39+
import { graphql } from "gatsby"
40+
41+
// data will be the values resolved from the query exported in this component.
42+
export default Product = ({ data }) => JSON.stringify(data)
43+
44+
export const query = graphql`
45+
# $name is the interpolated value from the url/file name
46+
query Project($name: String) {
47+
# query for the product that matches this name
48+
product(name: { eq: $name }) {
49+
# this is the data that gets given to the component in this file
50+
id
51+
name
52+
sku
53+
}
54+
}
55+
`
56+
```
57+
58+
Collection routes are unique because they will generate `n` pages for the results of querying all nodes for the Model supplied by the routing structure.
59+
60+
# Adoption strategy
61+
62+
Users can adopt this on their own by manually migrating their gatsby-node.js use cases into the new unified routing file system approach.
63+
64+
Additionally, this pattern will work well with upcoming tooling improvements like recipes and Gatsby admin.
65+
66+
# How we teach this
67+
68+
This will heavily affect documentation. We have several articles guiding users to gatsby-node and how to use that. Those articles should become "advanced" mode docs and any of the uses cases solved by this API should be updated in the docs to use the file system APIs.
69+
70+
# Unresolved questions
71+
72+
Are there common use cases that this API does not support?
73+
74+
# Try it out!
75+
76+
We have a prerelease of this that you can test out. Install this:
77+
78+
```
79+
yarn add gatsby@unifiedroutes-v2
80+
81+
// or
82+
83+
npm install gatsby@unifiedroutes-v2
84+
```

0 commit comments

Comments
 (0)