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: src/content/docs/pages/functions/routing.mdx
+22-29Lines changed: 22 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,22 +3,23 @@ pcx_content_type: reference
3
3
title: Routing
4
4
sidebar:
5
5
order: 2
6
-
7
6
---
8
7
8
+
import { FileTree } from"~/components";
9
+
9
10
Functions utilize file-based routing. Your `/functions` directory structure determines the designated routes that your Functions will run on. You can create a `/functions` directory with as many levels as needed for your project's use case. Review the following directory:
10
11
11
-
```
12
-
|---- …
13
-
|---- functions
14
-
|___ index.js
15
-
|___ helloworld.js
16
-
|___ howdyworld.js
17
-
|___ fruits
18
-
|___ index.js
19
-
|___ apple.js
20
-
|___ banana.js
21
-
```
12
+
<FileTree>
13
+
- ...
14
+
- functions
15
+
- index.js
16
+
- helloworld.js
17
+
- howdyworld.js
18
+
- fruits
19
+
- index.js
20
+
- apple.js
21
+
- banana.js
22
+
</FileTree>
22
23
23
24
The following routes will be generated based on the above file structure. These routes map the URL pattern to the `/functions` file that will be invoked when a visitor goes to the URL:
24
25
@@ -33,10 +34,8 @@ The following routes will be generated based on the above file structure. These
33
34
34
35
:::note[Trailing slash]
35
36
36
-
37
37
Trailing slash is optional. Both `/foo` and `/foo/` will be routed to `/functions/foo.js` or `/functions/foo/index.js`. If your project has both a `/functions/foo.js` and `/functions/foo/index.js` file, `/foo` and `/foo/` would route to `/functions/foo/index.js`.
38
38
39
-
40
39
:::
41
40
42
41
If no Function is matched, it will fall back to a static asset if there is one. Otherwise, the Function will fall back to the [default routing behavior](/pages/configuration/serving-pages/) for Pages' static assets.
@@ -72,25 +71,23 @@ By placing two sets of brackets around your filename – for example, `/users/[[
72
71
73
72
:::note[Route specificity]
74
73
75
-
76
74
More specific routes (routes with fewer wildcards) take precedence over less specific routes.
77
75
78
-
79
76
:::
80
77
81
78
#### Dynamic route examples
82
79
83
80
Review the following `/functions/` directory structure:
84
81
85
-
```
86
-
|---- …
87
-
|---- functions
88
-
|___ date.js
89
-
|___ users/
90
-
|___ special.js
91
-
|___ [user].js
92
-
|___ [[catchall]].js
93
-
```
82
+
<FileTree>
83
+
- ...
84
+
- functions
85
+
- date.js
86
+
-users
87
+
- special.js
88
+
-[user].js
89
+
-[[catchall]].js
90
+
</FileTree>
94
91
95
92
The following requests will match the following files:
96
93
@@ -131,10 +128,8 @@ On a purely static project, Pages offers unlimited free requests. However, once
131
128
132
129
:::note
133
130
134
-
135
131
Some frameworks (such as Remix, SvelteKit) will also automatically generate a `_routes.json` file. However, if your preferred framework does not, create an issue on their framework repository with a link to this page or let us know on [Discord](https://discord.cloudflare.com). Refer to the [Framework guide](/pages/framework-guides/) for more information on full-stack frameworks.
136
132
137
-
138
133
:::
139
134
140
135
### Create a `_routes.json` file
@@ -149,10 +144,8 @@ This file will include three different properties:
149
144
150
145
:::note
151
146
152
-
153
147
Wildcards match any number of path segments (slashes). For example, `/users/*` will match everything after the`/users/` path.
0 commit comments