React Router 會捕捉您路由模組中的錯誤,並將它們傳送到錯誤邊界,以防止發生錯誤時出現空白頁面。但是,ErrorBoundary 不足以記錄和報告錯誤。若要存取這些捕捉到的錯誤,請使用伺服器進入模組的 handleError 匯出。
如果您在應用程式目錄中沒有看到 entry.server.tsx
,則您正在使用預設進入點。使用此 CLI 命令顯示它
react-router reveal
每當 React Router 在伺服器上捕捉到應用程式中的錯誤時,就會呼叫此函式。
import { type HandleErrorFunction } from "react-router";
export const handleError: HandleErrorFunction = (
error,
{ request }
) => {
// React Router may abort some interrupted requests, don't log those
if (!request.signal.aborted) {
myReportError(error);
// make sure to still log the error so you can see it
console.error(error);
}
};