Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ClientOnly #1592

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
Copy link

@emdede emdede Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a case where you'd want to use a self-closing <ClientOnly />?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe 🤷 🤣

}): JSX.Element {
if (sharedConfig.context) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iirc this is the way to check if the app is currently hydrating. This check is needed so that UI correction can be applied, otherwise we can just render the children directly (since the app is just in CSR)

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 @@ -280,6 +280,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 @@ -609,3 +613,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;
}