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: docs/01-app/01-getting-started/08-server-and-client-components.mdx
+24-6Lines changed: 24 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -507,7 +507,7 @@ export default function Page() {
507
507
508
508
### Preventing environment poisoning
509
509
510
-
JavaScript modules can be shared between both Server and Client Components modules. This means it's possible to accidentanlly import server-only code into the client. For example, consider the following function:
510
+
JavaScript modules can be shared between both Server and Client Components modules. This means it's possible to accidentally import server-only code into the client. For example, consider the following function:
511
511
512
512
```ts filename="lib/data.ts" switcher
513
513
exportasyncfunction getData() {
@@ -541,10 +541,6 @@ As a result, even though `getData()` can be imported and executed on the client,
541
541
542
542
To prevent accidental usage in Client Components, you can use the [`server-only` package](https://www.npmjs.com/package/server-only).
543
543
544
-
```bash filename="Terminal"
545
-
npm install server-only
546
-
```
547
-
548
544
Then, import the package into a file that contains server-only code:
549
545
550
546
```js filename="lib/data.js"
@@ -563,4 +559,26 @@ export async function getData() {
563
559
564
560
Now, if you try to import the module into a Client Component, there will be a build-time error.
565
561
566
-
> **Good to know**: The corresponding [`client-only` package](https://www.npmjs.com/package/client-only) can be used to mark modules that contain client-only logic like code that accesses the `window` object.
562
+
The corresponding [`client-only` package](https://www.npmjs.com/package/client-only) can be used to mark modules that contain client-only logic like code that accesses the `window` object.
563
+
564
+
In Next.js, installing `server-only` or `client-only` is **optional**. However, if your linting rules flag extraneous dependencies, you may install them to avoid issues.
565
+
566
+
```bash package="npm"
567
+
npm install server-only
568
+
```
569
+
570
+
```bash package="yarn"
571
+
yarn add server-only
572
+
```
573
+
574
+
```bash package="pnpm"
575
+
pnpm add server-only
576
+
```
577
+
578
+
```bash package="bun"
579
+
bun add server-only
580
+
```
581
+
582
+
Next.js handles `server-only` and `client-only` imports internally to provide clearer error messages when a module is used in the wrong environment. The contents of these packages from NPM are not used by Next.js.
583
+
584
+
Next.js also provides its own type declarations for `server-only` and `client-only`, for TypeScript configurations where [`noUncheckedSideEffectImports`](https://www.typescriptlang.org/tsconfig/#noUncheckedSideEffectImports) is active.
0 commit comments