> ## Documentation Index
> Fetch the complete documentation index at: https://docs.privacyboost.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Ssr support

# SSR Support

This guide covers server-side rendering considerations for Next.js, Remix, and other SSR frameworks.

## The Challenge

The Privacy Boost SDK uses WebAssembly and browser APIs that aren't available on the server. You need to ensure the SDK only loads on the client. No bundler configuration is needed for the WebAssembly itself: it ships inside the package as base64.

## Next.js

### App Router

```tsx theme={null}
// app/providers.tsx
'use client';

import { PrivacyBoostProvider } from '@sunnyside-io/privacy-boost-react';

export function Providers({ children }: { children: React.ReactNode }) {
  return (
    <PrivacyBoostProvider
      config={{
        appId: process.env.NEXT_PUBLIC_APP_ID!,
        serverUrl: process.env.NEXT_PUBLIC_SERVER_URL!,
      }}
      loadingComponent={<div>Loading...</div>}
    >
      {children}
    </PrivacyBoostProvider>
  );
}
```

```tsx theme={null}
// app/layout.tsx
import { Providers } from './providers';

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html>
      <body>
        <Providers>{children}</Providers>
      </body>
    </html>
  );
}
```

### Pages Router

```tsx theme={null}
// pages/_app.tsx
import type { AppProps } from 'next/app';
import dynamic from 'next/dynamic';

// Dynamic import with SSR disabled
const PrivacyBoostProvider = dynamic(
  () => import('@sunnyside-io/privacy-boost-react').then(mod => mod.PrivacyBoostProvider),
  { ssr: false }
);

export default function App({ Component, pageProps }: AppProps) {
  return (
    <PrivacyBoostProvider config={config}>
      <Component {...pageProps} />
    </PrivacyBoostProvider>
  );
}
```

### Dynamic Component Import

For components that use SDK hooks:

```tsx theme={null}
// components/Wallet.tsx
'use client';

import { useAuth } from '@sunnyside-io/privacy-boost-react';

export function Wallet() {
  const { state, signInWithWalletAdapter } = useAuth();
  // ...
}
```

```tsx theme={null}
// pages/index.tsx (Pages Router)
import dynamic from 'next/dynamic';

// `Wallet` is a named export, so pick it off the module.
const Wallet = dynamic(() => import('../components/Wallet').then((mod) => mod.Wallet), {
  ssr: false,
  loading: () => <div>Loading wallet...</div>,
});

export default function Home() {
  return <Wallet />;
}
```

## Remix

### Client-Only Provider

```tsx theme={null}
// app/components/PrivacyProvider.client.tsx
import { PrivacyBoostProvider } from '@sunnyside-io/privacy-boost-react';

export function PrivacyProvider({ children }: { children: React.ReactNode }) {
  return (
    <PrivacyBoostProvider config={config}>
      {children}
    </PrivacyBoostProvider>
  );
}
```

```tsx theme={null}
// app/root.tsx
import { Outlet } from '@remix-run/react';
import { ClientOnly } from 'remix-utils';
import { PrivacyProvider } from './components/PrivacyProvider.client';

export default function App() {
  return (
    <html>
      <body>
        <ClientOnly fallback={<div>Loading...</div>}>
          {() => (
            <PrivacyProvider>
              <Outlet />
            </PrivacyProvider>
          )}
        </ClientOnly>
      </body>
    </html>
  );
}
```

## Hydration Mismatch Prevention

### useIsClient Hook

```tsx theme={null}
function useIsClient() {
  const [isClient, setIsClient] = useState(false);

  useEffect(() => {
    setIsClient(true);
  }, []);

  return isClient;
}

function WalletButton() {
  const isClient = useIsClient();
  const { state, signInWithWalletAdapter } = useAuth();

  if (!isClient) {
    return <button disabled>Loading...</button>;
  }

  if (state.status === 'signedIn') {
    return <span>Signed in</span>;
  }
  return <button onClick={() => signInWithWalletAdapter()}>Sign in</button>;
}
```

### Conditional Rendering

```tsx theme={null}
function ClientOnlyComponent({ children }: { children: React.ReactNode }) {
  const [mounted, setMounted] = useState(false);

  useEffect(() => {
    setMounted(true);
  }, []);

  if (!mounted) {
    return null;
  }

  return <>{children}</>;
}

// Usage
function Page() {
  return (
    <ClientOnlyComponent>
      <WalletComponent />
    </ClientOnlyComponent>
  );
}
```

## Environment Variables

### Next.js

```env theme={null}
# .env.local
NEXT_PUBLIC_APP_ID=your-app-id
NEXT_PUBLIC_SERVER_URL=https://optimism.sepolia.privacyboost.io
```

### Remix

```tsx theme={null}
// app/root.tsx
import { json } from '@remix-run/node';

export async function loader() {
  return json({
    ENV: {
      APP_ID: process.env.APP_ID,
      SERVER_URL: process.env.SERVER_URL,
    },
  });
}
```

## Testing SSR

```tsx theme={null}
// Check if window is available
function isBrowser() {
  return typeof window !== 'undefined';
}

function SafeComponent() {
  if (!isBrowser()) {
    return <ServerFallback />;
  }

  return <ClientComponent />;
}
```

## Best Practices

1. **Always use 'use client'** directive for components using SDK hooks
2. **Dynamic imports** for large components to reduce bundle size
3. **Provide fallbacks** for loading and server-rendered states
4. **Avoid window/document** access without checking environment
5. **Use environment variables** with NEXT\_PUBLIC\_ prefix for client access

## Next Steps

* [Testing](https://docs.privacyboost.io/sdk/react/advanced/testing.md)
* [Custom Hooks](https://docs.privacyboost.io/sdk/react/advanced/custom-hooks.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.