|
| 1 | +# Research: Improving `@flareapp/react` Context Collection |
| 2 | + |
| 3 | +## Current State |
| 4 | + |
| 5 | +The `FlareErrorBoundary` currently captures: |
| 6 | + |
| 7 | +- The error object |
| 8 | +- `componentStack` string from React's `ErrorInfo` (formatted) |
| 9 | +- Standard browser context via `@flareapp/js` (URL, user agent, cookies, query params) |
| 10 | + |
| 11 | +That's about it. There's room to grow. |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## Industry Landscape |
| 16 | + |
| 17 | +| Feature | Industry standard | Flare | |
| 18 | +|--------------------------------|-------------------|---------| |
| 19 | +| Component stack | Yes | Yes | |
| 20 | +| Component names in breadcrumbs | Some (via plugins)| No | |
| 21 | +| Component props/state | **No** | **No** | |
| 22 | +| Redux/store state | Some | No | |
| 23 | +| React Router integration | Some | No | |
| 24 | +| React 19 root error handlers | Emerging | No | |
| 25 | +| Component interaction trail | Rare | No | |
| 26 | +| `beforeCapture` scope hook | Rare | No | |
| 27 | +| User feedback dialog | Rare | No | |
| 28 | +| Component profiling | Rare | No | |
| 29 | + |
| 30 | +**Key finding: Nobody automatically captures component props or state.** Despite the theoretical possibility via React |
| 31 | +fiber internals, every tool either ignores these or provides manual hooks. Reasons: serialization complexity (circular |
| 32 | +refs, functions, React elements), performance cost of fiber tree traversal, and privacy risks (leaking PII/tokens). |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +## What's Worth Implementing (Prioritized) |
| 37 | + |
| 38 | +### Tier 1 -- Baseline improvements (low effort, high value) |
| 39 | + |
| 40 | +1. **Better component stack formatting and sourcemap-decoded display** -- currently just splitting by newline. Could |
| 41 | + parse into structured data (component name, file, line) for better Flare dashboard rendering. |
| 42 | + |
| 43 | +2. **`beforeCapture` callback on ErrorBoundary** -- a `beforeCapture(scope, error, componentStack)` hook. Lets |
| 44 | + users attach custom tags, component props, or state to the error before it's sent. This is the pragmatic answer to " |
| 45 | + how do we capture state" -- let the developer decide what to include. |
| 46 | + |
| 47 | +3. **React 19 `createRoot` error handlers** -- provide a `flareReactErrorHandler()` function for the new |
| 48 | + `onCaughtError`/`onUncaughtError`/`onRecoverableError` hooks. Few tools support this today. Gets you |
| 49 | + error capture *without* requiring an ErrorBoundary wrapper. |
| 50 | + |
| 51 | +### Tier 2 -- Differentiating features (moderate effort) |
| 52 | + |
| 53 | +4. **Component name annotation via build plugin** -- inject component name and source file attributes onto DOM nodes at |
| 54 | + build time (e.g. `data-flare-component`, `data-flare-source-file`). These survive minification. Since Flare already |
| 55 | + has `@flareapp/vite`, this could be a Vite plugin addition or a separate Babel plugin. Component names would then |
| 56 | + appear in breadcrumbs/glows. |
| 57 | + |
| 58 | +5. **React Router integration** -- capture parameterized route names (`/users/:id` instead of `/users/12345`) as |
| 59 | + context. Add navigation breadcrumbs on route changes. Support React Router v6/v7 and TanStack Router. |
| 60 | + |
| 61 | +6. **State management integration** -- provide a Redux middleware and/or Zustand middleware that attaches store state |
| 62 | + snapshots to error reports. Use a `stateTransformer` for sanitization. |
| 63 | + This is where "capture state" actually becomes practical -- at the store level, not the component level. |
| 64 | + |
| 65 | +### Tier 3 -- Nice-to-have (higher effort) |
| 66 | + |
| 67 | +7. **Component interaction breadcrumbs** -- track which components the user interacted with before the error. This gives |
| 68 | + a "user journey through components" trail. |
| 69 | + |
| 70 | +8. **Component profiling** -- `withProfiler()` HOC / `useProfiler()` hook that tracks mount/render/update timing. Useful |
| 71 | + for correlating errors with performance, but requires a tracing infrastructure. |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +## What's NOT Worth Pursuing |
| 76 | + |
| 77 | +- **Automatic props/state via fiber internals** -- no tool does this reliably. Internal API instability across React |
| 78 | + versions, serialization nightmares, privacy risks, performance cost. The fiber tree (`memoizedProps`, `memoizedState`) |
| 79 | + is unstable and undocumented. |
| 80 | +- **`captureOwnerStack()`** -- React 19 API that returns `null` in production. Development-only, useless for error |
| 81 | + tracking. |
| 82 | +- **Session replay** -- massive scope, framework-agnostic, not a React package concern. |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +## Recommended Implementation Order |
| 87 | + |
| 88 | +For the `@flareapp/react` package specifically: |
| 89 | + |
| 90 | +1. `beforeCapture` callback on `FlareErrorBoundary` -- quick win, gives users control |
| 91 | +2. React 19 `createRoot` error handler utility -- forward-looking, few tools support this yet |
| 92 | +3. Structured component stack parsing -- better data for the Flare dashboard |
| 93 | +4. Vite/Babel plugin for component name annotations -- builds on existing `@flareapp/vite` |
| 94 | +5. React Router integration (v6/v7) |
| 95 | +6. Redux/Zustand middleware (could be separate packages) |
| 96 | + |
| 97 | +## Tasks |
| 98 | + |
| 99 | +- [x] FlareErrorBoundary supports onError callback |
0 commit comments