|
| 1 | +# Fix for 'use client' and 'use server' Directive Conflict |
| 2 | + |
| 3 | +## Problem |
| 4 | +The build was failing with the error: |
| 5 | +``` |
| 6 | +Error: × It's not possible to have both `use client` and `use server` directives in the same file. |
| 7 | +``` |
| 8 | + |
| 9 | +This happened because the client-side `Form` component was directly importing the server action `contactUsFormSubmit`, which caused the bundler to try to include both client and server code in the same file. |
| 10 | + |
| 11 | +## Solution |
| 12 | +The solution was to separate the client and server code: |
| 13 | + |
| 14 | +### 1. Modified the Form Component |
| 15 | +- **File**: `src/sections/form/index.tsx` |
| 16 | +- **Changes**: |
| 17 | + - Removed the direct import of `contactUsFormSubmit` |
| 18 | + - Made the `Form` component accept the server action as a prop |
| 19 | + - Added proper TypeScript interfaces for the props |
| 20 | + |
| 21 | +### 2. Created Server Export |
| 22 | +- **File**: `src/sections/form/server/index.ts` |
| 23 | +- **Purpose**: Exports the server action separately so it can be imported by consumers |
| 24 | +- **Exports**: `contactUsFormSubmit` function and `ContactUsSchemaType` type |
| 25 | + |
| 26 | +### 3. Updated Package Configuration |
| 27 | +- **File**: `package.json` |
| 28 | +- **Changes**: Added export path for server components: |
| 29 | + ```json |
| 30 | + { |
| 31 | + "exports": { |
| 32 | + "./server": { |
| 33 | + "import": { |
| 34 | + "types": "./dist/sections/form/server/index.d.ts", |
| 35 | + "default": "./dist/sections/form/server/index.mjs" |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + ``` |
| 41 | + |
| 42 | +### 4. Updated Build Configuration |
| 43 | +- **File**: `tsup.config.ts` |
| 44 | +- **Changes**: Ensured proper directive preservation and file filtering |
| 45 | + |
| 46 | +## Usage |
| 47 | + |
| 48 | +### Client Component Usage |
| 49 | +```tsx |
| 50 | +// In your Next.js page or component |
| 51 | +import { Form } from 'cortex-react-components' |
| 52 | +import { contactUsFormSubmit } from 'cortex-react-components/server' |
| 53 | + |
| 54 | +export default function ContactPage() { |
| 55 | + return ( |
| 56 | + <div> |
| 57 | + <h1>Contact Us</h1> |
| 58 | + <Form onSubmit={contactUsFormSubmit} /> |
| 59 | + </div> |
| 60 | + ) |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +### Server Action Usage |
| 65 | +```tsx |
| 66 | +// The server action can be imported separately |
| 67 | +import { contactUsFormSubmit, type ContactUsSchemaType } from 'cortex-react-components/server' |
| 68 | + |
| 69 | +// Use in server components or API routes |
| 70 | +const handleSubmit = async (values: ContactUsSchemaType) => { |
| 71 | + const result = await contactUsFormSubmit(values) |
| 72 | + // Handle result... |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +## Benefits of This Approach |
| 77 | + |
| 78 | +1. **Separation of Concerns**: Client and server code are properly separated |
| 79 | +2. **Bundling Flexibility**: The bundler can now properly handle client vs server code |
| 80 | +3. **Type Safety**: Full TypeScript support for both client and server components |
| 81 | +4. **Reusability**: The Form component can be used with any server action that matches the interface |
| 82 | +5. **Tree Shaking**: Server code is not included in client bundles and vice versa |
| 83 | + |
| 84 | +## Key Files Modified |
| 85 | + |
| 86 | +- `src/sections/form/index.tsx` - Updated Form component |
| 87 | +- `src/sections/form/server/index.ts` - New server export file |
| 88 | +- `src/sections/index.ts` - Added form export |
| 89 | +- `package.json` - Added server export path |
| 90 | +- `tsup.config.ts` - Updated build configuration |
| 91 | + |
| 92 | +This fix ensures that the library can be built and used without directive conflicts while maintaining full functionality and type safety. |
0 commit comments