Nuxt3 on cloudflare pages #9209
Replies: 4 comments 4 replies
-
Hey @dreitzner thanks for posting your findings! I'm sure this is helpful for people until we figure get to work on proper Nuxt and Cloudflare support. Will also share this with my team. |
Beta Was this translation helpful? Give feedback.
-
Your placement did not worked for me, maybe it worked in some past version. This is my config I currently useI also enabled more features: server/plugins/sentry.ts
nuxt.config.ts
|
Beta Was this translation helpful? Give feedback.
-
Worked a bit further based on @iBobik's version, here is what I'm using: import type { H3Event } from "h3";
import { H3Error } from "h3";
import { Toucan } from "toucan-js";
export default defineNitroPlugin((nitroApp) => {
const { app: { version }, sentry: { dsn } } = useRuntimeConfig().public;
const setupSentry = async (event?: H3Event) => {
const sentry = new Toucan({
enabled: !import.meta.dev,
dsn,
release: version,
environment: import.meta.dev ? "dev" : "prod",
context: event,
request: event ? toWebRequest(event) : undefined,
requestDataOptions: {
allowedSearchParams: true,
allowedHeaders: true,
},
sampleRate: 0.7,
tracesSampleRate: 0.2,
});
if (event && ["POST", "PUT", "PATCH"].includes(event.method)) {
const rawBody = await readRawBody(event);
sentry.setRequestBody(rawBody);
}
sentry.setTag("server", true);
return sentry;
};
nitroApp.hooks.hook("error", async (error, { event }) => {
const ignoreErrors = [401, 404, 429];
if (error instanceof H3Error) {
if (ignoreErrors.includes(error.statusCode))
return;
}
const sentry = await setupSentry(event);
sentry.captureException(error);
});
nitroApp.hooks.hook("request", async (event) => {
event.context.$sentry = await setupSentry(event);
});
}); |
Beta Was this translation helpful? Give feedback.
-
To get Nuxt3 working with Cloudflare Pages and Sentry, use Toucan as a Sentry client for Cloudflare Workers. The server-side integration won't work with the Node SDK on Cloudflare Pages, but Toucan is designed to handle this. Here’s how you can adapt your import { Toucan } from 'toucan-js';
export default defineNitroPlugin((nitroApp) => {
const config = useRuntimeConfig();
const sentryConfig = config.public.sentry;
nitroApp.hooks.hook('error', (err, context) => {
const sentry = new Toucan({
dsn: sentryConfig.dsn,
context: context.event,
});
sentry.setTag('server', true);
sentry.captureException(err);
});
}); This should solve the issue! |
Beta Was this translation helpful? Give feedback.
-
Hi, good to see you 🫵
I hope you didn't have to search for too long, to find the answer you are looking for.
How to I get nuxt3 with cloudflare pages and sentry working?
There is a great article that we used as a starting point.
The only issue is that this handles the server errors with the node sdk, which does no work on cloudflare pages.
Toucan to the rescue!
Toucan is a sentry client for cloudflare workers, which also works for our usecase.
So the adaptions we needed to make
server/plugin.sentry.ts
using toucan lead us to this solution:I hope this helps at least one other person. 😊
FYI: @Lms24
Beta Was this translation helpful? Give feedback.
All reactions