sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

@sentry/nextjs and nextjs custom server

Open marexandre opened this issue 4 years ago • 8 comments

Hi,

Are there any documentation on how to setup @sentry/nextjs with https://nextjs.org/docs/advanced-features/custom-server ?

Thanks!

marexandre avatar Jul 07 '21 20:07 marexandre

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.

lobsterkatie avatar Jul 28 '21 06:07 lobsterkatie

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 🥀

github-actions[bot] avatar Oct 14 '21 12:10 github-actions[bot]

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.

vladanpaunovic avatar Jun 08 '22 13:06 vladanpaunovic

Hey @vladanpaunovic so if I only need this for api routes, will the @sentry/next package work out of the box?

rbabayoff avatar Jul 05 '22 01:07 rbabayoff

@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

vladanpaunovic avatar Jul 05 '22 08:07 vladanpaunovic

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:

  1. 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.

  2. I want to keep @sentry/nextjs for the vercel deployment, but when running next build when 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

rbabayoff avatar Jul 10 '22 22:07 rbabayoff

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?

rbabayoff avatar Jul 17 '22 15:07 rbabayoff

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

vladanpaunovic avatar Aug 10 '22 11:08 vladanpaunovic

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 🥀

github-actions[bot] avatar Jan 21 '23 00:01 github-actions[bot]