Skip to content

Commit

Permalink
migrate sentry to react router v7
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtsam committed Jan 16, 2025
1 parent 94eeb89 commit b23d043
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 122 deletions.
7 changes: 6 additions & 1 deletion app/components/error-boundary.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type ReactElement } from 'react'
import * as Sentry from '@sentry/react'
import { useEffect, type ReactElement } from 'react'
import {
Link,
isRouteErrorResponse,
Expand Down Expand Up @@ -40,6 +41,10 @@ export const GeneralErrorBoundary = ({
console.error(error)
}

useEffect(() => {
Sentry.captureException(error)
}, [error])

return (
<div className="container flex items-center justify-center py-20">
{isRouteErrorResponse(error)
Expand Down
95 changes: 0 additions & 95 deletions app/entry.server.backup.tsx

This file was deleted.

6 changes: 3 additions & 3 deletions app/entry.server.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PassThrough } from 'node:stream'

import { createReadableStreamFromReadable } from '@react-router/node'
import { captureException, captureRemixServerException } from '@sentry/remix'
import * as Sentry from '@sentry/node'
import chalk from 'chalk'
import { isbot } from 'isbot'
import { renderToPipeableStream } from 'react-dom/server'
Expand Down Expand Up @@ -87,9 +87,9 @@ export function handleError(

if (error instanceof Error) {
console.error(chalk.red(error.stack))
void captureRemixServerException(error, 'remix.server', request)
Sentry.captureException(error)
} else {
console.error(chalk.red(error))
captureException(error)
Sentry.captureException(error)
}
}
11 changes: 3 additions & 8 deletions app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { captureRemixErrorBoundaryError, withSentry } from '@sentry/remix'
import * as Sentry from '@sentry/react'
import clsx from 'clsx'
import {
data,
Expand Down Expand Up @@ -212,7 +212,7 @@ function AppWithProviders() {
)
}

export default withSentry(AppWithProviders)
export default Sentry.wrapUseRoutesV7(AppWithProviders)

export function Layout({ children }: { children: React.ReactNode }) {
const data = useRouteLoaderData<typeof loader>('root')
Expand All @@ -226,9 +226,4 @@ export function Layout({ children }: { children: React.ReactNode }) {
)
}

export function ErrorBoundary() {
const error = useRouteError()
captureRemixErrorBoundaryError(error)

return <GeneralErrorBoundary />
}
export const ErrorBoundary = GeneralErrorBoundary
26 changes: 14 additions & 12 deletions app/utils/monitoring.client.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import {
init as sentryInit,
browserTracingIntegration,
replayIntegration,
browserProfilingIntegration,
} from '@sentry/remix'
import * as Sentry from '@sentry/react'
import { useEffect } from 'react'
import { useLocation, useMatches } from 'react-router'
import {
useLocation,
useNavigationType,
createRoutesFromChildren,
matchRoutes,
} from 'react-router'

export function init() {
sentryInit({
Sentry.init({
dsn: window.ENV.SENTRY_DSN,
tracesSampleRate: 1,
profilesSampleRate: 1,
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1,

integrations: [
browserTracingIntegration({
Sentry.replayIntegration(),
Sentry.browserProfilingIntegration(),
Sentry.reactRouterV7BrowserTracingIntegration({
useEffect,
useLocation,
useMatches,
useNavigationType,
createRoutesFromChildren,
matchRoutes,
}),
replayIntegration(),
browserProfilingIntegration(),
],
})
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
"@react-router/remix-config-routes-adapter": "0.0.0-nightly-bf7ecb711-20240911",
"@react-router/remix-routes-option-adapter": "7.1.1",
"@react-router/serve": "^7.0.0",
"@sentry/node": "8.50.0",
"@sentry/profiling-node": "8.47.0",
"@sentry/react": "8.50.0",
"@sentry/remix": "8.47.0",
"@sentry/vite-plugin": "2.22.7",
"@testing-library/dom": "10.4.0",
Expand Down
Loading

0 comments on commit b23d043

Please sign in to comment.