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
Copy file name to clipboardExpand all lines: packages/plugin-rsc/README.md
+12-38Lines changed: 12 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -500,19 +500,15 @@ 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
-
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
-
505
-
### Using `server-only`
506
-
507
-
The `server-only` import prevents sensitive server code from being included in client bundles:
503
+
You can use `server-only` import to prevent accidentally importing server-only code on client, which can expose sensitive server code to public static assets.
@@ -526,14 +522,10 @@ export async function getData() {
526
522
```tsx
527
523
'use client'
528
524
import { getData } from'./server-utils.js'// ❌ This will fail at build time
529
-
530
-
exportfunctionClientComponent() {
531
-
constdata=awaitgetData()
532
-
return<div>{data.message}</div>
533
-
}
525
+
...
534
526
```
535
527
536
-
The plugin will show a build-time error when attempting to import server-only modules in client code:
528
+
When attempting to import server-only modules in client code, the plugin will show an error:
537
529
538
530
```sh
539
531
✘ [ERROR] "server-only" cannot be imported from a Client Component module. It should only be used from a Server Component.
@@ -543,40 +535,31 @@ The plugin will show a build-time error when attempting to import server-only mo
543
535
╵ ~~~~~~~~~~~~~~~~~~~
544
536
```
545
537
546
-
### Using `client-only`
547
-
548
-
The `client-only` import ensures browser-specific code isn't accidentally imported in server components:
538
+
Similarly, `client-only` import can ensure browser-specific code isn't accidentally imported in server environment:
549
539
550
540
- client-utils.js
551
541
552
542
```tsx
553
543
import'client-only'
554
544
555
-
exportfunctionuseLocalStorage(key:string) {
545
+
exportfunctiongetStorage(key) {
556
546
// This uses browser-only APIs
557
547
returnwindow.localStorage.getItem(key)
558
548
}
559
-
560
-
exportfunctiontrackEvent(eventName:string) {
561
-
// This might use browser-specific analytics
562
-
if (window.gtag) {
563
-
window.gtag('event', eventName)
564
-
}
565
-
}
566
549
```
567
550
568
551
- server.js
569
552
570
553
```tsx
571
-
import { trackEvent } from'./client-utils.js'// ❌ This will fail at build time
554
+
import { getStorage } from'./client-utils.js'// ❌ This will fail at build time
572
555
573
556
exportfunctionServerComponent() {
574
-
trackEvent('page_view') // This would crash on server
575
-
return<div>Server Component</div>
557
+
constdata=getStorage("settings")
558
+
...
576
559
}
577
560
```
578
561
579
-
The plugin will show a build-time error:
562
+
This will similarly fail at build time:
580
563
581
564
```sh
582
565
✘ [ERROR] "client-only" cannot be imported from a Server Component module. It should only be used from a Client Component.
@@ -586,18 +569,9 @@ The plugin will show a build-time error:
586
569
╵ ~~~~~~~~~~~~~~~~~~~
587
570
```
588
571
589
-
### Implementation Notes
590
-
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,
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:
572
+
Note that while 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, they don't need to be installed. The plugin internally handles these imports and provides build-time validation instead of runtime errors.
0 commit comments