Skip to content

Commit 2251d0f

Browse files
authored
1 parent b268cae commit 2251d0f

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

docs/01-app/01-getting-started/08-server-and-client-components.mdx

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ export default function Page() {
507507
508508
### Preventing environment poisoning
509509

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:
511511

512512
```ts filename="lib/data.ts" switcher
513513
export async function getData() {
@@ -541,10 +541,6 @@ As a result, even though `getData()` can be imported and executed on the client,
541541

542542
To prevent accidental usage in Client Components, you can use the [`server-only` package](https://www.npmjs.com/package/server-only).
543543

544-
```bash filename="Terminal"
545-
npm install server-only
546-
```
547-
548544
Then, import the package into a file that contains server-only code:
549545

550546
```js filename="lib/data.js"
@@ -563,4 +559,26 @@ export async function getData() {
563559

564560
Now, if you try to import the module into a Client Component, there will be a build-time error.
565561

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

Comments
 (0)