Skip to content

Commit 7c26fa2

Browse files
Closes #1288 (#1289)
Co-authored-by: Atila Fassina <[email protected]>
1 parent 6e421af commit 7c26fa2

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

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

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,57 @@
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-specific APIs, such as `window` or `document`.
87

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

11-
```tsx
12-
const location = window.document.location;
10+
1. **Isolate Client-Only Logic**: Create a separate file for the component that depends on browser-specific features, such as DOM or browser APIs.
1311

14-
export default function ClientOnlyComponent() {
15-
return <div>{location.href}</div>;
16-
}
17-
```
12+
```tsx title="ClientOnlyComponent"
13+
export default function ClientOnlyComponent() {
14+
const location = document.location.href;
15+
return <div>Current URL: {location}</div>;
16+
}
17+
```
18+
19+
2. **Import with `clientOnly`**: Use `clientOnly` to dynamically import the isolated component in your parent component or page.
20+
21+
```tsx title="IsomorphicComponent.tsx"
22+
import { clientOnly } from "@solidjs/start";
23+
24+
const ClientOnlyComp = clientOnly(() => import("./ClientOnlyComponent"));
1825

19-
Once isolated, it can then be imported dynamically using `clientOnly`:
26+
export default function IsomorphicComponent() {
27+
return <ClientOnlyComp />;
28+
}
29+
```
2030

21-
```tsx
31+
3. **Add a Fallback (Optional)**: Provide a `fallback` prop to display content while the client-only component is loading.
32+
33+
```tsx
34+
<ClientOnlyComp fallback={<div>Loading...</div>} />
35+
```
36+
37+
## Disabling SSR for Entire Pages
38+
39+
To disable SSR for an entire page, apply `clientOnly` at the page level. This ensures the page renders only on the client.
40+
41+
```tsx title="routes/page.tsx"
2242
import { clientOnly } from "@solidjs/start";
2343

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

26-
function IsomorphicComp() {
27-
return <ClientOnlyComp />;
46+
function Page() {
47+
// This code runs only on the client
48+
return <div>Client-only page content</div>;
2849
}
2950
```
3051

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

35-
| Argument | Type | Description |
36-
| -------- | --------------- | ------------------------------------ |
37-
| fn | `() => Promise` | Function to be run client-side only. |
54+
| Argument | Type | Description |
55+
| --------- | -------------------------------------------------- | -------------------------------------------------------------------------------------------------------- |
56+
| `fn` | `() => Promise<{ default: () => JSX.Element }>` | A function that dynamically imports a component to be rendered only on the client side. |
57+
| `options` | `{ lazy?: boolean }` | An optional object to configure loading behavior. Set `lazy: false` for eager loading |
58+
| `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)