fix: turbopack RSC detection (#6405)

This commit is contained in:
Alessio Gravili
2024-05-17 11:40:10 -04:00
committed by GitHub
parent 276213193b
commit 4dedd6e267

View File

@@ -2,6 +2,22 @@ import type React from 'react'
import { isPlainObject } from './isPlainObject.js'
/*
For reference: console.log output of [ClientComponent, RSC] array, tested in Turbo and Webpack (14.3.0-canary.37)
Both component functions async:
Turbo: [ [Function (anonymous)], [AsyncFunction: ExampleServer] ]
Webpack: [ {}, [AsyncFunction: ExampleServer] ]
Both component functions non-async:
Turbo: [ [Function (anonymous)], [Function: ExampleServer] ]
Webpack: [ {}, [Function: ExampleServer] ]
*/
export function isReactServerComponentOrFunction<T extends any>(
component: React.ComponentType | any,
): component is T {
@@ -13,14 +29,19 @@ export function isReactServerComponentOrFunction<T extends any>(
const isFunctionalComponent =
typeof component === 'function' && (!component.prototype || !component.prototype.render)
return isClassComponent || isFunctionalComponent
// Anonymous functions are Client Components in Turbopack. RSCs should have a name
const isAnonymousFunction = component.name === ''
return (isClassComponent || isFunctionalComponent) && !isAnonymousFunction
}
export function isReactClientComponent<T extends any>(
component: React.ComponentType | any,
): component is T {
// Do this to test for client components (`use client` directive) bc they import as empty objects
return typeof component === 'object' && !isPlainObject(component)
const isClientComponentWebpack = typeof component === 'object' && !isPlainObject(component) // In Webpack, client components are {}
const isClientComponentTurbo = typeof component === 'function' && component.name === '' // Anonymous functions are Client Components in Turbopack
return isClientComponentWebpack || isClientComponentTurbo
}
export function isReactComponentOrFunction<T extends any>(