Build Options for Next.js

Learn about configuration options used in your Next.js configuration to control your app's build process.

The Sentry Next.js SDK supports automatic code injection and source map upload during your app's build process using the withSentryConfig wrapper in your Next.js configuration file (next.config.js or next.config.mjs). For information on updating the configuration, see the Manual Setup guide.

    org

    Typestring

    The slug of the Sentry organization associated with the app.

    project

    Typestring

    The slug of the Sentry project associated with the app.

    authToken

    Typestring

    The authentication token to use for all communication with Sentry. Can be obtained from https://sentry.io/orgredirect/organizations/:orgslug/settings/auth-tokens/.

    sentryUrl

    Typestring
    Defaulthttps://sentry.io/

    The base URL of your Sentry instance. Use this if you are using a self-hosted or Sentry instance other than sentry.io.

    headers

    TypeRecord<string, string>

    Headers added to every outgoing network request.

    telemetry

    Typeboolean
    Defaulttrue

    If set to true, internal plugin errors and performance data will be sent to Sentry.

    At Sentry we like to use Sentry ourselves to deliver faster and more stable products. We're very careful of what we're sending. We won't collect anything other than error and high-level performance data. We will never collect your code or any details of the projects in which you're using this plugin.

    silent

    Typeboolean
    Defaultfalse

    Suppresses all Sentry SDK build logs.

    debug

    Typeboolean
    Defaultfalse

    Prints additional debug information about the SDK and uploading source maps when building the application.

    errorHandler

    Type(error: Error) => void

    A callback function that will be invoked when errors occur during the Sentry build process. This is particularly useful for gracefully handling CI/CD pipeline failures when there are connectivity issues. You can optionally re-throw the error to fail the build process.

    Copied
    withSentryConfig(nextConfig, {
      // ... other options
      errorHandler: (error) => {
        console.warn("Sentry build error occurred:", error);
        // Optionally, you can still fail the build by re-throwing the error
        // throw error;
      },
    });
    

    sourcemaps.disable

    Typeboolean

    Disable any functionality related to source maps.

    sourcemaps.assets

    Typestring | string[]

    A glob or an array of globs that specifies the build artifacts that should be uploaded to Sentry. If not specified, the plugin will try to upload all JavaScript files and source map files created during build.

    The globbing patterns follow the implementation of the glob package.

    sourcemaps.ignore

    Typestring | string[]
    Default[]

    A glob or an array of globs that specifies which build artifacts should not be uploaded to Sentry.

    sourcemaps.deleteSourcemapsAfterUpload

    Typeboolean
    Defaulttrue

    Toggle whether generated client-side source maps within your Next.js build folder will be automatically deleted after being uploaded to Sentry. When enabled, source maps in .next/static/ are deleted but server-side source maps (in .next/server/) are intentionally kept because:

    • They are required for server-side error reporting at runtime.
    • They are not a security concern since they are not publicly accessible to users.

    To customize which files are deleted after upload (for example, to include additional paths), use the sourcemaps.filesToDeleteAfterUpload option instead.

    sourcemaps.filesToDeleteAfterUpload

    Typestring | string[]

    An array of glob patterns that specifies which source map files will be deleted after being uploaded to Sentry. When set, this overrides the default deletion behavior of deleteSourcemapsAfterUpload.

    Use this option when you need fine-grained control over which source maps are deleted. Make sure to also enable hidden source maps for any files you plan to delete, otherwise your build output will contain broken sourceMappingURL references.

    Copied
    withSentryConfig(nextConfig, {
      sourcemaps: {
        filesToDeleteAfterUpload: [".next/static/**/*.map"],
      },
    });
    

    release.name

    Typestring

    Unique identifier for the release you want to create. Defaults to automatically detecting a value for your environment.

    release.create

    Typeboolean
    Defaulttrue

    Whether the plugin should create a release on Sentry during the build.

    release.finalize

    Typeboolean
    Defaulttrue

    Whether the Sentry release should be automatically finalized after the build ends.

    release.dist

    Typestring

    Unique identifier for the distribution, used to further segment your release. Usually your build number.

    bundleSizeOptimizations.excludeDebugStatements

    Typeboolean

    If set to true, the Sentry SDK will attempt to tree-shake any debugging code within itself during the build.

    bundleSizeOptimizations.excludeTracing

    Typeboolean

    If set to true, the Sentry SDK will attempt to tree-shake code related to tracing and performance monitoring.

    Notice: Do not enable this when using any performance monitoring-related SDK features.

    widenClientFileUpload

    Typeboolean
    Defaultfalse

    Include Next.js-internal code and code from dependencies when uploading source maps.

    When and why to widen the upload scope.

    If you find that there are some frames in your client-side stack traces that aren't getting source-mapped even when most others are, the issue might be that those frames are from files in static/chunks/ rather than static/chunks/pages/. By default, such files aren't uploaded because the majority of the files in static/chunks/ only contain Next.js or third-party code. To upload all of the files and source maps, including ones from third-party packages, set the widenClientFileUpload option to true.

    Note: Enabling this option can lead to longer build times.

    tunnelRoute

    Typestring | boolean

    Tunnel Sentry requests through this route on your Next.js server to prevent ad-blockers from blocking Sentry events from being sent.

    This option can be set to:

    • true for auto-generated routes, which are unpredictable and change with each deployment.
    • A custom static string path like /error-monitoring.
    If your middleware blocks requests without a session

    Your middleware (proxy.ts or middleware.ts) also runs for tunnel requests. On v10, the SDK skips your middleware only in rewrite mode with Webpack. Exclude the tunnel route in its matcher. This needs a fixed string, because true creates a new path for each build:

    proxy.ts
    Copied
    export const config = {
      matcher: [
        "/((?!error-monitoring|_next/static|_next/image|favicon.ico).*)",
      ],
    };
    

    Learn more about tunneling in the troubleshooting section.

    The tunnel route works in one of two modes:

    • Rewrite mode (default): A Next.js rewrite proxies each request to Sentry with all its request headers. Works only with Sentry SaaS DSNs.
    • Handler mode: A route handler in your app forwards only the request body to Sentry. Works with any DSN.

    Starting with versions 10.77.1 (v10) and 11.9.0 (v11), the browser sends tunnel requests in rewrite mode with credentials: "omit". If a layer in front of your app requires credentials (for example, Vercel Deployment Protection or HTTP Basic authentication), it rejects these requests. Use handler mode in this case. You can also set credentials: "same-origin", but rewrite mode then forwards the credentials to Sentry:

    instrumentation-client.ts
    Copied
    Sentry.init({
      dsn: "___PUBLIC_DSN___",
      transportOptions: {
        fetchOptions: { credentials: "same-origin" },
      },
    });
    

    Handler mode

    Available since: v10.77.1 (v10) and v11.9.0 (v11)

    Keep tunnelRoute and add this file. If your app uses a src directory, add it at src/app/sentry-tunnel/route.ts:

    app/sentry-tunnel/route.ts
    Copied
    export { POST } from "@sentry/nextjs/tunnel";
    

    This works with any tunnelRoute value, including true. When the SDK finds the file, the build prints this line:

    Copied
    [@sentry/nextjs] The tunnel route sends events through app/sentry-tunnel/route.ts. Only the event payload goes to Sentry.
    

    If the line is missing, the SDK uses rewrite mode.

    The handler accepts only the DSN of your server-side SDK. It rejects other DSNs with a 403 and requests larger than 20 MiB with a 413. If your browser SDK uses a different DSN, list the allowed DSNs:

    app/sentry-tunnel/route.ts
    Copied
    import { createSentryTunnelRoute } from "@sentry/nextjs/tunnel";
    
    export const { POST } = createSentryTunnelRoute({
      allowedDsns: ["___PUBLIC_DSN___"],
    });
    
    Limits of handler mode
    • Handler mode works only with the App Router.
    • The SDK doesn't find the file in a route group, such as app/(group)/sentry-tunnel/.
    • With "moduleResolution": "node" in tsconfig.json, the type check fails with Cannot find module '@sentry/nextjs/tunnel'. Set moduleResolution to bundler, node16, or nodenext.
    • Each event request runs the route handler. On serverless hosts, this is one function invocation for each request.

    useRunAfterProductionCompileHook

    Typeboolean
    Defaultfalse(webpack)|true(turbopack)

    Enables the use of the runAfterProductionCompile hook from Next.js to upload sourcemaps after the build is completed.

    • This option is set to true by default for Turbopack as there are no alternative ways to upload sourcemaps here.
    • This option is set to false for Webpack as the default behavior is to upload sourcemaps during the build process using the Sentry Webpack Plugin.

    Important: Enabling this option will mutate your Next.js build output by injecting Debug IDs via the Sentry CLI. If you are relying on any sort of integrity hashes for your build artifacts, you will need to disable this option.

    routeManifestInjection

    Typeboolean|object
    Defaulttrue

    Available since: v10.34.0

    Controls injection and filtering of the route manifest in the client bundle.

    The route manifest is a build-time generated mapping of your Next.js App Router routes that enables Sentry to group transactions by parameterized route names (for example, /users/:id instead of /users/123 or /users/456).

    You can set this option to false to disable the route manifest injection.

    Disable this option if:

    • You want to minimize client bundle size
    • You're experiencing build issues related to route scanning
    • You prefer raw URLs in transaction names

    You can also pass in an object with an exclude property to control which routes should be excluded from the route manifest. The exclude property accepts an array of strings or regular expressions, or a function that returns true to exclude a route.

    Copied
    withSentryConfig(nextConfig, {
      // Exclude specific routes using an array of strings or RegExps
      routeManifestInjection: {
        exclude: ["/api/health", "/api/excluded/[parameter]", /^\/internal\//],
      },
    });
    

    Excluded routes will appear as raw URLs in transaction names instead of parameterized routes.

    Use exclude if:

    • You want to hide internal or unreleased routes from appearing in the client bundle
    • You want to reduce bundle size by excluding routes that don't benefit from parameterized grouping (for example, static routes with no dynamic segments)

    reactComponentAnnotation

    Typeobject

    Annotates your React components with data-sentry-component, data-sentry-element, and data-sentry-source-file attributes at build time, so Sentry can identify which component a user interacted with in Session Replay and breadcrumbs.

    Disabled unless you set a value. Set enabled: true to turn it on, and ignoredComponents to skip components by name.

    This option drives both Webpack and Turbopack builds. On Turbopack it requires Next.js 16+; on an older version the SDK warns at build time.

    next.config.ts
    Copied
    import { withSentryConfig } from "@sentry/nextjs/config";
    
    export default withSentryConfig(nextConfig, {
      reactComponentAnnotation: {
        enabled: true,
        ignoredComponents: ["SensitiveForm", "InternalDebugPanel"],
      },
    });
    

    applicationKey

    Typestring

    Application key used by thirdPartyErrorFilterIntegration to tell your code apart from third-party code, so you can drop errors coming from browser extensions, injected scripts, and third-party widgets.

    The value must match an entry in the filterKeys array of your thirdPartyErrorFilterIntegration configuration. This option works for both Webpack and Turbopack builds.

    next.config.ts
    Copied
    import { withSentryConfig } from "@sentry/nextjs/config";
    
    export default withSentryConfig(nextConfig, {
      applicationKey: "my-nextjs-app",
    });
    

    buildTimeInstrumentation

    Typeboolean
    Defaulttrue

    Automatically instrument server-side dependencies at build time. This is what gives you tracing for libraries that can't be patched at runtime, including on platforms like Vercel and Netlify.

    Set to false to turn it off. Turbopack support requires Next.js 16+; the Webpack path works on earlier versions.

    webpack.autoInstrumentServerFunctions

    Typeboolean
    Defaulttrue

    Automatically instrument Next.js data fetching methods and Next.js API routes with error and performance monitoring.

    webpack.autoInstrumentMiddleware

    Typeboolean
    Defaulttrue

    Automatically instrument Next.js middleware with error and performance monitoring.

    webpack.autoInstrumentAppDirectory

    Typeboolean
    Defaulttrue

    Automatically instrument components in the app directory with error monitoring.

    webpack.excludeServerRoutes

    TypeArray<RegExp | string>

    Exclude specific server-side API routes or pages from automatic Sentry instrumentation during build time. This option takes an array of strings or regular expressions and affects pages in the pages and app directories.

    When defining routes, note the following:

    • Specify pages as routes and not as file system paths. For example, write /animals instead of pages/animals/index.js.
    • Make sure that any provided string matches the route exactly, has a leading slash, and doesn't have a trailing slash.
    Copied
    webpack.excludeServerRoutes: [
      "/some/excluded/route",
      "/excluded/route/with/[parameter]",
      /^\/route\/beginning\/with\/some\/prefix/,
      /\/routeContainingASpecificPathSegment\/?/,
    ];
    

    webpack.automaticVercelMonitors

    Typeboolean
    Defaultfalse

    Automatically create cron monitors in Sentry for your Vercel Cron Jobs if configured via vercel.json.

    webpack.treeshake

    Typeobject

    Configuration options for tree shaking. Refer to the tree shaking documentation for more details.

    _experimental.vercelCronsMonitoring

    Typeboolean
    Defaultfalse

    Automatically create Cron Monitors in Sentry for the Vercel Cron Jobs configured in your vercel.json, by detecting cron requests and emitting check-ins from spans.

    Unlike webpack.automaticVercelMonitors, which wraps route handlers at build time, this works with both Webpack and Turbopack and with both the App Router and the Pages Router. If you enable both, the SDK uses this one and warns that the other is redundant.

    next.config.ts
    Copied
    import { withSentryConfig } from "@sentry/nextjs/config";
    
    export default withSentryConfig(nextConfig, {
      _experimental: {
        vercelCronsMonitoring: true,
      },
    });
    
    Was this helpful?
    Help improve this content
    Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").