The `client-only` package is a specialized marker module within the React Server Components (RSC) ecosystem, primarily used in frameworks like Next.js App Router. Its sole purpose is to indicate to the build system that a module and all its transitive dependencies are intended exclusively for client-side execution. It doesn't export any functions or values, acting purely as a side-effect import. When a module imports `client-only`, compatible bundlers and frameworks will enforce that this module is never included in a server bundle, providing build-time errors for misuse. This package, currently at version `0.0.1` and infrequently updated due to its static nature, complements `server-only` to clearly define server-client boundaries in a hybrid rendering environment. Its key differentiator is its explicit, declarative way of preventing accidental server-side imports of client-specific code, which helps avoid issues like 'window is not defined' errors during server rendering.
npm install client-onlyVerified import paths — ran on the pinned version, not inferred.
Demonstrates a simple React Client Component that explicitly imports `client-only` to ensure it only runs in client environments and uses browser-specific APIs.
Wrap browser-specific code in `useEffect` hooks or conditional checks (`if (typeof window !== 'undefined')`) within your client components, even if they import `client-only`.
Always add `'use client';` at the very top of your React component files that are intended to be Client Components. Use `import 'client-only';` for utility modules or other non-component files that must strictly only exist in the client bundle.
Ensure that any dynamic or client-specific content within your Client Components is initialized consistently during SSR and client hydration, or is only rendered after the component mounts on the client (e.g., inside `useEffect`).
Consult your framework's documentation (e.g., Next.js, Remix) regarding its recommended approach for marking client-only modules. If the framework provides native equivalents, `client-only` might be redundant or serve as an additional explicit marker.
Ensure the module importing 'client-only' is itself a Client Component (marked with 'use client';) or is only imported by other Client Components. If it's a utility, ensure it's only called from client-side code.
Place all code that interacts with browser-specific APIs (e.g., `window`, `localStorage`) inside a `useEffect` hook, or guard it with `if (typeof window !== 'undefined')` to ensure it only runs in a browser environment.
No dependency data recorded yet.