Skip to content

Commit 9d92908

Browse files
committed
Add clientOnly HOC
1 parent 2a7a6c7 commit 9d92908

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

packages/solid/src/render/component.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
devComponent,
77
$PROXY,
88
$DEVCOMP,
9-
EffectFunction
9+
EffectFunction,
10+
onMount
1011
} from "../reactive/signal.js";
1112
import { sharedConfig, nextHydrateContext, setHydrateContext } from "./hydration.js";
1213
import type { JSX } from "../jsx.js";
@@ -354,3 +355,26 @@ export function createUniqueId(): string {
354355
const ctx = sharedConfig.context;
355356
return ctx ? `${ctx.id}${ctx.count++}` : `cl-${counter++}`;
356357
}
358+
359+
export function clientOnly<T extends Component<any>>(
360+
fn: () => Promise<{ default: T }>
361+
): T {
362+
const Lazy = lazy(fn);
363+
return ((props: any) => {
364+
if (sharedConfig.context) {
365+
const [flag, setFlag] = createSignal(false);
366+
367+
onMount(() => {
368+
setFlag(true);
369+
});
370+
371+
return createMemo(() => {
372+
if (flag()) {
373+
return createComponent(Lazy, props);
374+
}
375+
return undefined;
376+
});
377+
}
378+
return createComponent(Lazy, props);
379+
}) as unknown as T;
380+
}

packages/solid/src/server/rendering.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,3 +613,9 @@ export function Suspense(props: { fallback?: string; children: string }) {
613613
ctx.writeResource(id, "$$f");
614614
return props.fallback;
615615
}
616+
617+
export function clientOnly<T extends Component<any>>(
618+
fn: () => Promise<{ default: T }>
619+
): T {
620+
return (() => undefined) as unknown as T;
621+
}

0 commit comments

Comments
 (0)