Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use client #120

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ jobs:
- node: 21
npm: 10

continue-on-error: true

steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
46 changes: 44 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,32 @@ It is currently in a public Beta release right now as we push towards a 1.0 rele
we want to provide full capability for using React SDK in your production apps. We expect a 1.0 release to come in the
next month.

- [Key benefits of using Rollbar React are:](#key-benefits-of-using-rollbar-react-are)
- [In Beta](#in-beta)
- [Setup Instructions](#setup-instructions)
- [Prerequisites](#prerequisites)
- [Install Rollbar SDK](#install-rollbar-sdk)
- [Install Rollbar React SDK](#install-rollbar-react-sdk)
- [Usage and Reference](#usage-and-reference)
- [Simplest Usage Possible](#simplest-usage-possible)
- [Using with Next.js App Router](#using-with-nextjs-app-router)
- [Components](#components)
- [`Provider` Component](#provider-component)
- [Configuration Only Usage](#configuration-only-usage)
- [Instance Usage](#instance-usage)
- [React Native Usage](#react-native-usage)
- [`ErrorBoundary` Component](#errorboundary-component)
- [Simple Usage](#simple-usage)
- [Pass `prop`s to control behavior](#pass-props-to-control-behavior)
- [Pass a Fallback UI](#pass-a-fallback-ui)
- [`RollbarContext` Component](#rollbarcontext-component)
- [Basic Usage](#basic-usage)
- [Using with React Router](#using-with-react-router)
- [Functions](#functions)
- [`historyContext`](#historycontext)
- [`historyContext` to create `history.listener`](#historycontext-to-create-historylistener)
- [Basic `historyContext` usage](#basic-historycontext-usage)
- [Controlling `historyContext` behavior with options](#controlling-historycontext-behavior-with-options)
- [Hooks](#hooks)
- [Reliance on `Provider`](#reliance-on-provider)
- [`useRollbar` hook](#userollbar-hook)
- [`useRollbarContext` hook](#userollbarcontext-hook)
- [`useRollbarPerson` hook](#userollbarperson-hook)
Expand Down Expand Up @@ -125,6 +139,34 @@ export default function App() {
};
```

### Using with Next.js App Router

The first step to using Rollbar with the Next.js App Router is to configure your Rollbar instance.

```ts
import Rollbar from 'rollbar';

const baseConfig = {
captureUncaught: true,
captureUnhandledRejections: true,
environment: process.env.NODE_ENV,
};

export const clientConfig = {
accessToken: process.env.NEXT_PUBLIC_POST_CLIENT_ITEM_TOKEN,
...baseConfig,
};

export const serverInstance = new Rollbar({
accessToken: process.env.POST_SERVER_ITEM_TOKEN,
...baseConfig,
});
```

We suggest you create a single instance for use server side, to be certain there are not more than one, and a config to use in your client side components. React Server Components limit you to passing simple objects as props from Server Components to Client Components. The config object can be used by the Rollbar Provider to construct your Rollbar instance.

There's a fully functional [example](./examples/nextjs-approuter) that showcases the many ways in which you can integrate Rollbar into your modern web-app leveraging React Server Components and the Next.js App Router.

## Components

The following components are available as named imports from `@rollbar/react`.
Expand Down
4 changes: 4 additions & 0 deletions examples/nextjs-approuter/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"root": true,
"extends": "next/core-web-vitals"
}
36 changes: 36 additions & 0 deletions examples/nextjs-approuter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
66 changes: 66 additions & 0 deletions examples/nextjs-approuter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
```

Open [http:https://localhost:3000](http:https://localhost:3000) with your browser to see the result.

## Using Rollbar with the Next.js App Router

The first step to using Rollbar with the Next.js App Router is to configure your Rollbar instance.

```
// ./src/rollbar.ts
import Rollbar from 'rollbar';

const baseConfig = {
captureUncaught: true,
captureUnhandledRejections: true,
environment: process.env.NODE_ENV,
};

export const clientConfig = {
accessToken: process.env.NEXT_PUBLIC_POST_CLIENT_ITEM_TOKEN,
...baseConfig,
};

export const serverInstance = new Rollbar({
accessToken: process.env.POST_SERVER_ITEM_TOKEN,
...baseConfig,
});
```

We suggest you create a single instance for use server side, to be certain there are not more than one, and a config to use in your client side components. React Server Components limit you to passing simple objects as props from Server Components to Client Components. The config object can be used by the Rollbar Provider to construct your Rollbar instance.

### Configuring the Rollbar Provider

To be able to use the hooks consistently through your application. It is easiest if you configure your Rollbar Provider within your root layout.

**_Note:_** The Rollbar Provider uses a React Context. This context, like hooks are only available for use in your client components.

### Configuring the global-error handler

To be certain that you are catching all errors within your application, you will want to [configure a `global-error.js/tsx`](./src/app/global-error.tsx). This handler will catch errors, including from your root layout and relay then to Rollbar.

### Using the Next.js route error handler (Recommended)

Next.js provides an [error handler]() this handler will automatically wrap your router, at the desired level, within an [Error Boundary](). It is recommended to [use your Rollbar instance within this error handler](./src/app//next_error_handler/error.tsx) to report errors to Rollbar.

### Using the Rollbar ErrorBoundary

The `<ErrorBoundary>` component provided by this library [may still be used](./src/app/rollbar_error_boundary/page.tsx) if you would prefer that over the built in Next.js error handler

#### Special note on Error boundaries

The ErrorBoundary class is not perfect at catching and stopping the propagation of all errors. Be aware, that if you turn on `captureUncaught` or `captureUnhandledRejections` in your Rollbar config you may receive doubled occurrences.
4 changes: 4 additions & 0 deletions examples/nextjs-approuter/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};

export default nextConfig;
Loading
Loading