Skip to content

Commit f2260b9

Browse files
committed
tweak
1 parent 74097ed commit f2260b9

File tree

1 file changed

+12
-38
lines changed

1 file changed

+12
-38
lines changed

packages/plugin-rsc/README.md

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -500,19 +500,15 @@ See also [Vite documentation](https://vite.dev/guide/api-hmr.html#intellisense-f
500500
501501
## `server-only` and `client-only` import
502502
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.
508504
509505
- server-utils.js
510506
511507
```tsx
512508
import 'server-only'
513509

514510
export async function getData() {
515-
const res = await fetch('https://service-internal-service.com/data', {
511+
const res = await fetch('https://internal-service.com/data', {
516512
headers: {
517513
authorization: process.env.API_KEY,
518514
},
@@ -526,14 +522,10 @@ export async function getData() {
526522
```tsx
527523
'use client'
528524
import { getData } from './server-utils.js' // ❌ This will fail at build time
529-
530-
export function ClientComponent() {
531-
const data = await getData()
532-
return <div>{data.message}</div>
533-
}
525+
...
534526
```
535527
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:
537529
538530
```sh
539531
✘ [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
543535
~~~~~~~~~~~~~~~~~~~
544536
```
545537
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:
549539
550540
- client-utils.js
551541
552542
```tsx
553543
import 'client-only'
554544

555-
export function useLocalStorage(key: string) {
545+
export function getStorage(key) {
556546
// This uses browser-only APIs
557547
return window.localStorage.getItem(key)
558548
}
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-
}
566549
```
567550
568551
- server.js
569552
570553
```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
572555

573556
export function ServerComponent() {
574-
trackEvent('page_view') // This would crash on server
575-
return <div>Server Component</div>
557+
const data = getStorage("settings")
558+
...
576559
}
577560
```
578561
579-
The plugin will show a build-time error:
562+
This will similarly fail at build time:
580563
581564
```sh
582565
✘ [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:
586569
~~~~~~~~~~~~~~~~~~~
587570
```
588571
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.
595573
596-
```js
597-
rsc({
598-
validateImports: false, // Disable build-time validation
599-
})
600-
```
574+
This build-time validation is enabled by default and can be disabled by setting `validateImports: false` in the plugin options.
601575
602576
<!-- Learn more in -->
603577
<!-- https://nextjs.org/docs/app/getting-started/server-and-client-components#preventing-environment-poisoning -->

0 commit comments

Comments
 (0)