Server Side Rendering

When using the React hooks exported by Account Kit in a server-side rendered setting, you will see inconsistencies of the user state between the server and the client. This will lead to flashes of content when a user is logged in. To avoid this, the account state can be optimistically loaded on the server and passed to the client.

To enable this setting, you can set ssr: true when creating a config. We also make the config a function, so that we can call it once per request which allows for request-based isolation of the account state.

[config.ts]
1import { createConfig } from "@account-kit/react";
2import { sepolia, alchemy } from "@account-kit/infra";
3
4export const config = () =>
5 createConfig({
6 // required
7 transport: alchemy({ rpcUrl: "/api/rpc" }),
8 chain: sepolia,
9 ssr: true, // [!code ++]
10 });

This setting will defer hydration of the account state to the client after the initial mount.

Persisting the Account State

To consistently pass the state between the server and the client, you can pass in a cookie storage to the config object created above. The cookie storage allows the client state to be written serialized to a cookie which can be passed along to the server on each request. This allows the server to have access to certain parts of the account state when rendering, ensuring a consistent render between client and server (eg. user’s address displayed in the top nav). Instances which can only be created on the client will still not be available on the server, however. This includes the signer or smart contract account instances.

[config.ts]
1import {
2 createConfig,
3 cookieStorage, // [!code ++]
4} from "@account-kit/react";
5import { sepolia, alchemy } from "@account-kit/infra";
6import { QueryClient } from "@tanstack/react-query";
7
8export const queryClient = new QueryClient();
9
10// [!code focus:99]
11export const config = () =>
12 createConfig({
13 // required
14 transport: alchemy({ rpcUrl: "/api/rpc" }),
15 chain: sepolia,
16 ssr: true, // [!code ++]
17 storage: cookieStorage, // [!code ++]
18 });

Now, depending on your application, you can get the state from cookies and pass in the initialState to the AlchemyAccountProvider to hydrate the account state on the client.

Next.js App Directory

If you are using NextJS App Directory, you can read the cookie state and pass it to the providers like so:

1// @noErrors
2import React from "react";
3import { cookieToInitialState } from "@account-kit/core";
4import type { Metadata } from "next";
5import { Inter } from "next/font/google";
6import { headers } from "next/headers";
7import { config } from "./config";
8import "./globals.css";
9import { Providers } from "./providers";
10
11const inter = Inter({ subsets: ["latin"] });
12
13export const metadata: Metadata = {
14 title: "Embedded Accounts Getting Started",
15 description: "Embedded Accounts Quickstart Guide",
16};
17
18export default function RootLayout({
19 children,
20}: Readonly<{
21 children: React.ReactNode;
22}>) {
23 // This will allow us to persist state across page boundaries
24 const initialState = cookieToInitialState(
25 // the config here is just used to compute the initial state
26 config(),
27 headers().get("cookie") ?? undefined
28 );
29
30 return (
31 <html lang="en">
32 <body className={inter.className}>
33 <Providers initialState={initialState}>{children}</Providers>
34 </body>
35 </html>
36 );
37}

Next.js Pages Directory

Coming soon!

Vanilla SSR

Coming soon!