Skip to content

Commit d93a3d1

Browse files
committed
refactor: improve JSX delegate creation and component ID resolution
1 parent 52aa1fe commit d93a3d1

2 files changed

Lines changed: 21 additions & 22 deletions

File tree

crates/rari/src/rsc/rendering/core/js/component_eval_setup.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ if (!globalThis.React || typeof globalThis.React.createElement !== 'function') {
2323
}
2424

2525
function createJsxDelegate(runtimeJsx, globalJsx) {
26-
return runtimeJsx || globalJsx || ((type, props, key) => {
27-
if (!globalThis.React || typeof globalThis.React.createElement !== 'function')
28-
return null
26+
return (type, props, key) => {
27+
if (globalThis.React && typeof globalThis.React.createElement === 'function')
28+
return globalThis.React.createElement(type, key !== undefined ? { ...props, key } : props)
29+
if (typeof runtimeJsx === 'function')
30+
return runtimeJsx(type, props, key)
31+
if (typeof globalJsx === 'function')
32+
return globalJsx(type, props, key)
2933

30-
return globalThis.React.createElement(type, key !== undefined ? { ...props, key } : props)
31-
})
34+
return null
35+
}
3236
}
3337

3438
if (typeof _jsx === 'undefined')

packages/rari/src/runtime/shared/get-client-component.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -223,26 +223,21 @@ export async function getClientComponentAsync(id: string): Promise<any> {
223223
const clientComponentPaths = (globalThis as unknown as GlobalWithRari)['~clientComponentPaths'] || {}
224224
const clientComponentNames = (globalThis as unknown as GlobalWithRari)['~clientComponentNames'] || {}
225225

226-
let componentInfo = clientComponents[id] as LazyComponentInfo
226+
const hashIndex = id.indexOf('#')
227+
const baseId = hashIndex === -1 ? id : id.slice(0, hashIndex)
228+
const exportName = hashIndex === -1 ? undefined : id.slice(hashIndex + 1)
227229

228-
if (componentInfo) {
229-
const exportName = id.includes('#') ? id.slice(id.indexOf('#') + 1) : undefined
230+
let componentInfo = clientComponents[baseId] as LazyComponentInfo
231+
if (componentInfo)
230232
return await ensureComponentLoaded(componentInfo, exportName)
231-
}
232233

233-
if (id.includes('#')) {
234-
const hashIndex = id.indexOf('#')
235-
const path = id.slice(0, hashIndex)
236-
const exportName = id.slice(hashIndex + 1)
237-
const variants = getPathVariants(path)
238-
239-
for (const variant of variants) {
240-
const componentId = clientComponentPaths[variant]
241-
if (componentId) {
242-
componentInfo = clientComponents[componentId] as LazyComponentInfo
243-
if (componentInfo)
244-
return await ensureComponentLoaded(componentInfo, exportName)
245-
}
234+
const variants = getPathVariants(baseId)
235+
for (const variant of variants) {
236+
const componentId = clientComponentPaths[variant]
237+
if (componentId) {
238+
componentInfo = clientComponents[componentId] as LazyComponentInfo
239+
if (componentInfo)
240+
return await ensureComponentLoaded(componentInfo, exportName)
246241
}
247242
}
248243

0 commit comments

Comments
 (0)