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
This Plugin only exposes a single route. It will be available wherever it is mounted. In the above example, because it is mounted in `functions/register.ts`, it will validate requests to `/register`. The Plugin is mounted with a single object parameter with the following properties.
43
41
44
42
[`secret`](https://dashboard.hcaptcha.com/settings) (mandatory) and [`sitekey`](https://dashboard.hcaptcha.com/sites) (optional) can both be found in your hCaptcha dashboard.
Cloudflare maintains a number of official Pages Plugins for you to use in your Pages projects:
12
11
13
12
<DirectoryListing />
14
13
15
-
***
14
+
---
16
15
17
16
## Author a Pages Plugin
18
17
19
18
A Pages Plugin is a Pages Functions distributable which includes built-in routing and functionality. Developers can include a Plugin as a part of their Pages project wherever they chose, and can pass it some configuration options. The full power of Functions is available to Plugins, including middleware, parameterized routes, and static assets.
20
19
21
20
For example, a Pages Plugin could:
22
21
23
-
* Intercept HTML pages and inject in a third-party script.
24
-
* Proxy a third-party service's API.
25
-
* Validate authorization headers.
26
-
* Provide a full admin web app experience.
27
-
* Store data in KV or Durable Objects.
28
-
* Server-side render (SSR) webpages with data from a CMS.
29
-
* Report errors and track performance.
22
+
- Intercept HTML pages and inject in a third-party script.
23
+
- Proxy a third-party service's API.
24
+
- Validate authorization headers.
25
+
- Provide a full admin web app experience.
26
+
- Store data in KV or Durable Objects.
27
+
- Server-side render (SSR) webpages with data from a CMS.
28
+
- Report errors and track performance.
30
29
31
30
A Pages Plugin is essentially a library that developers can use to augment their existing Pages project with a deep integration to Functions.
32
31
33
32
## Use a Pages Plugin
34
33
35
34
Developers can enhance their projects by mounting a Pages Plugin at a route of their application. Plugins will provide instructions of where they should typically be mounted (for example, an admin interface might be mounted at `functions/admin/[[path]].ts`, and an error logger might be mounted at `functions/_middleware.ts`). Additionally, each Plugin may take some configuration (for example, with an API token).
36
35
37
-
***
36
+
---
38
37
39
38
## Static form example
40
39
41
40
In this example, you will build a Pages Plugin and then include it in a project.
42
41
43
42
The first Plugin should:
44
43
45
-
* intercept HTML forms.
46
-
* store the form submission in [KV](/kv/api/).
47
-
* respond to submissions with a developer's custom response.
44
+
- intercept HTML forms.
45
+
- store the form submission in [KV](/kv/api/).
46
+
- respond to submissions with a developer's custom response.
The `npx wrangler pages functions build` command supports a number of arguments, including:
70
68
71
-
*`--plugin` which tells the command to build a Pages Plugin, (rather than Pages Functions as part of a Pages project)
72
-
*`--outdir` which allows you to specify where to output the built Plugin
73
-
*`--external` which can be used to avoid bundling external modules in the Plugin
74
-
*`--watch` argument tells the command to watch for changes to the source files and rebuild the Plugin automatically
69
+
-`--plugin` which tells the command to build a Pages Plugin, (rather than Pages Functions as part of a Pages project)
70
+
-`--outdir` which allows you to specify where to output the built Plugin
71
+
-`--external` which can be used to avoid bundling external modules in the Plugin
72
+
-`--watch` argument tells the command to watch for changes to the source files and rebuild the Plugin automatically
75
73
76
74
For more information about the available arguments, run `npx wrangler pages functions build --help`.
77
75
78
-
79
76
:::
80
77
81
78
In our example, `dist/index.js` will be the entrypoint to your Plugin. This is a generated file built by Wrangler with the `npm run build` command. Add the `dist/` directory to your `.gitignore`.
82
79
83
80
Next, create a `functions` directory and start coding your Plugin. The `functions` folder will be mounted at some route by the developer, so consider how you want to structure your files. Generally:
84
81
85
-
* if you want your Plugin to run on a single route of the developer's choice (for example, `/foo`), create a `functions/index.ts` file.
86
-
* if you want your Plugin to be mounted and serve all requests beyond a certain path (for example, `/admin/login` and `/admin/dashboard`), create a `functions/[[path]].ts` file.
87
-
* if you want your Plugin to intercept requests but fallback on either other Functions or the project's static assets, create a `functions/_middleware.ts` file.
82
+
- if you want your Plugin to run on a single route of the developer's choice (for example, `/foo`), create a `functions/index.ts` file.
83
+
- if you want your Plugin to be mounted and serve all requests beyond a certain path (for example, `/admin/login` and `/admin/dashboard`), create a `functions/[[path]].ts` file.
84
+
- if you want your Plugin to intercept requests but fallback on either other Functions or the project's static assets, create a `functions/_middleware.ts` file.
88
85
89
86
:::note[Do not include the mounted path in your Plugin]
90
87
91
-
92
88
Your Plugin should not use the mounted path anywhere in the file structure (for example, `/foo` or `/admin`). Developers should be free to mount your Plugin wherever they choose, but you can make recommendations of how you expect this to be mounted in your `README.md`.
93
89
94
-
95
90
:::
96
91
97
92
You are free to use as many different files as you need. The structure of a Plugin is exactly the same as Functions in a Pages project today, except that the handlers receive a new property of their parameter object, `pluginArgs`. This property is the initialization parameter that a developer passes when mounting a Plugin. You can use this to receive API tokens, KV/Durable Object namespaces, or anything else that your Plugin needs to work.
@@ -100,44 +95,48 @@ Returning to your static form example, if you want to intercept requests and ove
100
95
101
96
```typescript
102
97
classFormHandler {
103
-
element(element) {
104
-
const name =element.getAttribute('data-static-form-name')
To create a good developer experience, you should consider adding TypeScript typings to your Plugin. This allows developers to use their IDE features for autocompletion, and also ensure that they include all the parameters you are expecting.
@@ -146,8 +145,8 @@ In the `index.d.ts`, export a function which takes your `pluginArgs` and returns
@@ -163,7 +162,7 @@ You can distribute your Plugin however you choose. Popular options include publi
163
162
164
163
Make sure you are including the generated `dist/` directory, your typings `index.d.ts`, as well as a `README.md` with instructions on how developers can use your Plugin.
165
164
166
-
***
165
+
---
167
166
168
167
### 5. Install your Pages Plugin
169
168
@@ -187,14 +186,14 @@ A Plugin's default export is a function which takes the same context parameter t
@@ -234,7 +233,7 @@ If you experience any problems with any one Plugin, file an issue on that Plugin
234
233
235
234
If you experience any problems with Plugins in general, we would appreciate your feedback in the #pages-discussions channel in [Discord](https://discord.com/invite/cloudflaredev)! We are excited to see what you build with Plugins and welcome any feedback about the authoring or developer experience. Let us know in the Discord channel if there is anything you need to make Plugins even more powerful.
236
235
237
-
***
236
+
---
238
237
239
238
## Chain your Plugin
240
239
@@ -246,25 +245,26 @@ import cloudflareAccessPlugin from "@cloudflare/pages-plugin-cloudflare-access";
This Plugin only exposes a single route to verify an incoming Turnstile response in a `POST` as the `cf-turnstile-response` parameter. It will be available wherever it is mounted. In the example above, it is mounted in `functions/register.ts`. As a result, it will validate requests to `/register`.
Copy file name to clipboardExpand all lines: src/content/docs/pages/how-to/refactor-a-worker-to-pages-functions.mdx
+1-3Lines changed: 1 addition & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ pcx_content_type: how-to
3
3
title: Refactor a Worker to a Pages Function
4
4
---
5
5
6
-
import { Render } from"~/components"
6
+
import { Render } from"~/components";
7
7
8
8
In this guide, you will learn how to refactor a Worker made to intake form submissions to a Pages Function that can be hosted on your Cloudflare Pages application. [Pages Functions](/pages/functions/) is a serverless function that lives within the same project directory as your application and is deployed with Cloudflare Pages. It enables you to run server-side code that adds dynamic functionality without running a dedicated server. You may want to refactor a Worker to a Pages Function for one of these reasons:
To refactor the above Worker, go to your Pages project directory and create a `/functions` folder. In `/functions`, create a `form.js` file. This file will handle form submissions.
0 commit comments