简体   繁体   English

带 SSR 的哨兵

[英]Sentry with SSR

  • We have an application on Reactjs我们有一个关于 Reactjs 的应用程序
  • We have an SSR that builds the app on webpack, renders this application and returns HTML我们有一个 SSR,它在 webpack 上构建应用程序,呈现这个应用程序并返回 HTML
  • Then on the client-side we hydrate the app然后在客户端,我们为应用程序添加水合物
  • So the app is run on both server and client sides.因此,该应用程序在服务器端和客户端都运行。

Problem:问题:

We'd like to use sentry in react, so that it can be used on both client and server sides, because an error may occur in either place我们想在 react 中使用 sentry,这样它就可以在客户端和服务器端使用,因为任何地方都可能出现错误

What we've tried:我们尝试过的:

There are 2 modules @sentry/browser and @sentry/node.有 2 个模块 @sentry/browser 和 @sentry/node。 We tried to make a module that would do:我们尝试制作一个可以执行以下操作的模块:

export default isClientSide ? SentryBrowser : SentryNode

So if it's client, then use SentryBrowser.因此,如果它是客户端,则使用 SentryBrowser。 If server - SentryNode如果服务器 - SentryNode

But @sentry/node can't be run on webpack since it can't resolve node dependencies like fs , path etc.但是 @sentry/node 不能在 webpack 上运行,因为它不能解析节点依赖,比如fspath等。

Question: How can we use a single Sentry interface on both client and server sides for React like @sentry/nextjs works on both sides?问题:我们如何在客户端和服务器端都使用单个 Sentry 接口来让 React 像 @sentry/nextjs 一样在双方都工作?

Short answer - you can't.简短的回答 - 你不能。

Client-side is wrapped in special HOC, called in most cases Error Boundary<\/a> .客户端被包裹在特殊的 HOC 中,在大多数情况下称为Error Boundary<\/a> 。 React passes up an error to the root component of the project and catches it and reports it to the URL. React 将错误传递给项目的根组件并捕获它并将其报告给 URL。 This is a browser, which is client's "server", can do requests on ethernet and so on.这是一个浏览器,它是客户端的“服务器”,可以在以太网上进行请求等等。

If you run NextJS server, not html export<\/code> (extremely important condition!), under the hood it is uses some NodeJS libraries ( Express<\/a> , for example).如果您运行 NextJS 服务器,而不是html export<\/code> (极其重要的条件!),那么它在后台使用了一些 NodeJS 库(例如Express<\/a> )。 This server has nothing in common with Webpack or JS compilation and is used as middleware.该服务器与 Webpack 或 JS 编译没有任何共同之处,用作中间件。 As you can see<\/a> , it is just a wrapper upon some route or functionality. 如您所见<\/a>,它只是某些路由或功能的包装器。 If you got an error on server, it is reported by NodeJS, not Webpack.如果你在服务器上遇到错误,它是由 NodeJS 报告的,而不是 Webpack。 So the flag somewhere in React or Webpack plugins will not help you.所以 React 或 Webpack 插件中某处的标志对你没有帮助。

One more time - client and server are different environments, browser and NodeJS server.再来一次 - 客户端和服务器是不同的环境,浏览器和 NodeJS 服务器。 The errors are reported by servers, not bundlers.这些错误是由服务器报告的,而不是捆绑程序。 Webpack will not help you here. Webpack 在这里帮不了你。

"

Our solution was to pass in an errorHandler prop to the app.我们的解决方案是将 errorHandler 属性传递给应用程序。 When rendering on the server we'd pass in @sentry/node's captureException, and on the client we'd pass in the @sentry/browser's one.在服务器上渲染时,我们会传入@sentry/node 的 captureException,而在客户端上,我们会传入@sentry/browser 的。 Seemed to work just fine.似乎工作得很好。

const App = ({errorHandler}) => {
  ..
  return (<ErrorBoundary errorHandler={errorHandler}>
    ..
  </ErrorBoundary>);

Then on server:然后在服务器上:

import { captureException as serverCaptureEx } from '@sentry/node';

hydrate(..., <App errorHandler={serverCaptureEx} />)

and on client:在客户端:

import { captureException as clientCaptureEx } from '@sentry/browser';

render(..., <App errorHandler={clientCaptureEx} />)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM