Skip to content

Commit b7b2263

Browse files
chore(open-next): add server-side sentry to open-next site
1 parent 333d70f commit b7b2263

File tree

7 files changed

+187
-65
lines changed

7 files changed

+187
-65
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Note: this custom worker-entrypoint is used so that the worker can include sentry support
2+
// and it has been written be following:
3+
// - the official open-next docs: https://opennext.js.org/cloudflare/howtos/custom-worker
4+
// - the official sentry docs: https://docs.sentry.io/platforms/javascript/guides/cloudflare
5+
6+
import * as Sentry from '@sentry/cloudflare';
7+
8+
import type { ExecutionContext } from '@cloudflare/workers-types';
9+
10+
import { default as handler } from '../.open-next/worker.js';
11+
12+
export default Sentry.withSentry(
13+
() => ({
14+
dsn: 'https://examplePublicKey@o0.ingest.sentry.io/0',
15+
// Adds request headers and IP for users, for more info visit:
16+
// https://docs.sentry.io/platforms/javascript/guides/cloudflare/configuration/options/#sendDefaultPii
17+
sendDefaultPii: true,
18+
// Enable logs to be sent to Sentry
19+
enableLogs: true,
20+
// Set tracesSampleRate to 1.0 to capture 100% of spans for tracing.
21+
// Learn more at
22+
// https://docs.sentry.io/platforms/javascript/guides/cloudflare/configuration/options/#tracesSampleRate
23+
tracesSampleRate: 1.0,
24+
}),
25+
{
26+
async fetch(
27+
request: Request,
28+
env: Record<string, unknown>,
29+
ctx: ExecutionContext
30+
) {
31+
return handler.fetch(request, env, ctx);
32+
},
33+
}
34+
);
35+
36+
export { DOQueueHandler } from '../.open-next/worker.js';

apps/site/eslint.config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ import baseConfig from '../../eslint.config.js';
66

77
export default baseConfig.concat([
88
{
9-
ignores: ['pages/en/blog/**/*.{md,mdx}/**', 'public', 'next-env.d.ts'],
9+
ignores: [
10+
'pages/en/blog/**/*.{md,mdx}/**',
11+
'public',
12+
'next-env.d.ts',
13+
// The worker entrypoint is bundled by wrangler, not tsc. Its imports
14+
// trigger a tsc crash (see tsconfig.json), so it is excluded from both
15+
// type checking and ESLint's type-aware linting.
16+
'cloudflare/worker-entrypoint.ts',
17+
],
1018
},
1119

1220
eslintReact.configs['recommended-typescript'],

apps/site/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@
107107
"typescript": "catalog:",
108108
"typescript-eslint": "~8.57.2",
109109
"user-agent-data-types": "0.4.2",
110-
"wrangler": "^4.77.0"
110+
"wrangler": "^4.77.0",
111+
"@cloudflare/workers-types": "^4.20260418.1",
112+
"@sentry/cloudflare": "^10.49.0"
111113
},
112114
"imports": {
113115
"#site/*": [

apps/site/tsconfig.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,14 @@
3737
".next/types/**/*.ts",
3838
".next/dev/types/**/*.ts"
3939
],
40-
"exclude": ["node_modules", ".next"]
40+
"exclude": [
41+
"node_modules",
42+
".next",
43+
".open-next",
44+
// The worker entrypoint is bundled by wrangler (not tsc). Its imports of
45+
// @sentry/cloudflare and .open-next/worker.js trigger an infinite-recursion
46+
// crash in the TypeScript compiler (v5.9) during type resolution of
47+
// @cloudflare/workers-types, so we exclude it from type checking.
48+
"cloudflare/worker-entrypoint.ts"
49+
]
4150
}

apps/site/wrangler.jsonc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "./node_modules/wrangler/config-schema.json",
3-
"main": ".open-next/worker.js",
3+
"main": "./cloudflare/worker-entrypoint.ts",
44
"name": "nodejs-website",
55
"compatibility_date": "2024-11-07",
66
"compatibility_flags": ["nodejs_compat", "global_fetch_strictly_public"],
@@ -59,4 +59,9 @@
5959
"new_sqlite_classes": ["DOQueueHandler"],
6060
},
6161
],
62+
"version_metadata": {
63+
// CF_VERSION_METADATA used for sentry
64+
// See: https://docs.sentry.io/platforms/javascript/guides/cloudflare/#release-configuration-optional
65+
"binding": "CF_VERSION_METADATA",
66+
},
6267
}

