{"library":"rsc-env","title":"RSC Environment Discriminator","description":"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.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install rsc-env"],"cli":null},"imports":["import { rsc } from 'rsc-env';","import type { rsc } from 'rsc-env';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"// utils/feature-toggle.ts\nimport { rsc } from \"rsc-env\";\n\ninterface FeatureConfig {\n  name: string;\n  enabledInServer: boolean;\n  message: string;\n}\n\nconst getFeatureStatus = (feature: string): FeatureConfig => {\n  if (rsc) {\n    // This block is only included in server component builds\n    console.log(`[RSC] Checking feature '${feature}' status.`);\n    return {\n      name: feature,\n      enabledInServer: true,\n      message: `Feature '${feature}' is active on the server.`\n    };\n  } else {\n    // This block is tree-shaken from server component builds\n    console.log(`[Client] Checking feature '${feature}' status.`);\n    return {\n      name: feature,\n      enabledInServer: false,\n      message: `Feature '${feature}' is active on the client.`\n    };\n  }\n};\n\n// Example usage in a shared component or utility\nexport function MySharedComponent() {\n  const status = getFeatureStatus(\"new-dashboard-widget\");\n  return (\n    <div>\n      <h2>Feature Status: {status.name}</h2>\n      <p>{status.message}</p>\n      {rsc && <p>Server-side logic: {status.enabledInServer ? 'Enabled' : 'Disabled'}</p>}\n      {!rsc && <p>Client-side logic: {status.enabledInServer ? 'Enabled' : 'Disabled'}</p>}\n    </div>\n  );\n}","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}