Guide

What runs where

A component runs once, on the server. The browser gets the HTML and the values that the code of the user's actions needs, and nothing else. This page says which code runs where.

#The server renders, the browser resumes

So the body of a component is server code. A console.log at the top of a component prints in the terminal of the server.

#Client chunks

Code runs in the browser only when a chunk holds it:

A chunk holds the module-level imports and bindings that this code uses. Everything else stays on the server.

#Data crosses as props and model

The only way a value gets from the server to the browser is the model, which holds your let state and props, and which is plain data. Read a secret or a file in the component body, and pass the result in as a prop or a let. Do not import the secret into code that a handler or a binding runs.

#What fails the build

The build fails in two cases. vite dev only warns, when the chunk is first requested, and still serves it.

The error names the chain, from the component and its chunk through each import to the cause:

idlip: the client chunk handler=add of /src/app.tsx imports a Node.js module: /src/app.tsx > handler=add > /src/db.ts > node:fs.

Code that only the server render reaches is not checked: the body of a component outside its handlers and bindings, and the entry. A component may import node:fs and read a file there.

A build checks the code that stays in the chunk after unused code is removed, so a server-only function in a shared file that no chunk calls passes. vite dev has no such step. It follows the imports of your own files and does not look into packages, so it can warn about an import that a build removes, and miss one in a package. The build is the gate.

#Server functions

A marker for a function that only runs on the server, 'use server', comes with server functions in a later release. There is no .server.ts naming convention. The two checks above catch the mistakes that a marker would not.