Skip to content
AnswerWell geo Documentation

Research · 9 min read · Answerwell

Pages Functions Plugins: Mounting, Middleware, Routing, and Chaining

How Pages Functions Plugins install, mount on routes, chain middleware, and integrate auth, forms, GraphQL, and observability.

This page summarizes Cloudflare’s documentation on Pages Plugins: distributable Pages Functions you add to a Pages project with npm (or another package manager), mount on a route or as middleware, and configure at initialization time.

Pages Plugin: what it is and when to use one

Section titled “Pages Plugin: what it is and when to use one”

A Pages Plugin is a Pages Functions distributable that ships built-in routing and behavior. You install a package, import it in a file under functions/, pass configuration (API keys, bindings, callbacks), and the plugin runs where you mounted it—on one path, under a path prefix, or across the whole site via _middleware.

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.

— Cloudflare, Pages Plugins

Use a plugin when you want a repeatable integration (Access, Turnstile, Sentry, static HTML forms) without reimplementing the same Function logic in every project. Use a custom plugin when you are packaging reusable middleware or route handlers for other developers—same functions/ layout as a normal Pages app, but handlers receive pluginArgs from whoever mounts the plugin.

Typical jobs plugins cover: intercept HTML, proxy third-party APIs, validate auth headers, captcha or bot checks, error reporting, GraphQL endpoints, CMS-driven SSR, and KV/Durable Object storage—all described as examples in the authoring overview.

Official Pages Plugins: what ships from the maintainer

Section titled “Official Pages Plugins: what ships from the maintainer”

Cloudflare maintains official plugins listed on Pages Plugins. Each has its own reference page with install and usage; the table below is a routing guide, not a full option list.

Plugin Package (install name) Typical mount Main purpose
Cloudflare Access @cloudflare/pages-plugin-cloudflare-access Protected routes or admin Validate Access JWT; failed validation → 403
Google Chat @cloudflare/pages-plugin-google-chat Single bot URL Google Chat bot + API for alerts
GraphQL @cloudflare/pages-plugin-graphql e.g. functions/graphql.ts → /graphql Expose one GraphQL route
hCaptcha @cloudflare/pages-plugin-hcaptcha POST route (e.g. functions/register.ts) Verify human on form POST
Honeycomb @cloudflare/pages-plugin-honeycomb Often _middleware Send events to Honeycomb
Sentry @cloudflare/pages-plugin-sentry functions/_middleware.ts first Catch exceptions below in the chain
Static Forms @cloudflare/pages-plugin-static-forms _middleware or specific route Intercept data-static-form-name forms
Stytch @cloudflare/pages-plugin-stytch Middleware Validate session_token
Turnstile @cloudflare/pages-plugin-turnstile POST route Verify cf-turnstile-response
vercel/og @cloudflare/pages-plugin-vercel-og Often path _middleware Generate OG images, optional meta injection
Community (see community list) Varies Third-party maintained

Community examples named in Community Plugins include asset negotiation, Proxyflare for Pages, Rollbar, tRPC, and Twind—each maintained outside Cloudflare’s core set.

Install a Pages Plugin: package managers and project setup

Section titled “Install a Pages Plugin: package managers and project setup”

Every official plugin follows the same install pattern. If the project has no package.json, run npm init first (see Install your Pages Plugin).

npm i @cloudflare/pages-plugin-sentry

The same commands appear across plugin docs with yarn, pnpm, and bun equivalents—for example Sentry, Turnstile, and Static Forms.

Add the package to package.json, commit it with your Pages project, and set any environment variables or bindings (KV, D1, secrets) the plugin expects in the Pages dashboard before deploy.

Mount a Pages Plugin: routes, [[path]], and _middleware

Section titled “Mount a Pages Plugin: routes, [[path]], and _middleware”

Mounting means creating a file under functions/ and exporting an onRequest (or method-specific exports) that wraps the plugin.

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

— Cloudflare, Use a Pages Plugin

Plugins that only expose one route run wherever you mount them. GraphQL mounted at functions/graphql.ts serves /graphql. hCaptcha on functions/register.ts validates /register.

Use _middleware when the plugin should run before other handlers or static assets, or across many routes. Sentry should be first in _middleware so it captures exceptions in everything below:

The Sentry Pages Plugin captures and logs all exceptions which occur below it in the execution chain of your Pages Functions. It is therefore recommended that you install this Plugin at the root of your application in functions/_middleware.ts as the very first Plugin.

— Cloudflare, Sentry

Static Forms and custom form interceptors also commonly use middleware to rewrite HTML forms on GET and handle POST.

Catch-all paths (functions/admin/[[path]].ts)

Section titled “Catch-all paths (functions/admin/[[path]].ts)”

For an app under a prefix (e.g. /admin/login, /admin/dashboard), use [[path]] as in the authoring guide.

A plugin’s default export is a function that receives the same context as a normal Pages Function; you call it with your config and pass through context:

import staticFormInterceptorPlugin from "@cloudflare/static-form-interceptor";
export const onRequest = (context) => {
return staticFormInterceptorPlugin({
kv: context.env.FORM_KV,
respondWith: async ({ formData }) => {
const name = formData.get("name");
return new Response(`Thank you for your submission, ${name}!`);
},
})(context);
};

(Example from Mount your Pages Plugin.)

Chain Pages Plugins: arrays, order, and context.data

Section titled “Chain Pages Plugins: arrays, order, and context.data”

