|
2 | 2 | title: clientOnly
|
3 | 3 | ---
|
4 | 4 |
|
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`. |
8 | 7 |
|
9 |
| -To use `clientOnly`, isolate the desired component with DOM interactions in a file: |
| 8 | +## How to Use `clientOnly` in Components |
10 | 9 |
|
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. |
13 | 11 |
|
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")); |
18 | 25 |
|
19 |
| -Once isolated, it can then be imported dynamically using `clientOnly`: |
| 26 | + export default function IsomorphicComponent() { |
| 27 | + return <ClientOnlyComp />; |
| 28 | + } |
| 29 | + ``` |
20 | 30 |
|
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" |
22 | 42 | import { clientOnly } from "@solidjs/start";
|
23 | 43 |
|
24 |
| -const ClientOnlyComp = clientOnly(() => import("../ClientOnlyComp")); |
| 44 | +export default clientOnly(async () => ({ default: Page }), { lazy: true }); |
25 | 45 |
|
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>; |
28 | 49 | }
|
29 | 50 | ```
|
30 | 51 |
|
31 |
| -**Note:** The `<ClientOnlyComp />` can take a fallback prop for when it is loading. |
32 |
| - |
33 | 52 | ## Parameters
|
34 | 53 |
|
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