Skip to content

Commit c7da7cf

Browse files
lforstimatwawana
andauthored
Add docs for Next.js SDK option autoInstrumentServerFunctions (#5542)
Co-authored-by: Isabel <[email protected]>
1 parent c823b1c commit c7da7cf

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/platforms/javascript/guides/nextjs/manual-setup.mdx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ const moduleExports = {
183183
// sections below for information on the following options:
184184
// - disableServerWebpackPlugin
185185
// - disableClientWebpackPlugin
186+
// - autoInstrumentServerFunctions
186187
// - hideSourceMaps
187188
// - widenClientFileUpload
188189
// - transpileClientSDK
@@ -241,6 +242,54 @@ module.exports = withSentryConfig(moduleExports);
241242

242243
In that case you can also skip the `sentry-cli` configuration step below.
243244

245+
### Automatically Instrument API Routes and Data Fetching Methods
246+
247+
_(New in version 7.14.0)_
248+
249+
The SDK provides an option to automatically instrument API routes and server-side [Next.js data fetching methods](https://nextjs.org/docs/basic-features/data-fetching/overview) with error and performance monitoring, removing the need to manually wrap API routes in `withSentry`:
250+
251+
```javascript {filename:next.config.js}
252+
const moduleExports = {
253+
sentry: {
254+
autoInstrumentServerFunctions: true,
255+
},
256+
};
257+
```
258+
259+
Under the hood, the SDK is using a Webpack loader to wrap all your API route handlers and data fetching methods.
260+
261+
If the automatic instrumentation doesn't work for your use case, API routes can also be wrapped manually using the `withSentry` function:
262+
263+
```javascript {filename:pages/api/*}
264+
import { withSentry } from "@sentry/nextjs";
265+
266+
const handler = (req, res) => {
267+
res.status(200).json({ name: "John Doe" });
268+
};
269+
270+
export default withSentry(handler);
271+
```
272+
273+
```typescript {filename:pages/api/*}
274+
import type { NextApiRequest, NextApiResponse } from "next"
275+
import { withSentry } from "@sentry/nextjs";
276+
277+
const handler = (req: NextApiRequest, res: NextApiResponse) => {
278+
res.status(200).json({ name: "John Doe" });
279+
};
280+
281+
export default withSentry(handler);
282+
```
283+
284+
Data fetching methods can also be manually wrapped using the following functions:
285+
286+
- `withSentryServerSideGetInitialProps` for `getInitialProps`
287+
- `withSentryGetServerSideProps` for `getServerSideProps`
288+
- `withSentryGetStaticProps` for `getStaticProps`
289+
- `withSentryServerSideErrorGetInitialProps` for `getInitialProps` in [custom Error pages](https://nextjs.org/docs/advanced-features/custom-error-page)
290+
- `withSentryServerSideAppGetInitialProps` for `getInitialProps` in [custom `App` components](https://nextjs.org/docs/advanced-features/custom-app)
291+
- `withSentryServerSideDocumentGetInitialProps` for `getInitialProps` in [custom `Document` components](https://nextjs.org/docs/advanced-features/custom-document)
292+
244293
### Use `hidden-source-map`
245294

246295
_(New in version 6.17.1, will default to `true` in 8.0.0 and beyond.)_

0 commit comments

Comments
 (0)