Multiple plugins on one route export an array of handlers. Order matters: middleware higher in the filesystem runs before deeper handlers, and array entries run in sequence.

The chain example stacks Sentry, Cloudflare Access, a custom Function that reads context.data.cloudflareAccessJWT and sets Sentry user info, then an admin plugin—so errors are logged only after Access succeeds and the dashboard runs last.

Patterns for downstream handlers:

Exporting onRequestPost as an array is common for captcha plugins: the plugin runs first, then your handler after validation (Turnstile usage, hCaptcha usage).

Configure official plugins: auth, bots, forms, and observability

Section titled “Configure official plugins: auth, bots, forms, and observability”

The Plugin takes an object with two properties: the domain of your Cloudflare Access account, and the policy aud (audience) to validate against. Any requests which fail validation will be returned a 403 status code.

— Cloudflare, Cloudflare Access

The plugin exposes one route—that URL is what you set as App URL in Google Cloud Console (Google Chat).

Static Forms: data-static-form-name and respondWith

Section titled “Static Forms: data-static-form-name and respondWith”

The Static Forms Pages Plugin intercepts all form submissions made which have the data-static-form-name attribute set.

— Cloudflare, Static Forms

You implement storage or notifications in respondWith; the plugin overrides form method/action so POST stays on the same page (Static Forms usage).

Both plugins verify tokens from multipart form data by default (cf-turnstile-response, h-captcha-response), use CF-Connecting-IP for remoteip when omitted, and support custom onError responses (Turnstile, hCaptcha).

Prefer HONEYCOMB_API_KEY and HONEYCOMB_DATASET from context.env (Honeycomb). In Honeycomb’s UI, enable Automatically unpack nested JSON and set Maximum unpacking depth to 5, as required in that doc.

Pass project_id, secret, and env (envs.test or envs.live); Cloudflare recommends storing the secret in KV rather than source (Stytch).

vercel/og: images and HTMLRewriter extractors

Section titled “vercel/og: images and HTMLRewriter extractors”

Configure imagePathSuffix, a React component, optional extractors (e.g. og:title), and autoInject.openGraph for meta tags (vercel/og).

Author a custom Pages Plugin: build, pluginArgs, and path rules

Section titled “Author a custom Pages Plugin: build, pluginArgs, and path rules”

Authoring is documented end-to-end on Pages Plugins (static form interceptor example).

Package layout: package.json with a build script such as:

npx wrangler pages functions build --plugin --outdir=dist

--plugin builds a plugin distributable; --outdir, --external, and --watch are supported (Create a new Pages Plugin). Ship dist/index.js as the entry; add dist/ to .gitignore.

File layout inside the plugin mirrors consumer Projects:

  • functions/index.ts — single route chosen by the consumer.
  • functions/[[path]].ts — serve everything under a mount prefix.
  • functions/_middleware.ts — intercept and optionally context.next() to static assets or other Functions.

Do not include the mounted path in your Plugin … Developers should be free to mount your Plugin wherever they choose.

— Cloudflare, Author a Pages Plugin

Handlers read context.pluginArgs—the object passed when the consumer mounts the plugin (KV namespaces, tokens, respondWith, etc.) (Author a Pages Plugin).

Custom middleware often uses HTMLRewriter on GET to adjust forms and handles POST separately, as in the static form example on the same page.

Local test and deploy a Pages project with plugins

Section titled “Local test and deploy a Pages project with plugins”
  1. Run local dev with Wrangler Pages, including bindings and env vars the plugin needs (Test your Pages Plugin).

  2. For Static Forms or a custom interceptor, confirm GET responses rewrite forms (method="POST", hidden static-form-name field) and POST returns your respondWith response (Test your Pages Plugin).

  3. Verify the plugin is listed in package.json, push to git, and let Cloudflare Pages deploy (Deploy your Pages project).

  4. For plugin-specific failures, use that plugin’s issue tracker; for general plugin platform feedback, Cloudflare points to #pages-discussions on Discord (Deploy your Pages project).

  • Wrong mount file: single-route plugins (GraphQL, Google Chat, captcha on one form) need a dedicated functions/…ts path; site-wide behavior belongs in _middleware.
  • Sentry not first: installing Sentry below other handlers misses errors those handlers throw first (Sentry).
  • Hard-coded secrets: Stytch, Turnstile, hCaptcha, and Honeycomb docs assume dashboard or context.env / KV—not committed keys (Stytch, Honeycomb).
  • Baking mount paths into plugin packages: breaks consumer flexibility (Author a Pages Plugin).
  • Missing Honeycomb unpacking settings: events may not unpack as expected without depth 5 (Honeycomb).
  • Google Chat App URL mismatch: the Console URL must match the single route the plugin exposes (Google Chat).
  1. Confirm the npm package for your plugin is in package.json and installed locally using the command on that plugin’s Cloudflare doc (for example Sentry Installation).
  2. Check that functions/ contains the mount path described in that plugin’s Usage section (e.g. GraphQL, Turnstile).
  3. Run wrangler pages dev with the same bindings and environment variables as production (Test your Pages Plugin); hit the route and confirm expected status codes (e.g. Access 403 when JWT is invalid per Cloudflare Access).
  4. After deploy, exercise one successful and one failing path (captcha, Access, or form submission) and confirm observability plugins (Sentry, Honeycomb) receive events when configured.
  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page does not match your project or the linked Cloudflare docs, email yimasynthetic@gmail.com with the page URL and the exact sentence you think is wrong so it can be corrected or clarified.