-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto Instrumentation breaks Next.js edge API config (vercel region) #7450
Comments
Hey @coding-camel! We actually had the chance to talk to the Next.js team at Vercel about this. Vercel actually uses static analysis to read this value. This means any build tool or Webpack loader/plugin that non-trivially modifies code at bundle time will trip it up. This is unfortunately what the Sentry SDK is running into. This requires Vercel to fix this upstream - I would recommend reaching out to them! Until then you have to disable the wrapping using |
@AbhiPrasad Why would Sentry need to fiddle with other exports? Shouldn't it just wrap the default export and be done? Thank you for looking into it. |
@coding-camel it shouldn't fiddle with any exports - you can see this if you tried to run a route in production and import
export const config = { runtime: 'edge', regions: 'iad1' }
export default () => {
throw new Error('Test')
}
import { config } from "./test";
export default () => {
console.log(config);
throw new Error('Test')
} It's also why using the config to set a function as sentry-javascript/packages/e2e-tests/test-applications/nextjs-app-dir/pages/api/edge-endpoint.ts Line 1 in d6561b8
Vercel should be grabbing the If you had decided to use another transform, or a custom swc plugin to build your edge function you would run into the same issue (which other users do). |
@coding-camel I've reached out to Vercel folks I have a contact with, but I recommend you also do the same if you can. Let's see if we can come up with a resolution for this problem! |
@AbhiPrasad Hey, I reached out to them, this is the response I got today.
|
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 "A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀 |
Ok, let's try to come up with a solution for this. This problem is a bit nuanced and I think I am opinionated on this so I'll try to explain the problem step by step. Maybe I'll change my mind by the end of my explanation :D What does the Sentry Next.js SDK do?In order to instrument API routes we built a webpack loader that wraps your routes with error and performance instrumentation. The loader achieves this by importing your route module from a "virtual" module, wrapping your route handler in a Sentry function, and re-exporting everything from the virtual module. Additionally, we are setting the Under the hood, we're using Rollup to take care of the bundling. The main reason for this is that rollup is very flexible and battle-tested. We have identified a lot of issues by now that we would have run into if we did manual AST manipulation to wrap the handler. How does this break the edge region config?When bundling with Rollup, we are changing how the Next.js constructs the build manifest containing the edge region by doing static code analysis on the module AFTER loaders have been applied (TODO link to relevant code section in Next.js repo, maybe Vercel folks can help out here 🙏). IIRC the pattern Next.js looks for is pretty strict so the way we transform the What possible solutions are there?There are a few possible routes to fix this, both from Next.js' side and from Sentry's side. Here's a list that is probably not complete:
Solution brainstormI see two sides to this problem. First, the Sentry SDK is breaking a feature of Next.js and we need to fix it. The second problem I see is with the static code analysis that Next.js is doing. To me personally, it seems a bit too strict. I would love your thoughts on this @leerob, @Schniz (maybe also others who can comment). Do you have any other ideas on how we could tackle this? |
Hi, I just tested this again and it looks like this resolved itself. I assume the nice folks over at Vercel improved the detection logic for this. To be sure, I still recommend upgrading the SDK to the latest version. Let me know if you still have any problems related to this issue. |
Yes we are :) Some of this is due to technical limitations but there is a lot we can improve. |
It's an item here: #6726 |
Is there an existing issue for this?
How do you use Sentry?
Sentry Saas (sentry.io)
Which SDK are you using?
@sentry/nextjs
SDK Version
7.43.0
SDK Setup
sentry.edge.config
Steps to Reproduce
Create
pages/api/test.ts
and buildExpected Result
Region should be iad1 (Washington, D.C., USA)
Actual Result
Region is Global (default)
Quick fix
If we set:
And re-deploy, Vercel shows the correct region of Washington, D.C., USA
Debugging
Running
next build
locally I looked in.next/server/middleware-manifest.json
It's missing the regions property. It should look like this:
Attempts to fix
I have also tried:
All the same result.
I also tried wrapping the function myself. But I could not get
wrapApiHandlerWithSentry
to work at all in edge routes, maybe I am doing something wrong. It did work in node routes.Am I missing something obvious? I am not sure what the root problem is but hopefully this is useful.
The text was updated successfully, but these errors were encountered: