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

feat: deprecate ClientOnly and replace with clientOnly (same as solid-start) #74

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions examples/basic/pages/index/+Page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { ClientOnly } from "vike-solid/ClientOnly";
import { clientOnly } from "vike-solid/ClientOnly";

const ClientOnlyCounter = clientOnly(() => import("./Counter"));
const ClientOnlyCounterSlow = clientOnly(async () => {
// Wasting time to show the fallback
await new Promise(resolve => setTimeout(resolve, 2000));

return import("./Counter");
});

export default function Page() {
return (
Expand All @@ -8,22 +16,9 @@ export default function Page() {
<ul>
<li>Rendered to HTML.</li>

<ClientOnly load={() => import("./Counter")} fallback={<li>Waiting for client-side only component to load (quick)</li>}>
{(Counter) => <li>
Interactive 1. <Counter />
</li>}
</ClientOnly>

<ClientOnly load={async () => {
// Wasting time to show the fallback
await new Promise(resolve => setTimeout(resolve, 2000));
<li><ClientOnlyCounter fallback={<>Waiting for client-side only component to load (quick)</>} /></li>

return import("./Counter");
}} fallback={<li>Waiting for client-side only component to load (slow)</li>}>
{(Counter) => <li>
Interactive 2. <Counter />
</li>}
</ClientOnly>
<li><ClientOnlyCounterSlow fallback={<>Waiting for client-side only component to load (slow)</>} /></li>
</ul>
</>
);
Expand Down
2 changes: 1 addition & 1 deletion vike-solid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ SolidJS integration for Vike, see [vike.dev/solid](https://vike.dev/solid).

## Scaffold new app

Use [Bati](https://batijs.github.io/) to scaffold a Vike app using `vike-solid`.
Use [Bati](https://batijs.dev/) to scaffold a Vike app using `vike-solid`.

## Add to existing app

Expand Down
51 changes: 48 additions & 3 deletions vike-solid/components/ClientOnly.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import type { Component, JSX } from "solid-js";
import { createEffect, createSignal, lazy, Suspense } from "solid-js";
import { Dynamic } from "solid-js/web";
import {
Component,
ComponentProps,
createEffect,
createMemo,
createSignal,
JSX,
lazy,
onMount,
sharedConfig,
splitProps,
Suspense,
untrack
} from "solid-js";
import { Dynamic, isServer } from "solid-js/web";

function ClientOnlyError() {
return <p>Error loading component.</p>
}

/**
* @deprecated Replaced by {@link clientOnly}
*/
export function ClientOnly<T>(props: {
load: () => Promise<{ default: Component<T> } | Component<T>>
children: (Component: Component<T>) => JSX.Element
Expand Down Expand Up @@ -33,3 +48,33 @@ export function ClientOnly<T>(props: {

return <Suspense fallback={props.fallback}><Dynamic component={getComponent()} /></Suspense>;
}


// Copied from https://github.com/solidjs/solid-start/blob/2d75d5fedfd11f739b03ca34decf23865868ac09/packages/start/src/shared/clientOnly.tsx#L7
/**
* Same as `clientOnly` from solid-start
* @see {@link https://docs.solidjs.com/solid-start/reference/client/client-only}
*/
export function clientOnly<T extends Component<any>>(
fn: () => Promise<{
default: T;
}>
) {
if (isServer) return (props: ComponentProps<T> & { fallback?: JSX.Element }) => props.fallback;

const [comp, setComp] = createSignal<T>();
fn().then(m => setComp(() => m.default));
return (props: ComponentProps<T>) => {
let Comp: T | undefined;
let m: boolean;
const [, rest] = splitProps(props, ["fallback"]);
if ((Comp = comp()) && !sharedConfig.context) return Comp(rest);
const [mounted, setMounted] = createSignal(!sharedConfig.context);
onMount(() => setMounted(true));
return createMemo(
() => (
(Comp = comp()), (m = mounted()), untrack(() => (Comp && m ? Comp(rest) : props.fallback))
)
);
};
}
Loading