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
+43-26Lines changed: 43 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,15 +10,17 @@ import { FileTree } from "~/components";
10
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:
11
11
12
12
<FileTree>
13
+
13
14
- ...
14
15
- functions
15
-
- index.js
16
-
- helloworld.js
17
-
- howdyworld.js
18
-
- fruits
19
-
- index.js
20
-
- apple.js
21
-
- banana.js
16
+
- index.js
17
+
- helloworld.js
18
+
- howdyworld.js
19
+
- fruits
20
+
- index.js
21
+
- apple.js
22
+
- banana.js
23
+
22
24
</FileTree>
23
25
24
26
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:
@@ -80,13 +82,15 @@ More specific routes (routes with fewer wildcards) take precedence over less spe
80
82
Review the following `/functions/` directory structure:
81
83
82
84
<FileTree>
85
+
83
86
- ...
84
87
- functions
85
-
- date.js
86
-
- users
87
-
- special.js
88
-
-[user].js
89
-
-[[catchall]].js
88
+
- date.js
89
+
- users
90
+
- special.js
91
+
-[user].js
92
+
-[[catchall]].js
93
+
90
94
</FileTree>
91
95
92
96
The following requests will match the following files:
@@ -106,7 +110,7 @@ For files which match a single URL segment (use a single set of brackets), the v
106
110
107
111
```js
108
112
exportfunctiononRequest(context) {
109
-
returnnewResponse(context.params.user)
113
+
returnnewResponse(context.params.user);
110
114
}
111
115
```
112
116
@@ -116,7 +120,7 @@ For files which match against multiple URL segments (use a double set of bracket
@@ -138,9 +142,9 @@ Create a `_routes.json` file to control when your Function is invoked. It should
138
142
139
143
This file will include three different properties:
140
144
141
-
***version**: Defines the version of the schema. Currently there is only one version of the schema (version 1), however, we may add more in the future and aim to be backwards compatible.
142
-
***include**: Defines routes that will be invoked by Functions. Accepts wildcard behavior.
143
-
***exclude**: Defines routes that will not be invoked by Functions. Accepts wildcard behavior. `exclude` always take priority over `include`.
145
+
-**version**: Defines the version of the schema. Currently there is only one version of the schema (version 1), however, we may add more in the future and aim to be backwards compatible.
146
+
-**include**: Defines routes that will be invoked by Functions. Accepts wildcard behavior.
147
+
-**exclude**: Defines routes that will not be invoked by Functions. Accepts wildcard behavior. `exclude` always take priority over `include`.
144
148
145
149
:::note
146
150
@@ -154,9 +158,9 @@ Below is an example of a `_routes.json`.
154
158
155
159
```json
156
160
{
157
-
"version": 1,
158
-
"include": ["/*"],
159
-
"exclude": []
161
+
"version": 1,
162
+
"include": ["/*"],
163
+
"exclude": []
160
164
}
161
165
```
162
166
@@ -166,16 +170,29 @@ Below is another example of a `_routes.json` file. Any route inside the `/build`
166
170
167
171
```json
168
172
{
169
-
"version": 1,
170
-
"include": ["/*"],
171
-
"exclude": ["/build/*"]
173
+
"version": 1,
174
+
"include": ["/*"],
175
+
"exclude": ["/build/*"]
172
176
}
173
177
```
174
178
179
+
## Fail open / closed
180
+
181
+
If on the Workers Free plan, you can configure how Pages behaves when your daily free tier allowance of Pages Functions requests is exhausted. If, for example, you are performing authentication checks or other critical functionality in your Pages Functions, you may wish to disable your Pages project when the allowance is exhausted.
182
+
183
+
1. Log in to the [Cloudflare dashboard](https://dash.cloudflare.com) and select your account.
184
+
2. In **Account Home**, select **Workers & Pages**.
185
+
3. In **Overview**, select your Pages project.
186
+
4. Select **Settings** > **Runtime** > **Fail open / closed**.
187
+
188
+
"Fail open" means that static assets will continue to be served, even if Pages Functions would ordinarily have run first. "Fail closed" means an error page will be returned, rather than static assets.
189
+
190
+
The daily request limit for Pages Functions can be removed entirely by upgrading to [Workers Standard](/workers/platform/pricing/#workers).
191
+
175
192
### Limits
176
193
177
194
Functions invocation routes have the following limits:
178
195
179
-
* You must have at least one include rule.
180
-
* You may have no more than 100 include/exclude rules combined.
181
-
* Each rule may have no more than 100 characters.
196
+
- You must have at least one include rule.
197
+
- You may have no more than 100 include/exclude rules combined.
0 commit comments