**BREAKING:** The minimum required Next.js version has been bumped from `15.0.0-rc.0` to `15.0.0-canary.53`. This is because the way client components are represented changed somewhere between those versions, and it is not feasible to support both versions in our RSC detection logic.
36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
import type React from 'react'
|
|
|
|
const clientRefSymbol = Symbol.for('react.client.reference')
|
|
|
|
export function isReactServerComponentOrFunction<T extends any>(
|
|
component: React.ComponentType | any,
|
|
): component is T {
|
|
if (component === null || component === undefined) {
|
|
return false
|
|
}
|
|
const hasClientComponentSymbol = component.$$typeof == clientRefSymbol
|
|
|
|
const isFunctionalComponent = typeof component === 'function'
|
|
// Anonymous functions are Client Components in Turbopack. RSCs should have a name
|
|
const isAnonymousFunction = typeof component === 'function' && component.name === ''
|
|
|
|
const isRSC = isFunctionalComponent && !isAnonymousFunction && !hasClientComponentSymbol
|
|
|
|
return isRSC
|
|
}
|
|
|
|
export function isReactClientComponent<T extends any>(
|
|
component: React.ComponentType | any,
|
|
): component is T {
|
|
if (component === null || component === undefined) {
|
|
return false
|
|
}
|
|
return !isReactServerComponentOrFunction(component) && component.$$typeof == clientRefSymbol
|
|
}
|
|
|
|
export function isReactComponentOrFunction<T extends any>(
|
|
component: React.ComponentType | any,
|
|
): component is T {
|
|
return isReactServerComponentOrFunction(component) || isReactClientComponent(component)
|
|
}
|