This document outlines the Sentry integration implemented for frontend error tracking and crash reporting.
Sentry has been integrated to automatically capture:
- Uncaught JavaScript exceptions
- React render errors
- Failed API promises
- Performance traces
- Session replays for debugging
sentry.server.config.ts- Server-side Sentry configurationsentry.client.config.ts- Client-side Sentry configurationsrc/components/ErrorBoundary.tsx- React error boundary componentsrc/app/error.tsx- Next.js 500 error pagesrc/middleware.ts- Middleware for request tracing
package.json- Added @sentry/nextjs dependency.env.example- Added NEXT_PUBLIC_SENTRY_DSN environment variablesrc/app/layout.tsx- Wrapped app with ErrorBoundary
Add the following to your .env.local file:
# Sentry DSN for error tracking
# Get this from your Sentry project settings
NEXT_PUBLIC_SENTRY_DSN=your_sentry_dsn_here-
Create a Sentry Project:
- Go to https://sentry.io
- Create a new project for "Next.js"
- Copy the DSN from your project settings
-
Configure Environment:
- Add the DSN to your
.env.localfile - Reference
.env.examplefor the required format
- Add the DSN to your
-
Install Dependencies:
npm install
-
Test the Integration:
- Run the development server
- Trigger an error to verify Sentry captures it
- Check your Sentry dashboard for the error report
- Custom ErrorBoundary component wraps the entire application
- Automatically captures React errors and sends them to Sentry
- Provides user-friendly error UI with recovery options
- Shows error details in development mode
- Next.js error.tsx page for server errors
- Automatically reports errors to Sentry
- Graceful fallback UI for users
- Traces enabled for performance monitoring
- Session replays for debugging user interactions
- Automatic breadcrumb collection
- Middleware integration for request tracing
- Continues traces across server and client
- Initializes Sentry for server-side rendering
- Configures traces sampling
- Sets debug mode appropriately
- Initializes Sentry for client-side
- Includes Replay integration for session recording
- Masks sensitive data in replays
- Configures error and session sampling rates
To test the Sentry integration:
-
Development Testing:
npm run dev
- Navigate to the app
- The ErrorBoundary will catch any React errors
- Check browser console for Sentry debug info
-
Error Simulation:
- Add
throw new Error('Test error')to a component - Verify the error appears in Sentry dashboard
- Add
-
Production Testing:
npm run build npm start
- Test in production environment
- Verify errors are captured with proper context
- Error Boundaries: Wrap critical components with ErrorBoundary for granular error handling
- Custom Context: Add custom context to Sentry reports for better debugging
- Sampling Rates: Adjust sampling rates based on traffic and Sentry plan limits
- Sensitive Data: Ensure sensitive data is masked in session replays
Once deployed, monitor:
- Error rates and trends
- Performance metrics
- User session replays
- Release health
- DSN is public but only allows error reporting
- No sensitive data should be sent to Sentry
- Session replays mask text and block media by default
- Review Sentry data retention policies