Skip to content

Commit 74097ed

Browse files
hi-ogawaclaude
andcommitted
docs(rsc): improve validateImports documentation with complete examples
- Add comprehensive examples for server-only and client-only imports - Include actual error messages developers will see - Fix typo: valdiation -> validation - Add cross-reference from validateImports option to detailed section - Replace all TODO placeholders with meaningful content 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 1461410 commit 74097ed

File tree

1 file changed

+53
-14
lines changed

1 file changed

+53
-14
lines changed

packages/plugin-rsc/README.md

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ export default defineConfig({
354354
serverHandler: false,
355355

356356
// this controls build-time validation of 'server-only' and 'client-only' imports.
357-
// this is enabled by default.
357+
// this is enabled by default. See the "server-only and client-only import" section for details.
358358
validateImports: true,
359359

360360
// by default, the plugin uses a build-time generated encryption key for
@@ -500,9 +500,11 @@ See also [Vite documentation](https://vite.dev/guide/api-hmr.html#intellisense-f
500500
501501
## `server-only` and `client-only` import
502502
503-
You can use `server-only` import to avoid accidentally leaking certain modules to client build static assets.
503+
The plugin provides build-time validation for `server-only` and `client-only` imports to prevent accidentally exposing server code to client bundles or importing browser-specific code in server environments.
504504
505-
For example, ...todo
505+
### Using `server-only`
506+
507+
The `server-only` import prevents sensitive server code from being included in client bundles:
506508
507509
- server-utils.js
508510
@@ -523,42 +525,79 @@ export async function getData() {
523525
524526
```tsx
525527
'use client'
526-
import { getData } from './api-utils.js'
528+
import { getData } from './server-utils.js' // ❌ This will fail at build time
527529

528530
export function ClientComponent() {
529531
const data = await getData()
530532
return <div>{data.message}</div>
531533
}
532534
```
533535
534-
The plugin will show a following error ....todo
536+
The plugin will show a build-time error when attempting to import server-only modules in client code:
535537
536538
```sh
537-
`server-only` cannot be imported in client build
539+
✘ [ERROR] "server-only" cannot be imported from a Client Component module. It should only be used from a Server Component.
540+
541+
client.js:2:25:
542+
2import { getData } from './server-utils.js'
543+
~~~~~~~~~~~~~~~~~~~
538544
```
539545
540-
On the other way around, `client-only` import ...todo
546+
### Using `client-only`
547+
548+
The `client-only` import ensures browser-specific code isn't accidentally imported in server components:
549+
550+
- client-utils.js
551+
552+
```tsx
553+
import 'client-only'
554+
555+
export function useLocalStorage(key: string) {
556+
// This uses browser-only APIs
557+
return window.localStorage.getItem(key)
558+
}
559+
560+
export function trackEvent(eventName: string) {
561+
// This might use browser-specific analytics
562+
if (window.gtag) {
563+
window.gtag('event', eventName)
564+
}
565+
}
566+
```
541567
542568
- server.js
543569
544570
```tsx
571+
import { trackEvent } from './client-utils.js' // ❌ This will fail at build time
572+
545573
export function ServerComponent() {
546-
todo
574+
trackEvent('page_view') // This would crash on server
575+
return <div>Server Component</div>
547576
}
548577
```
549578
550-
- client-utils.js
579+
The plugin will show a build-time error:
551580
552-
```tsx
553-
import 'client-only'
581+
```sh
582+
✘ [ERROR] "client-only" cannot be imported from a Server Component module. It should only be used from a Client Component.
554583

555-
todo
584+
server.js:1:27:
585+
1import { trackEvent } from './client-utils.js'
586+
~~~~~~~~~~~~~~~~~~~
556587
```
557588
589+
### Implementation Notes
590+
558591
Note that there are official npm packages [`server-only`](https://www.npmjs.com/package/server-only) and [`client-only`](https://www.npmjs.com/package/client-only) created by React team,
559-
but they don't need to be installed. `@vitejs/plugin-rsc` internally overrides them to provide a better error message during time instead of runtime error provided by the actual packages.
592+
but they don't need to be installed. `@vitejs/plugin-rsc` internally overrides them to provide a better error message during build time instead of the runtime error provided by the actual packages.
593+
594+
This build-time validation is enabled by default and can be disabled by setting `validateImports: false` in the plugin options:
560595
561-
This build time valdiation is enabled by default and it can be disabled by `RscPluginOptions.validateImports: false`.
596+
```js
597+
rsc({
598+
validateImports: false, // Disable build-time validation
599+
})
600+
```
562601
563602
<!-- Learn more in -->
564603
<!-- https://nextjs.org/docs/app/getting-started/server-and-client-components#preventing-environment-poisoning -->

0 commit comments

Comments
 (0)