Skip to content

Commit 65c2aee

Browse files
committed
Return dummy trace & TracePanel during SSR
1 parent ddfc59d commit 65c2aee

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

packages/react-hook-tracer/src/components/TracePanel.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MutableRefObject, useCallback, useEffect, useState } from 'react'
1+
import { MutableRefObject, ReactNode, useCallback, useEffect, useState } from 'react'
22

33
import { tracer } from '../Tracer'
44
import { LogEntry, TraceOrigin, TraceOrigins } from '../types'
@@ -14,6 +14,13 @@ interface TracePanelProps {
1414
traceOrigins: TraceOrigins
1515
refreshTracePanelRef: MutableRefObject<(() => void) | null>
1616
}
17+
18+
const TracePanelWrapper = ({ children }: { children?: ReactNode | undefined }) => (
19+
<div className="trace-panel" data-testid="trace-panel">
20+
{children}
21+
</div>
22+
)
23+
1724
export const TracePanel = ({
1825
componentLabel,
1926
props,
@@ -52,7 +59,7 @@ export const TracePanel = ({
5259
const traceOriginList: TraceOrigin[] = [mount, render, ...hooks, trace]
5360

5461
return (
55-
<div className="trace-panel" data-testid="trace-panel">
62+
<TracePanelWrapper>
5663
<div className="component-label">
5764
<div>{componentLabel}</div>
5865
</div>
@@ -77,10 +84,12 @@ export const TracePanel = ({
7784
/>
7885
))}
7986
</div>
80-
</div>
87+
</TracePanelWrapper>
8188
)
8289
}
8390

91+
export const TracePanelServerSide = () => <TracePanelWrapper />
92+
8493
interface PropProps {
8594
propKey: string
8695
propValue: unknown

packages/react-hook-tracer/src/hooks/useTracer.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useCallback, useRef } from 'react'
22

33
import { isTraceLogRegistered, logPendingToConsole, setTracerConfig, tracer } from '../Tracer'
44
import * as componentRegistry from '../componentRegistry'
5-
import { TracePanel } from '../components/TracePanel'
5+
import { TracePanel, TracePanelServerSide } from '../components/TracePanel'
66
import * as reactInternals from '../reactInternals'
77
import { ShowProps } from '../types'
88
import * as util from '../util'
@@ -27,6 +27,20 @@ export const useTracer = (
2727
): {
2828
trace: (message: string) => void
2929
TracePanel: () => JSX.Element
30+
} => {
31+
if (util.isServerRendered) {
32+
return { trace: (_message: string) => {}, TracePanel: TracePanelServerSide }
33+
} else {
34+
// eslint-disable-next-line react-hooks/rules-of-hooks
35+
return useTracerClientSide(options)
36+
}
37+
}
38+
39+
const useTracerClientSide = (
40+
options?: UseTracerOptions,
41+
): {
42+
trace: (message: string) => void
43+
TracePanel: () => JSX.Element
3044
} => {
3145
const showPropValue = mkShowPropValue(options?.showProps)
3246
const componentInfo = componentRegistry.registerCurrentComponent()

0 commit comments

Comments
 (0)