Skip to content

Commit 30bc5a0

Browse files
committed
Don't trace hooks during server-side rendering
1 parent 5160dac commit 30bc5a0

File tree

10 files changed

+41
-9
lines changed

10 files changed

+41
-9
lines changed

packages/react-hook-tracer/src/hooks/useCallback.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ export function useCallback<F extends Function>(
1515
deps: React.DependencyList,
1616
traceOptions?: UseCallbackTraceOptions,
1717
): F {
18-
if (!util.isProductionBuild && componentRegistry.isCurrentComponentTraced()) {
18+
if (
19+
!util.isProductionBuild &&
20+
!util.isServerRendered &&
21+
componentRegistry.isCurrentComponentTraced()
22+
) {
1923
// eslint-disable-next-line react-hooks/rules-of-hooks
2024
return useCallbackTraced(
2125
callback as unknown as (...args: never[]) => unknown, // Convert untyped Function to explicit function type.

packages/react-hook-tracer/src/hooks/useContext.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ export interface UseContextTraceOptions<T> {
1212
}
1313

1414
export function useContext<T>(context: Context<T>, traceOptions?: UseContextTraceOptions<T>): T {
15-
if (!util.isProductionBuild && componentRegistry.isCurrentComponentTraced()) {
15+
if (
16+
!util.isProductionBuild &&
17+
!util.isServerRendered &&
18+
componentRegistry.isCurrentComponentTraced()
19+
) {
1620
// eslint-disable-next-line react-hooks/rules-of-hooks
1721
return useContextTraced(context, traceOptions)
1822
} else {

packages/react-hook-tracer/src/hooks/useEffect.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export function useEffect(
1515
traceOptions?: UseEffectTraceOptions,
1616
): void {
1717
const hook =
18-
!util.isProductionBuild && componentRegistry.isCurrentComponentTraced()
18+
!util.isProductionBuild &&
19+
!util.isServerRendered &&
20+
componentRegistry.isCurrentComponentTraced()
1921
? useEffectTraced
2022
: React.useEffect
2123
return hook(effect, deps, traceOptions)

packages/react-hook-tracer/src/hooks/useInsertionEffect.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export function useInsertionEffect(
1515
traceOptions?: UseInsertionEffectTraceOptions,
1616
): void {
1717
const hook =
18-
!util.isProductionBuild && componentRegistry.isCurrentComponentTraced()
18+
!util.isProductionBuild &&
19+
!util.isServerRendered &&
20+
componentRegistry.isCurrentComponentTraced()
1921
? useInsertionEffectTraced
2022
: React.useEffect
2123
return hook(effect, deps, traceOptions)

packages/react-hook-tracer/src/hooks/useLayoutEffect.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ export function useLayoutEffect(
1616
traceOptions?: UseLayoutEffectTraceOptions,
1717
): void {
1818
const hook =
19-
!util.isProductionBuild && componentRegistry.isCurrentComponentTraced()
19+
!util.isProductionBuild &&
20+
!util.isServerRendered &&
21+
componentRegistry.isCurrentComponentTraced()
2022
? useLayoutEffectTraced
2123
: React.useEffect
2224
return hook(effect, deps, traceOptions)

packages/react-hook-tracer/src/hooks/useMemo.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ export function useMemo<T>(
1414
deps: React.DependencyList | undefined,
1515
traceOptions?: UseMemoTraceOptions<T>,
1616
): T {
17-
if (!util.isProductionBuild && componentRegistry.isCurrentComponentTraced()) {
17+
if (
18+
!util.isProductionBuild &&
19+
!util.isServerRendered &&
20+
componentRegistry.isCurrentComponentTraced()
21+
) {
1822
// eslint-disable-next-line react-hooks/rules-of-hooks
1923
return useMemoTraced(factory, deps, traceOptions)
2024
} else {

packages/react-hook-tracer/src/hooks/useReducer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ export function useReducer<I, S, A>(
7676
initializer?: (arg: I) => S,
7777
traceOptions?: UseReducerTraceOptions<S, A>,
7878
): [S, Dispatch<A>] {
79-
if (!util.isProductionBuild && componentRegistry.isCurrentComponentTraced()) {
79+
if (
80+
!util.isProductionBuild &&
81+
!util.isServerRendered &&
82+
componentRegistry.isCurrentComponentTraced()
83+
) {
8084
// eslint-disable-next-line react-hooks/rules-of-hooks
8185
return useReducerTraced(reducer, initialArg, initializer, traceOptions)
8286
} else {

packages/react-hook-tracer/src/hooks/useRef.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ export function useRef<T>(
2929
initialValue?: T,
3030
traceOptions?: UseRefTraceOptions<T>,
3131
): MutableRefObject<T | undefined> | RefObject<T | undefined> {
32-
if (!util.isProductionBuild && componentRegistry.isCurrentComponentTraced()) {
32+
if (
33+
!util.isProductionBuild &&
34+
!util.isServerRendered &&
35+
componentRegistry.isCurrentComponentTraced()
36+
) {
3337
// eslint-disable-next-line react-hooks/rules-of-hooks
3438
return useRefTraced(initialValue, traceOptions)
3539
} else {

packages/react-hook-tracer/src/hooks/useState.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ export function useState<S = undefined>(
2424
initialState?: S | (() => S),
2525
traceOptions?: UseStateTraceOptions<S>,
2626
): [S | undefined, Dispatch<SetStateAction<S | undefined>>] {
27-
if (!util.isProductionBuild && componentRegistry.isCurrentComponentTraced()) {
27+
if (
28+
!util.isProductionBuild &&
29+
!util.isServerRendered &&
30+
componentRegistry.isCurrentComponentTraced()
31+
) {
2832
// eslint-disable-next-line react-hooks/rules-of-hooks
2933
return useStateTraced(initialState, traceOptions)
3034
} else {

packages/react-hook-tracer/src/util.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as types from './types'
22

33
export const isProductionBuild = process.env.NODE_ENV === 'production'
44

5+
export const isServerRendered = typeof window === 'undefined'
6+
57
export const includes = <T>(xs: readonly T[], x: T): boolean => xs.indexOf(x) >= 0
68

79
export const flatMap = <T, S>(xs: readonly T[], f: (x: T) => readonly S[]): S[] =>

0 commit comments

Comments
 (0)