Open History to see the last commit
1import type { JSXElement } from '@yuku-toolchain/types'
2β
3/**
4 * Handles <ClientOnly> JSX elements on the server side.
5 *
6 * On the server, the children of <ClientOnly> should be removed since they
7 * are client-only code. Only the fallback prop (if present) will be rendered.
8 *
9 * Transform:
10 * <ClientOnly fallback={<Loading />}>{clientOnlyContent}</ClientOnly>
11 * Into:
12 * <ClientOnly fallback={<Loading />} />
13 *
14 * Or if no fallback:
15 * <ClientOnly>{clientOnlyContent}</ClientOnly>
16 * Into:
17 * <ClientOnly />
18 */
19export function handleClientOnlyJSX(
20 element: JSXElement,
21 _opts: { env: 'server' },
22): void {
23 // Remove all children - they are client-only code
24 element.children = []
25β
26 // Make it a self-closing element since there are no children
27 element.openingElement.selfClosing = true
28 element.closingElement = null
29}