Skip to content

Commit

Permalink
Merge 9d92908 into 220a289
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Mar 25, 2023
2 parents 220a289 + 9d92908 commit 6f203fc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
26 changes: 25 additions & 1 deletion packages/solid/src/render/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
devComponent,
$PROXY,
$DEVCOMP,
EffectFunction
EffectFunction,
onMount
} from "../reactive/signal.js";
import { sharedConfig, nextHydrateContext, setHydrateContext } from "./hydration.js";
import type { JSX } from "../jsx.js";
Expand Down Expand Up @@ -354,3 +355,26 @@ export function createUniqueId(): string {
const ctx = sharedConfig.context;
return ctx ? `${ctx.id}${ctx.count++}` : `cl-${counter++}`;
}

export function clientOnly<T extends Component<any>>(
fn: () => Promise<{ default: T }>
): T {
const Lazy = lazy(fn);
return ((props: any) => {
if (sharedConfig.context) {
const [flag, setFlag] = createSignal(false);

onMount(() => {
setFlag(true);
});

return createMemo(() => {
if (flag()) {
return createComponent(Lazy, props);
}
return undefined;
});
}
return createComponent(Lazy, props);
}) as unknown as T;
}
23 changes: 22 additions & 1 deletion packages/solid/src/render/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
Accessor,
Setter,
onCleanup,
MemoOptions
MemoOptions,
onMount
} from "../reactive/signal.js";
import { mapArray, indexArray } from "../reactive/array.js";
import { sharedConfig } from "./hydration.js";
Expand Down Expand Up @@ -258,3 +259,23 @@ export function ErrorBoundary(props: {
"_SOLID_DEV_" ? { name: "value" } : undefined
) as Accessor<JSX.Element>;
}

export function ClientOnly(props: {
children?: JSX.Element,
}): JSX.Element {
if (sharedConfig.context) {
const [flag, setFlag] = createSignal(false);

onMount(() => {
setFlag(true);
});

return createMemo(() => {
if (flag()) {
return createMemo(() => props.children);
}
return undefined;
});
}
return createMemo(() => props.children);
}
10 changes: 10 additions & 0 deletions packages/solid/src/server/rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ export function ErrorBoundary(props: {
return { t: `<!e${id}>${resolveSSRNode(res)}<!/e${id}>` };
}

export function ClientOnly(props: { children?: JSX.Element }): JSX.Element {
return undefined;
}

// Suspense Context
export interface Resource<T> {
(): T | undefined;
Expand Down Expand Up @@ -606,3 +610,9 @@ export function Suspense(props: { fallback?: string; children: string }) {
ctx.writeResource(id, "$$f");
return props.fallback;
}

export function clientOnly<T extends Component<any>>(
fn: () => Promise<{ default: T }>
): T {
return (() => undefined) as unknown as T;
}

0 comments on commit 6f203fc

Please sign in to comment.