Skip to content

Commit 1b3d110

Browse files
committed
tweak
1 parent f2260b9 commit 1b3d110

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

packages/plugin-rsc/README.md

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,14 @@ See also [Vite documentation](https://vite.dev/guide/api-hmr.html#intellisense-f
500500
501501
## `server-only` and `client-only` import
502502
503+
<!-- references? -->
504+
<!-- https://nextjs.org/docs/app/getting-started/server-and-client-components#preventing-environment-poisoning -->
505+
<!-- https://overreacted.io/how-imports-work-in-rsc/ -->
506+
503507
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.
504508
509+
For example, with the following codes:
510+
505511
- server-utils.js
506512
507513
```tsx
@@ -525,17 +531,13 @@ import { getData } from './server-utils.js' // ❌ This will fail at build time
525531
...
526532
```
527533
528-
When attempting to import server-only modules in client code, the plugin will show an error:
534+
the plugin will show an error:
529535
530536
```sh
531-
✘ [ERROR] "server-only" cannot be imported from a Client Component module. It should only be used from a Server Component.
532-
533-
client.js:2:25:
534-
2import { getData } from './server-utils.js'
535-
~~~~~~~~~~~~~~~~~~~
537+
[rsc:validate-imports] 'server-only' cannot be imported in client build (importer: '/.../client.js', ...)
536538
```
537539
538-
Similarly, `client-only` import can ensure browser-specific code isn't accidentally imported in server environment:
540+
Similarly, `client-only` import can ensure browser-specific code isn't accidentally imported in server environment. For example,
539541
540542
- client-utils.js
541543
@@ -559,24 +561,16 @@ export function ServerComponent() {
559561
}
560562
```
561563
562-
This will similarly fail at build time:
564+
the plugin will show an error:
563565
564566
```sh
565-
✘ [ERROR] "client-only" cannot be imported from a Server Component module. It should only be used from a Client Component.
566-
567-
server.js:1:27:
568-
1import { trackEvent } from './client-utils.js'
569-
~~~~~~~~~~~~~~~~~~~
567+
[rsc:virtual-client-package] 'client-only' cannot be imported in server build (importer: '/.../server.js', ...)
570568
```
571569
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.
570+
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 provides build-time validation instead of runtime errors.
573571
574572
This build-time validation is enabled by default and can be disabled by setting `validateImports: false` in the plugin options.
575573
576-
<!-- Learn more in -->
577-
<!-- https://nextjs.org/docs/app/getting-started/server-and-client-components#preventing-environment-poisoning -->
578-
<!-- https://overreacted.io/how-imports-work-in-rsc/ -->
579-
580574
## Credits
581575
582576
This project builds on fundamental techniques and insights from pioneering Vite RSC implementations.

packages/plugin-rsc/examples/starter/src/root.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import viteLogo from '/vite.svg'
33
import { getServerCounter, updateServerCounter } from './action.tsx'
44
import reactLogo from './assets/react.svg'
55
import { ClientCounter } from './client.tsx'
6+
import 'client-only'
67

78
export function Root(props: { url: URL }) {
89
return (

0 commit comments

Comments
 (0)