Skip to content

Commit d96632c

Browse files
[Docs] .server modules and .client modules (#12502)
* [Docs] .server modules * Remove line-break * Signing CLA * Fix line-break * .client modules * Fix line-break * Apply suggestions from code review --------- Co-authored-by: Brooks Lybrand <[email protected]>
1 parent a6353f8 commit d96632c

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@
225225
- pavsoldatov
226226
- pcattori
227227
- petersendidit
228+
- pierophp
228229
- printfn
229230
- promet99
230231
- proshunsuke

docs/explanation/special-files.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,45 @@ For an example, please refer to the default [`entry.server.tsx`][node-streaming-
281281

282282
Note that this does not handle thrown `Response` instances from your `loader`/`action` functions. The intention of this handler is to find bugs in your code which result in unexpected thrown errors. If you are detecting a scenario and throwing a 401/404/etc. `Response` in your `loader`/`action` then it's an expected flow that is handled by your code. If you also wish to log, or send those to an external service, that should be done at the time you throw the response.
283283

284+
## `.server` modules
285+
286+
While not strictly necessary, `.server` modules are a good way to explicitly mark entire modules as server-only.
287+
The build will fail if any code in a `.server` file or `.server` directory accidentally ends up in the client module graph.
288+
289+
```txt
290+
app
291+
├── .server 👈 marks all files in this directory as server-only
292+
│ ├── auth.ts
293+
│ └── db.ts
294+
├── cms.server.ts 👈 marks this file as server-only
295+
├── root.tsx
296+
└── routes.ts
297+
```
298+
299+
`.server` modules must be within your app directory.
300+
301+
Refer to the Route Module section in the sidebar for more information.
302+
303+
# `.client` modules
304+
305+
While uncommon, you may have a file or dependency that uses module side effects in the browser. You can use `*.client.ts` on file names or nest files within `.client` directories to force them out of server bundles.
306+
307+
```ts filename=feature-check.client.ts
308+
// this would break the server
309+
export const supportsVibrationAPI =
310+
"vibrate" in window.navigator;
311+
```
312+
313+
Note that values exported from this module will all be `undefined` on the server, so the only places to use them are in [`useEffect`][use_effect] and user events like click handlers.
314+
315+
```ts
316+
import { supportsVibrationAPI } from "./feature-check.client.ts";
317+
318+
console.log(supportsVibrationAPI);
319+
// server: undefined
320+
// client: true | false
321+
```
322+
284323
[react-router-config]: https://api.reactrouter.com/v7/types/_react_router_dev.config.Config.html
285324
[route-module]: ../start/framework/route-module
286325
[routing]: ../start/framework/routing
@@ -289,3 +328,4 @@ Note that this does not handle thrown `Response` instances from your `loader`/`a
289328
[rendertopipeablestream]: https://react.dev/reference/react-dom/server/renderToPipeableStream
290329
[rendertoreadablestream]: https://react.dev/reference/react-dom/server/renderToReadableStream
291330
[node-streaming-entry-server]: https://github.com/remix-run/react-router/blob/dev/packages/react-router-dev/config/defaults/entry.server.node.tsx
331+
[use_effect]: https://react.dev/reference/react/useEffect

0 commit comments

Comments
 (0)