@sentry/nextjs and nextjs custom server
Hi,
Are there any documentation on how to setup @sentry/nextjs with https://nextjs.org/docs/advanced-features/custom-server ?
Thanks!
That's not a use case we've investigated yet, so no, there aren't currently any docs. For API routes, nothing should be different - withSentry should work out of the box. Non-API routes, on the other hand, I would expect not to be traced or have their errors caught, as the code we have for that relies on monkeypatching the built-in server.
Before I give any advice on how to implement that yourself, though, I'm actually planning this sprint or next to re-evaluate the way that we do that, as there appear to be environments where it only works inconsistently. So for the moment, I'd suggest starting with your API routes (and your frontend). Once I get a better handle on whether we might take a different approach to non-API routes, I'll better be able to advise you on that score.
This issue has gone three weeks without activity. In another week, I will close it.
But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!
"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀
Instead of adopting our SDK, I suggest we treat this a combination of @sentry/node + @sentry/react or @sentry/nextjs. It could be sufficient to update our docs to describe this use case.
This will probably give up on some (small) functionality using a custom server which should be fine as a custom server itself reduces the functionality of nextjs.
Hey @vladanpaunovic so if I only need this for api routes, will the @sentry/next package work out of the box?
@rbabayoff I will assume that by "this" you meant a custom server.
If so, then @sentry/nextjs on client + @sentry/node on your server should do the job.
@sentry/nextjs will cover the frontend and most of the features from Next.js. If that proves to be buggy for your custom use case, I suggest you fall back to @sentry/react for a more controlled but also more manual instrumentation.
@sentry/node will instrument your custom node server
Hey @vladanpaunovic , thanks for your answer. My use case is a bit more complicated - in the same app, one of my endpoints is a long running api endpoint for file uploads, which I need to run on a custom server. Everything else should work on vercel.
A couple of questions:
-
How do I force the upload of source maps when I run next build when using only @sentry/nextjs? It doesn't seem to happen.
-
I want to keep @sentry/nextjs for the vercel deployment, but when running
next buildwhen deploying the custom server, I'd like to be able to replace all @sentry/nextjs on the server side with @sentry/node and still force source map uploads.
Will this work with a custom webpack config in the next.config.js that sentry generated with npx @sentry/wizard -i nextjs (EF_IS_UPLOAD_SERVER is our env var for custom server builds):
const moduleExports = {
webpack: (config, options) => {
// Next.js will call this webpack function twice, once for the
// server and once for the client. Read more:
// https://nextjs.org/docs/api-reference/next.config.js/custom-webpack-config
if (process.env.EF_IS_UPLOAD_SERVER && options.isServer) {
config.resolve.alias["@sentry/nextjs"] = "@sentry/node";
}
return config;
},
};
Thanks in advance
Hey @vladanpaunovic , simplifying my question - how can I force the generation and upload of source maps to sentry while still using @sentry/nextjs and next build, taking into account that I'll use the custom server only for api endpoints?
I think what you can do is play a bit with sentry-webpack-plugin configuration. We have a docs on source maps here https://docs.sentry.io/platforms/javascript/sourcemaps/generating/ but you can always check the repo https://github.com/getsentry/sentry-webpack-plugin
This issue has gone three weeks without activity. In another week, I will close it.
But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!
"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