docs/cloudflare-build-and-deployment.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ For more details, refer to the [Wrangler documentation](https://developers.cloud
1414

1515
Key configurations include:
1616

17-
- `main`: Points to the worker generated by the OpenNext adapter.
17+
- `main`: Points to a custom worker entry point ([`site/cloudflare/worker-entrypoint.ts`](../apps/site/cloudflare/worker-entrypoint.ts)) that wraps the OpenNext-generated worker (see [Custom Worker Entry Point](#custom-worker-entry-point) and [Sentry](#sentry) below).
1818
- `account_id`: Specifies the Cloudflare account ID. This is not required for local previews but is necessary for deployments. You can obtain an account ID for free by signing up at [dash.cloudflare.com](https://dash.cloudflare.com/login).
1919
- This is currently set to `fb4a2d0f103c6ff38854ac69eb709272`, which is the ID of a Cloudflare account controlled by Node.js, and used for testing.
2020
- `build`: Defines the build command to generate the Node.js filesystem polyfills required for the application to run on Cloudflare Workers. This uses the [`@flarelabs/wrangler-build-time-fs-assets-polyfilling`](https://github.com/flarelabs-net/wrangler-build-time-fs-assets-polyfilling) package.
2121
- `alias`: Maps aliases for the Node.js filesystem polyfills generated during the build process.
2222
- `r2_buckets`: Contains a single R2 binding definition for `NEXT_INC_CACHE_R2_BUCKET`. This is used to implement the Next.js incremental cache.
2323
- This is currently set up to a R2 bucket in the aforementioned Cloudflare testing account.
2424
- `durable_objects`: Contains a single DurableObject binding definition for `NEXT_CACHE_DO_QUEUE`. This is used to implement the Open-next cache queue.
25+
- `version_metadata`: Contains a binding for `CF_VERSION_METADATA`, used for Sentry release configuration (see [Sentry](#sentry) below).
2526

2627
### OpenNext Configuration
2728

@@ -54,6 +55,24 @@ The custom loader can be found at [`site/cloudflare/image-loader.ts`](../apps/si
5455

5556
For more details on this see: https://developers.cloudflare.com/images/transform-images/integrate-with-frameworks/#global-loader
5657

58+
### Custom Worker Entry Point
59+
60+
Instead of directly using the OpenNext-generated worker (`.open-next/worker.js`), the application uses a custom worker entry point at [`site/cloudflare/worker-entrypoint.ts`](../apps/site/cloudflare/worker-entrypoint.ts). This allows customizing the worker's behavior before requests are handled (currently used to integrate [Sentry](#sentry) error monitoring).
61+
62+
The custom entry point imports the OpenNext-generated handler from `.open-next/worker.js` and re-exports the `DOQueueHandler` Durable Object needed by the application.
63+
64+
For more details on custom workers, refer to the [OpenNext custom worker documentation](https://opennext.js.org/cloudflare/howtos/custom-worker).
65+
66+
### Sentry
67+
68+
Error monitoring is provided by [Sentry](https://sentry.io/) via the [`@sentry/cloudflare`](https://www.npmjs.com/package/@sentry/cloudflare) package.
69+
70+
The [custom worker entry point](#custom-worker-entry-point) wraps the OpenNext handler with `Sentry.withSentry()`, which instruments incoming requests for error and performance tracking.
71+
72+
The `version_metadata` binding (`CF_VERSION_METADATA`) in the Wrangler configuration enables Sentry [release configuration](https://docs.sentry.io/platforms/javascript/guides/cloudflare/#release-configuration-optional), allowing errors to be associated with specific worker versions.
73+
74+
For more details, refer to the [Sentry Cloudflare guide](https://docs.sentry.io/platforms/javascript/guides/cloudflare).
75+
5776
## Scripts
5877

5978
Preview and deployment of the website targeting the Cloudflare network is implemented via the following two commands:

0 commit comments

Comments
 (0)