Skip to content

Commit 4f19bcd

Browse files
authored
Enhance clientOnly documentation
1 parent 237da62 commit 4f19bcd

1 file changed

Lines changed: 45 additions & 19 deletions

File tree

src/routes/solid-start/reference/client/client-only.mdx

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,62 @@
22
title: clientOnly
33
---
44

5-
Wrapping components in `clientOnly` will cause them render _only_ in the client.
6-
This can useful for components that interact directly with the DOM, such as jQuery, since they can not render on the server.
7-
It works similar to [`lazy`](/reference/component-apis/lazy) but will only render _after hydration_ and will never load on the server.
5+
The `clientOnly` function allows components or pages to render exclusively on the client side, bypassing server-side rendering (_SSR_).
6+
This is useful for code that relies on the browser, such as Web3 interactions (like `window.ethereum` for Ethereum-based dApps) or IndexedDB for client-side storage.
87

9-
To use `clientOnly`, isolate the desired component with DOM interactions in a file:
8+
## How to Use `clientOnly`
109

11-
```tsx
12-
const location = window.document.location;
10+
Follow these steps to implement `clientOnly` in your Solid Start project:
1311

14-
export default function ClientOnlyComponent() {
15-
return <div>{location.href}</div>;
16-
}
17-
```
12+
1. **Isolate Client-Only Logic**: Create a separate file for the component that depends on browser-specific features, such as DOM or browser APIs.
13+
14+
```tsx
15+
// ClientOnlyComponent.tsx
16+
export default function ClientOnlyComponent() {
17+
const location = document.location.href;
18+
return <div>Current URL: {location}</div>;
19+
}
20+
```
21+
22+
2. **Import with `clientOnly`**: Use `clientOnly` to dynamically import the isolated component in your parent component or page.
23+
24+
```tsx
25+
// IsomorphicComponent.tsx
26+
import { clientOnly } from "@solidjs/start";
1827

19-
Once isolated, it can then be imported dynamically using `clientOnly`:
28+
const ClientOnlyComp = clientOnly(() => import("./ClientOnlyComponent"));
29+
30+
export default function IsomorphicComponent() {
31+
return <ClientOnlyComp />;
32+
}
33+
```
34+
35+
3. **Add a Fallback (Optional)**: Provide a `fallback` prop to display content while the client-only component is loading.
36+
37+
```tsx
38+
<ClientOnlyComp fallback={<div>Loading...</div>} />
39+
```
40+
41+
## Disabling SSR for Entire Pages
42+
43+
To disable SSR for an entire page, apply `clientOnly` at the page level. This ensures the page renders only on the client.
2044

2145
```tsx
46+
// for example routes/page.tsx
2247
import { clientOnly } from "@solidjs/start";
2348

24-
const ClientOnlyComp = clientOnly(() => import("../ClientOnlyComp"));
49+
export default clientOnly(async () => ({ default: Page }), { lazy: true });
2550

26-
function IsomorphicComp() {
27-
return <ClientOnlyComp />;
51+
function Page() {
52+
// This code runs only on the client
53+
return <div>Client-only page content</div>;
2854
}
2955
```
3056

31-
**Note:** The `<ClientOnlyComp />` can take a fallback prop for when it is loading.
32-
3357
## Parameters
3458

35-
| Argument | Type | Description |
36-
| -------- | --------------- | ------------------------------------ |
37-
| fn | `() => Promise` | Function to be run client-side only. |
59+
| Argument | Type | Description |
60+
| --------- | -------------------------------------------------- | -------------------------------------------------------------------------------------------------------- |
61+
| `fn` | `() => Promise<{ default: () => JSX.Element }>` | A function that dynamically imports a component to be rendered only on the client side. |
62+
| `options` | `{ lazy?: boolean }` | An optional object to configure loading behavior. Set `lazy: false` for eager loading |
63+
| `props` | `Record<string, any> & { fallback?: JSX.Element }` | Props passed to the component, including an optional `fallback` for rendering while the component loads. |

0 commit comments

Comments
 (0)