rsc-env is a focused utility package designed to provide a reliable method for discriminating between React Server Component (RSC) and other JavaScript environments (e.g., client components, traditional server-side rendering). It achieves this by leveraging export conditions, a modern module feature that allows bundlers to statically determine the correct environment at build time. This enables aggressive tree-shaking, ensuring that server-only logic is entirely removed from client bundles, thereby optimizing bundle size and improving application performance. The package is currently in its early stages, at version 0.0.2, and its release cadence is expected to align with the evolving React Server Components ecosystem. A key differentiator for rsc-env is its targeted approach to the `react-server` condition, offering a more robust and future-proof solution for conditional code execution in modern React applications, especially those built with frameworks like Next.js App Router, compared to heuristic-based methods (e.g., checking for `useEffect` presence). This explicit approach ensures bundler compatibility and optimal performance.
npm install rsc-envVerified import paths — ran on the pinned version, not inferred.
This code demonstrates how `rsc-env` can be used within a shared utility function and component to conditionally execute or include code based on whether it's running in a React Server Component or a client component environment, leveraging static tree-shaking.
Always consult the latest `rsc-env` documentation and release notes when upgrading to new versions, and be prepared to adapt code.
Ensure your project's bundler (e.g., Webpack 5+, Vite with `@vitejs/plugin-rsc`) is up-to-date and configured to support React Server Components and their associated export conditions.
For general client/server environment checks, continue using standard methods like `typeof window === 'undefined'`. Use `rsc-env` exclusively for conditional logic pertinent to React Server Components.
Verify your bundler (e.g., Vite with `@vitejs/plugin-rsc`, Next.js App Router's built-in bundler) is properly configured for React Server Components. Ensure it supports the `exports` field and the `react-server` condition, and update to the latest compatible bundler version if necessary.
Always use ESM `import { rsc } from 'rsc-env';` syntax. Ensure your project is configured for ESM, especially in a React Server Component context.Review the conditional logic (e.g., `if (rsc) { ... }`) to ensure it's statically analyzable by the bundler. Check bundler configuration for tree-shaking and dead code elimination settings. Ensure the `react-server` condition is only applied to server component entry points.No dependency data recorded yet.