Setup Sentry with Cloudflare

This commit is contained in:
Greg Bergé
2025-01-09 09:40:47 +01:00
parent fec48ce708
commit b5e2e3fa8d
5 changed files with 21 additions and 35 deletions
+1
View File
@@ -25,6 +25,7 @@
"@gitbook/react-openapi": "workspace:*",
"@radix-ui/react-checkbox": "^1.0.4",
"@radix-ui/react-popover": "^1.0.7",
"@sentry/cloudflare": "8.35.0",
"@sentry/nextjs": "8.35.0",
"@sindresorhus/fnv1a": "^3.1.0",
"@tailwindcss/container-queries": "^0.1.1",
+20 -6
View File
@@ -1,13 +1,10 @@
// @ts-ignore
import nextOnPagesHandler from '@cloudflare/next-on-pages/fetch-handler';
import { withSentry } from '@sentry/cloudflare';
import { withMiddlewareHeadersStorage } from './lib/middleware';
/**
* We use a custom entrypoint until we can move to opennext (https://github.com/opennextjs/opennextjs-cloudflare/issues/92).
* There is a bug in next-on-pages where headers can't be set on the response in the middleware for RSC requests (https://github.com/cloudflare/next-on-pages/issues/897).
*/
export default {
const exportedHandler = {
async fetch(request, env, ctx) {
const response = await withMiddlewareHeadersStorage(() =>
nextOnPagesHandler.fetch(request, env, ctx),
@@ -15,4 +12,21 @@ export default {
return response;
},
} as ExportedHandler<{ ASSETS: Fetcher }>;
} satisfies ExportedHandler<{ ASSETS: Fetcher }>;
const dsn = process.env.SENTRY_DSN;
/**
* We use a custom entrypoint until we can move to opennext (https://github.com/opennextjs/opennextjs-cloudflare/issues/92).
* There is a bug in next-on-pages where headers can't be set on the response in the middleware for RSC requests (https://github.com/cloudflare/next-on-pages/issues/897).
*/
export default dsn
? withSentry(
() => ({
dsn,
tracesSampleRate: 0,
}),
// @ts-ignore
exportedHandler,
)
: exportedHandler;
-15
View File
@@ -1,15 +0,0 @@
import * as Sentry from '@sentry/nextjs';
import { Instrumentation } from 'next';
export async function register() {
console.log('Instrumentation.register');
await import('./sentry');
}
export const onRequestError: Instrumentation.onRequestError = async (...args) => {
console.log('Instrumentation.onRequestError');
Sentry.captureRequestError(...args);
// There is an issue on Cloudflare that requires us to flush the events manually.
// https://github.com/getsentry/sentry-javascript/issues/14931#issuecomment-2577640023
await Sentry.flush();
};
-3
View File
@@ -97,9 +97,6 @@ export type LookupResult = PublishedContentWithCache & {
* The middleware also takes care of persisting the visitor authentication state.
*/
export async function middleware(request: NextRequest) {
// Instrumentation is not loaded correctly...
await import('./sentry');
const { url, mode } = getInputURL(request);
setTag('url', url.toString());
-11
View File
@@ -1,11 +0,0 @@
import * as Sentry from '@sentry/nextjs';
const dsn = process.env.SENTRY_DSN;
if (dsn) {
console.log('Initializing Sentry with DSN:', dsn);
Sentry.init({
dsn,
// Disable tracing as it creates additional requests in an env where subrequests are limited.
tracesSampleRate: 0,
});
}