@baselime/react-rum

中文文档
0.3.1发布于 2024/09/09MITTypeScriptES Module近 30 天下载 2.8万

Optimise the real world performance of your react applications

原始使用文档
文档目录10 节
  1. Usage
  2. Web Vitals
  3. Capture Errors
  4. Error Boundaries
  5. Capture Exceptions
  6. Custom Events
  7. Setting the active user
  8. Using your data
  9. Props
  10. License

Baselime React Real User Monitoring

Documentation Latest Release License

Baselime enables observability for the next generation of cloud applications.

This library enables you to monitor real-user behaviour of your React and Next.js applications.

Usage

npm i --save @baselime/react-rum

Add the BaselimeRum Component at the root of your React application application.

Get the publicApiKey from the Baselime console. Make sure to use a public API key.


function Page({ child }) {

return (
    <BaselimeRum apiKey={publicApiKey}>
        {child}
    </BaselimeRum>)
}

The following data is automatically captured for every page view of your application:

  • timezone
  • language
  • os
  • userAgent
  • url
  • device
  • country
  • city

Web Vitals

Additionally, you can enable capturing web vitals from your React applications. Use the enableWebVitals prop.

Load this at the top of your application to avoid resending the web vital data.

import { BaselimeRum } from '@baselime/react-rum';

function Page({ child }) {

return (
    <BaselimeRum apiKey={publicApiKey} enableWebVitals>
        {child}
    </BaselimeRum>)
}

Capture Errors

BaselimeRum automatically captures and sends any Unhandled Errors in your application to Baselime.

import { BaselimeRum } from '@baselime/react-rum';

function Page({ child }) {

return (
    <BaselimeRum apiKey={publicApiKey} enableWebVitals fallback={<div>Something went wrong</div>}>
        {child}
    </BaselimeRum>)
}

Error Boundaries

To provide a better UX for end users, use React Error Boundaries.

The BaselimeErrorBoundary catches errors in any of its child components, reports the error to Baselime. It works in conjunction with the <BaselimeRum /> Component so that all errors are correlated by Page Load, and User Session.

import { BaselimeErrorBoundary } from '@baselime/react-rum';

function UserProfile({ child }) {

return (<BaselimeErrorBoundary fallback={<div>Could not display your user profile</div>}>
            <UserProfileImage />
            <UserName />
            <UserBiography />
        </BaselimeErrorBoundary>
    )
}

This is based on the excellent react-error-boundary project.

Capture Exceptions

Error Boundaries do not catch errors inside event handlers. To catch Exceptions

import { useBaselimeRum } from '@baselime/react-rum';

function MyButtonComponent() {
    const { captureException } = useBaselimeRum();

    function handleClick(e) {
        try { 
                 // Do something that could throw  
        } catch (error) {
            // sends errors to Baselime so they can be fixed   
            captureException(error)
       }
    }

    return <button onClick={handleClick}>Click Me</button>
}

Custom Events

Capture custom events for analytics and monitoring. Like logs but with all the power of Baselime.

sendEvent(message: string, payload)

import { useBaselimeRum } from '@baselime/react-rum';

function CheckoutComponent() {
    const { sendEvent } = useBaselimeRum();

    function handleClick() {

        const checkoutSession = await createImaginaryCheckoutSession()
        sendEvent("Checkout Started", {
            ...checkoutSession
        })
    }

    return <button onClick={handleClick}>Checkout</button>
}


Setting the active user

To set the User from another component then call

import { useBaselimeRum } from '@baselime/react-rum';

function UserCard({ child }) {
    const { setUser } = useBaselimeRum();

    function login(user) {

        setUser(user.id);
    }
    return (
        <Button onClick={login}>Login</Button>
    }

Using your data

Once the data is captured, you can query, search and analyse your data in the Baselime console. You can create dashboards and alerts based on the Real User Monitoring metrics.

Props

ParameterDescription
apiKeyYour Baselime API key for authentication and authorization.
enableWebVitals(Optional) A boolean flag indicating whether to enable tracking of web vitals.
enableLocal(Optional) A boolean flag indicating whether to enable local tracking.
dataset(Optional) The dataset to store the data to. Defaults to web.
serviceThe name of the application being monitored. Defaults to the hostname.
fallbackA fallback UI component in case the application crashes

License

© Baselime Limited, 2023

Distributed under MIT License (The MIT License).

See LICENSE for more information.

版本与发布记录

@baselime/react-rum 共发布 21 个版本,首次发布于 2023/09/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.3.1latest2024/09/092 年前
0.3.02024/09/092 年前
0.2.92024/04/142 年前
0.2.82024/02/272 年前
0.2.72023/12/222 年前
0.2.62023/12/112 年前

关于 @baselime/react-rum 的常见问题

@baselime/react-rum 是什么?

Optimise the real world performance of your react applications

如何安装 @baselime/react-rum?

在项目目录运行 npm install @baselime/react-rum,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.2.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@baselime/react-rum 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@baselime/react-rum 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@baselime/react-rum 可以商用吗?

@baselime/react-rum 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@baselime/react-rum 的下载量有多少?

根据 npm 官方下载统计,@baselime/react-rum 在 2026/10/08 – 2026/11/06 这 30 天内被下载 28,340 次,日均约 945 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@baselime/react-rum 还在维护吗?

最新版本 0.3.1 发布于 2024/09/09(2 年前);至今共发布 21 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/09/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。