-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cloudflare): Add plugin for cloudflare pages (#13123)
Before reviewing this change, I recommend reading through a GH discussion I wrote up that explains the reasoning behind the API surface of the cloudflare SDK: #13007 This PR adds support for [Cloudflare Pages](https://developers.cloudflare.com/pages/), Cloudflare's fullstack development deployment platform that is powered by Cloudflare Workers under the hood. Think of this platform having very similar capabilities (and constraints) as Vercel. To set the plugin up, you do something like so: ```javascript // functions/_middleware.js import * as Sentry from '@sentry/cloudflare'; export const onRequest = Sentry.sentryPagesPlugin({ dsn: process.env.SENTRY_DSN, tracesSampleRate: 1.0, }); ``` We have to use the middleware instead of a global init because we need to call `init` for every single new incoming request to make sure the sentry instance does not get stale with redeployments. While implementing `sentryPagesPlugin`, I noticed that there was a logic that was redundant between it and `withSentry`, the API for cloudflare workers. This led me to refactor this into a common helper, `wrapRequestHandler`, which is contained in `packages/cloudflare/src/request.ts`. That is why there is diffs in this PR for `packages/cloudflare/src/handler.ts`.
- Loading branch information
1 parent
e2668f8
commit 945cdbc
Showing
10 changed files
with
543 additions
and
355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { setAsyncLocalStorageAsyncContextStrategy } from './async'; | ||
import type { CloudflareOptions } from './client'; | ||
import { wrapRequestHandler } from './request'; | ||
|
||
/** | ||
* Plugin middleware for Cloudflare Pages. | ||
* | ||
* Initializes the SDK and wraps cloudflare pages requests with SDK instrumentation. | ||
* | ||
* @example | ||
* ```javascript | ||
* // functions/_middleware.js | ||
* import * as Sentry from '@sentry/cloudflare'; | ||
* | ||
* export const onRequest = Sentry.sentryPagesPlugin({ | ||
* dsn: process.env.SENTRY_DSN, | ||
* tracesSampleRate: 1.0, | ||
* }); | ||
* ``` | ||
* | ||
* @param _options | ||
* @returns | ||
*/ | ||
export function sentryPagesPlugin< | ||
Env = unknown, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
Params extends string = any, | ||
Data extends Record<string, unknown> = Record<string, unknown>, | ||
>(options: CloudflareOptions): PagesPluginFunction<Env, Params, Data, CloudflareOptions> { | ||
setAsyncLocalStorageAsyncContextStrategy(); | ||
return context => wrapRequestHandler({ options, request: context.request, context }, () => context.next()); | ||
} |
Oops, something went wrong.