Skip to content

Render on the server

A server has no wallet, and a page still has to show the account in its first HTML. The state of the connection is plain data: store it in a cookie, render from it on the server, and let the wallet confirm it in the browser.

page.tsx
import { Address, Connection } from "@ethect/core";
import { useConnection } from "./ethect";
// Rendered on the server, then again in the browser. Both start from `initial`.
export function Account({ initial }: { initial: Connection.State }) {
const connection = useConnection(initial);
switch (connection.status) {
case "connected": // the wallet says so. An address is shown in its EIP-55 form
return <p>{Address.checksum(connection.accounts[0])}</p>;
case "reconnecting": // the cookie says so, and the wallet has not answered yet
return (
<p className="unconfirmed">
{connection.accounts[0] ? Address.checksum(connection.accounts[0]) : "…"}
</p>
);
default:
return <p>Not connected</p>;
}
}
// On the server. `cookies` is the `Cookie` header of the request, or `null` when it has none.
export const Page = ({ cookies }: { cookies: string | null }) => (
<Account initial={Connection.fromCookie(cookies)} />
);

Account uses a hook: under React Server Components it is a client component, in a file of its own that starts with "use client".

ethect.tsx is the file of Let the user pick a wallet, with three additions: it starts from the cookie, it keeps the cookie in step, and it asks the remembered wallet once.

Loading this file on a server touches no browser API, and builds nothing: the runtime is made when something first runs on it, and on a server nothing does.

None of the three needs a server. In a page that only the browser renders, the cookie and Connection.reconnect keep the connection across a reload: start useConnection from { status: "disconnected" }, as the picker does, and the state follows.

The state The value of the connection cookie
connected, reconnecting {"v":1,"wallet":"io.metamask","accounts":["0x…"],"chainId":1}
disconnected, connecting {"v":1}

The id of the wallet, its accounts and its chain. No provider, no permission, no key and no signature. Connection.toCookie writes it percent-encoded, for the whole site and for a year. Connection.fromCookie finds it among the other cookies and never fails: anything it does not know reads as disconnected.

That is why a stored connection reads as reconnecting, and never as connected: only the wallet can say that it is connected.

When Where The state Account shows
the request arrives server reconnecting, read from the cookie the address of the cookie
the page hydrates browser the same: initial came with the HTML the same HTML
the wallet answers browser connected, with what the wallet says now the address, confirmed
the user changed account since browser connected, with the new account first the new address
the wallet is gone, or gives no account browser disconnected Not connected
  • The first render is the same on both sides. It comes from initial, which the server read and sent. The cookie the browser reads only tells the runtime which wallet to ask.
  • reconnect waits for the wallet, and gives up. A wallet announces itself a moment after the page loads. It is waited for two seconds, and its answer for two more: see reconnectTimeout.
  • A disconnection is stored too. After Connection.disconnect the cookie is {"v":1}, so the next visit does not connect the user again behind their back.
  • Nothing signs on a server. A send or a signature fails at once with SignerError "Disconnected". Reads work: they go to your node.

To render more than the address, read for it on the server. Build the layers for each request: the runtime of ethect.tsx is one connection, and on a server it would be every visitor’s.

server.ts
import { Effect, Layer } from "effect";
import { FetchHttpClient } from "effect/http";
import { Call, Connection, Transport, Wallets } from "@ethect/core";
// The layers of one request: the cookie is the state, and there is no wallet.
const Server = (cookies: string | null) =>
Connection.layer({ initial: Connection.fromCookie(cookies) }).pipe(
Layer.provideMerge(Wallets.of([])),
Layer.provideMerge(Transport.http("https://ethereum-rpc.publicnode.com")),
Layer.provide(FetchHttpClient.layer),
);
// The account of the cookie and its balance, or nothing.
const header = Effect.gen(function* () {
const state = yield* Connection.get;
const account = state.status === "reconnecting" ? state.accounts[0] : undefined;
return account && { account, balance: yield* Call.balance(account) };
});
export const load = (request: Request) =>
header.pipe(
Effect.provide(Server(request.headers.get("cookie"))),
Effect.orElseSucceed(() => undefined), // the node did not answer: render without it
Effect.runPromise,
);
  1. the server

    1. your nodeno wallet is asked: there is none. Your node is asked what you read
  2. the browser

    1. your programwaits for the remembered wallet to announce itself, up to 2 s
    2. the walleteth_accountsno prompt
    3. the walleteth_chainIdthen: no prompt
    4. your programthen listens to accountsChanged, chainChanged and disconnect
  3. each change

    1. your programthe cookie is written again. No request

eth_requestAccounts, the request that prompts, is never sent here: only a click on a wallet sends it. See Connection.reconnect, Connection.toCookie and Signer.connected.