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, such as Web3 interactions (like window.ethereum
for Ethereum-based dApps) or IndexedDB for client-side storage.
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.
// ClientOnlyComponent.tsxexport default function ClientOnlyComponent() {const location = document.location.href;return <div>Current URL: {location}</div>;} -
Import with
clientOnly
: UseclientOnly
to dynamically import the isolated component in your parent component or page.// IsomorphicComponent.tsximport { clientOnly } from "@solidjs/start";const ClientOnlyComp = clientOnly(() => import("./ClientOnlyComponent"));export default function IsomorphicComponent() {return <ClientOnlyComp />;} -
Add a Fallback (Optional): Provide a
fallback
prop 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.
// for example routes/page.tsximport { 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: false for eager loading |
props | Record<string, any> & { fallback?: JSX.Element } | Props passed to the component, including an optional fallback for rendering while the component loads. |