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
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
+
499
564
## Credits
500
565
501
566
This project builds on fundamental techniques and insights from pioneering Vite RSC implementations.
0 commit comments