You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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]>
Copy file name to clipboardExpand all lines: packages/plugin-rsc/README.md
+53-14Lines changed: 53 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -354,7 +354,7 @@ export default defineConfig({
354
354
serverHandler:false,
355
355
356
356
// 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.
358
358
validateImports:true,
359
359
360
360
// 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
500
500
501
501
## `server-only` and `client-only` import
502
502
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.
504
504
505
-
For example, ...todo
505
+
### Using `server-only`
506
+
507
+
The `server-only` import prevents sensitive server code from being included in client bundles:
506
508
507
509
- server-utils.js
508
510
@@ -523,42 +525,79 @@ export async function getData() {
523
525
524
526
```tsx
525
527
'use client'
526
-
import { getData } from'./api-utils.js'
528
+
import { getData } from'./server-utils.js'// ❌ This will fail at build time
527
529
528
530
exportfunctionClientComponent() {
529
531
constdata=awaitgetData()
530
532
return<div>{data.message}</div>
531
533
}
532
534
```
533
535
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:
535
537
536
538
```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
+
2 │ import { getData } from'./server-utils.js'
543
+
╵ ~~~~~~~~~~~~~~~~~~~
538
544
```
539
545
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
+
exportfunctionuseLocalStorage(key:string) {
556
+
// This uses browser-only APIs
557
+
returnwindow.localStorage.getItem(key)
558
+
}
559
+
560
+
exportfunctiontrackEvent(eventName:string) {
561
+
// This might use browser-specific analytics
562
+
if (window.gtag) {
563
+
window.gtag('event', eventName)
564
+
}
565
+
}
566
+
```
541
567
542
568
- server.js
543
569
544
570
```tsx
571
+
import { trackEvent } from'./client-utils.js'// ❌ This will fail at build time
572
+
545
573
exportfunctionServerComponent() {
546
-
todo
574
+
trackEvent('page_view') // This would crash on server
575
+
return<div>Server Component</div>
547
576
}
548
577
```
549
578
550
-
- client-utils.js
579
+
The plugin will show a build-time error:
551
580
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.
554
583
555
-
todo
584
+
server.js:1:27:
585
+
1 │ import { trackEvent } from'./client-utils.js'
586
+
╵ ~~~~~~~~~~~~~~~~~~~
556
587
```
557
588
589
+
### Implementation Notes
590
+
558
591
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:
560
595
561
-
This build time valdiation is enabled by default and it can be disabled by `RscPluginOptions.validateImports:false`.
0 commit comments