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
@@ -405,7 +409,9 @@ This module re-exports RSC runtime API provided by `react-server-dom/client.brow
405
409
-`createFromFetch`: a robust way of `createFromReadableStream((await fetch("...")).body)`
406
410
-`encodeReply/setServerCallback`: server function related...
407
411
408
-
## CSS Support
412
+
## Tips
413
+
414
+
### CSS Support
409
415
410
416
The plugin automatically handles CSS code-splitting and injection for server components. This eliminates the need to manually call [`import.meta.viteRsc.loadCss()`](#importmetaviterscloadcss) in most cases.
411
417
@@ -439,11 +445,11 @@ export function Page() {
439
445
}
440
446
```
441
447
442
-
## Canary and Experimental channel releases
448
+
###Canary and Experimental channel releases
443
449
444
450
See https://github.com/vitejs/vite-plugin-react/pull/524 for how to install the package for React [canary](https://react.dev/community/versioning-policy#canary-channel) and [experimental](https://react.dev/community/versioning-policy#all-release-channels) usages.
445
451
446
-
## Using `@vitejs/plugin-rsc` as a framework package's `dependencies`
452
+
###Using `@vitejs/plugin-rsc` as a framework package's `dependencies`
447
453
448
454
By default, `@vitejs/plugin-rsc` is expected to be used as `peerDependencies` similar to `react` and `react-dom`. When `@vitejs/plugin-rsc` is not available at the project root (e.g., in `node_modules/@vitejs/plugin-rsc`), you will see warnings like:
449
455
@@ -474,7 +480,7 @@ export default function myRscFrameworkPlugin() {
474
480
}
475
481
```
476
482
477
-
## Typescript
483
+
### Typescript
478
484
479
485
Types for global API are defined in `@vitejs/plugin-rsc/types`. For example, you can add it to `tsconfig.json` to have types for `import.meta.viteRsc` APIs:
You can use the `server-only` import to prevent accidentally importing server-only code into client bundles, which can expose sensitive server code in public static assets.
510
+
For example, the plugin will show an error `'server-only' cannot be imported in client build` for the following code:
import { getData } from'./server-utils.js'// ❌ 'server-only' cannot be imported in client build
532
+
...
533
+
```
534
+
535
+
Similarly, the `client-only` import ensures browser-specific code isn't accidentally imported into server environments.
536
+
For example, the plugin will show an error `'client-only' cannot be imported in server build` for the following code:
537
+
538
+
- client-utils.js
539
+
540
+
```tsx
541
+
import'client-only'
542
+
543
+
exportfunctiongetStorage(key) {
544
+
// This uses browser-only APIs
545
+
returnwindow.localStorage.getItem(key)
546
+
}
547
+
```
548
+
549
+
- server.js
550
+
551
+
```tsx
552
+
import { getStorage } from'./client-utils.js'// ❌ 'client-only' cannot be imported in server build
553
+
554
+
exportfunctionServerComponent() {
555
+
constdata=getStorage("settings")
556
+
...
557
+
}
558
+
```
559
+
560
+
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 overrides these imports and surfaces their runtime errors as build-time errors.
561
+
562
+
This build-time validation is enabled by default and can be disabled by setting `validateImports:false` in the plugin options.
563
+
497
564
## Credits
498
565
499
566
This project builds on fundamental techniques and insights from pioneering Vite RSC implementations.
0 commit comments