clientOnly
Edit this pageThe clientOnly function allows components or pages to render exclusively on the client side, bypassing server-side rendering (SSR).
This is useful for code that relies on the browser-specific APIs, such as window or document.
How to Use clientOnly in Components
- 
Isolate Client-Only Logic: Create a separate file for the component that depends on browser-specific features, such as DOM or browser APIs. export default function ClientOnlyComponent() {const location = document.location.href;return <div>Current URL: {location}</div>;}
- 
Import with clientOnly: UseclientOnlyto dynamically import the isolated component in your parent component or page.import { clientOnly } from "@solidjs/start";const ClientOnlyComp = clientOnly(() => import("./ClientOnlyComponent"));export default function IsomorphicComponent() {return <ClientOnlyComp />;}
- 
Add a Fallback (Optional): Provide a fallbackprop to display content while the client-only component is loading.<ClientOnlyComp fallback={<div>Loading...</div>} />
Disabling SSR for Entire Pages
To disable SSR for an entire page, apply clientOnly at the page level. This ensures the page renders only on the client.
import { clientOnly } from "@solidjs/start";
export default clientOnly(async () => ({ default: Page }), { lazy: true });
function Page() {  // This code runs only on the client  return <div>Client-only page content</div>;}Parameters
| Argument | Type | Description | 
|---|---|---|
| fn | () => Promise<{ default: () => JSX.Element }> | A function that dynamically imports a component to be rendered only on the client side. | 
| options | { lazy?: boolean } | An optional object to configure loading behavior. Set lazy: falsefor eager loading | 
| props | Record<string, any> & { fallback?: JSX.Element } | Props passed to the component, including an optional fallbackfor rendering while the component loads. |