Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 43 additions & 26 deletions src/content/docs/pages/functions/routing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import { FileTree } from "~/components";
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:

<FileTree>

- ...
- functions
- index.js
- helloworld.js
- howdyworld.js
- fruits
- index.js
- apple.js
- banana.js
- index.js
- helloworld.js
- howdyworld.js
- fruits
- index.js
- apple.js
- banana.js

</FileTree>

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:
Expand Down Expand Up @@ -80,13 +82,15 @@ More specific routes (routes with fewer wildcards) take precedence over less spe
Review the following `/functions/` directory structure:

<FileTree>

- ...
- functions
- date.js
- users
- special.js
- [user].js
- [[catchall]].js
- date.js
- users
- special.js
- [user].js
- [[catchall]].js

</FileTree>

The following requests will match the following files:
Expand All @@ -106,7 +110,7 @@ For files which match a single URL segment (use a single set of brackets), the v

```js
export function onRequest(context) {
return new Response(context.params.user)
return new Response(context.params.user);
}
```

Expand All @@ -116,7 +120,7 @@ For files which match against multiple URL segments (use a double set of bracket

```js
export function onRequest(context) {
return new Response(JSON.stringify(context.params.catchall))
return new Response(JSON.stringify(context.params.catchall));
}
```

Expand All @@ -138,9 +142,9 @@ Create a `_routes.json` file to control when your Function is invoked. It should

This file will include three different properties:

* **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.
* **include**: Defines routes that will be invoked by Functions. Accepts wildcard behavior.
* **exclude**: Defines routes that will not be invoked by Functions. Accepts wildcard behavior. `exclude` always take priority over `include`.
- **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.
- **include**: Defines routes that will be invoked by Functions. Accepts wildcard behavior.
- **exclude**: Defines routes that will not be invoked by Functions. Accepts wildcard behavior. `exclude` always take priority over `include`.

:::note

Expand All @@ -154,9 +158,9 @@ Below is an example of a `_routes.json`.

```json
{
"version": 1,
"include": ["/*"],
"exclude": []
"version": 1,
"include": ["/*"],
"exclude": []
}
```

Expand All @@ -166,16 +170,29 @@ Below is another example of a `_routes.json` file. Any route inside the `/build`

```json
{
"version": 1,
"include": ["/*"],
"exclude": ["/build/*"]
"version": 1,
"include": ["/*"],
"exclude": ["/build/*"]
}
```

## Fail open / closed

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.

1. Log in to the [Cloudflare dashboard](https://dash.cloudflare.com) and select your account.
2. In **Account Home**, select **Workers & Pages**.
3. In **Overview**, select your Pages project.
4. Select **Settings** > **Runtime** > **Fail open / closed**.

"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.

The daily request limit for Pages Functions can be removed entirely by upgrading to [Workers Standard](/workers/platform/pricing/#workers).

### Limits

Functions invocation routes have the following limits:

* You must have at least one include rule.
* You may have no more than 100 include/exclude rules combined.
* Each rule may have no more than 100 characters.
- You must have at least one include rule.
- You may have no more than 100 include/exclude rules combined.
- Each rule may have no more than 100 characters.
Loading