Nextjs App Router Auto instrumentation (How does it work?) #13442
-
IntroI am using Sentry for the first time in my Nextjs application with the new App router v14.2.5. I have followed the nextjs wizard and read the manual setup guide to understand how to use Sentry with nextjs. As of my understanding sentry will automatically report errors in server components, but for server actions I have to wrap with a special function from sentry. I have tested that errors thrown in actions are reported to sentry. This is all fine and good. Errors in server components on the other hand.... My IssueI am having a hard time catching errors in server components. First I thought sentry would automatically "just work", but this was not the case. The page I was trying to test errors on had a // app/page.tsx
export default async function Page(){
const user = await getUserFromDb()
return <NestedServerComponent />
} // app/_shared/NestedServerComponent.server.tsx
export default async function NestedServerComponent(){
// Do some async stuff and maybe throw an error
return <ClientComponents />
}
wrapServerComponentWithSentry functionI also found that sentry exports a function called wrapServerComponentWithSentry that is not documented.
Is it something wrong with my setup or can someone confirm that Sentry should be able to catch errors in nested server components? Me personally don't like this magic, I would like to read how the magic works? How does sentry know if its a server component and auto wraps it? Does it only wrap server components in page.tsx files? Another issue (maybe relevant)When building nextjs and when running in development i get theses wierd warnings from sentry.
If this is why sentry doesn't report the errors it would be nice if it said so aswell. hehe... |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 11 replies
-
After looking at the source code I see this regex in the webpack config fo nextjs sentry plugin: const isServerComponentResource = (resourcePath: string): boolean => {
const normalizedAbsoluteResourcePath = normalizeLoaderResourcePath(resourcePath);
// ".js, .jsx, or .tsx file extensions can be used for Pages"
// https://beta.nextjs.org/docs/routing/pages-and-layouts#pages:~:text=.js%2C%20.jsx%2C%20or%20.tsx%20file%20extensions%20can%20be%20used%20for%20Pages.
return (
appDirPath !== undefined &&
normalizedAbsoluteResourcePath.startsWith(appDirPath + path.sep) &&
!!normalizedAbsoluteResourcePath.match(
// eslint-disable-next-line @sentry-internal/sdk/no-regexp-constructor
new RegExp(`[\\\\/](page|layout|loading|head|not-found)\\.(${pageExtensionRegex})$`),
)
);
}; Does this mean it only wrapps server components with the names |
Beta Was this translation helpful? Give feedback.
Sorry for the late reply! Yes that issue you linked is very much the culprit of your findings and a limitation by Next.js which they will resolve in Next.js@15 :) The canary also already has the feature shipped necessary to collect server component errors. This is the relevant discussion in the Next.js Repo: vercel/next.js#67149
We also point this out in our docs but it's maybe a bit hard to spot: https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#report-react-component-render-errors