---
url: https://void.cloud/integrations/frameworks/react-router.md
description: >-
  Add Void beside the React Router Vite plugin for databases, storage, and
  deployment, without a separate Cloudflare adapter.
---

# React Router v7

Add `voidPlugin()` alongside the [React Router v7](https://reactrouter.com/) Vite plugin to use Void's database, storage, and deployment features. Void includes the Cloudflare adapter.

## Setup

### 1. Create a new project

```bash
npm create react-router@latest my-app
cd my-app
```

### 2. Install dependencies

```bash
npm install -D void
```

### 3. Configure `vite.config.ts`

```ts
import { reactRouter } from '@react-router/dev/vite';
import { defineConfig } from 'vite';
import { voidPlugin } from 'void';

export default defineConfig({
  plugins: [
    voidPlugin(), // must come before the framework plugin
    reactRouter(),
  ],
});
```

For React Router v7, set `future.v8_viteEnvironmentApi` to `true` in `react-router.config.ts` to enable the Vite Environment API.

If you use React Router 8 with `prerender` in `react-router.config.ts`, set the build command before your first deployment:

```ts
// void.config.ts
import { defineConfig } from 'void/config';

export default defineConfig({
  inference: { build: 'react-router build' },
});
```

This lets the build finish after prerendering so Void can upload the app. The default Vite build can stay open at that point. [React Router issue](https://github.com/remix-run/react-router/issues/15427)

### 4. Deploy

```bash
void connect --platform cloudflare
void deploy
```

On the first command, sign in and select your Cloudflare account. `void deploy` builds the app and publishes it to that account. To deploy to your team’s Void platform, see [Deployment](../../guide/deployment.md).

## Using Void Features

Use Void resources in loaders and actions. For authentication, use your framework’s Better Auth integration.

### Database

To show the users in your database, query the `users` table in the route’s `loader`. React Router passes the result to the route component.

```tsx
import { users } from '@schema';
import { db } from 'void/db';
import type { Route } from './+types/users';

export async function loader({}: Route.LoaderArgs) {
  return { users: await db.select().from(users) };
}
```

### Other resources

See [supported resources and background jobs](./overview.md#feature-support) for KV, storage, AI, cron jobs, and queues.

To run a queue consumer locally, start the dev server and use the [manual queue trigger](../../guide/queues.md#local-development). Use the dev server’s port and the token in `.void/dev-trigger-token`. Calling `send()` in dev or preview accepts the message without running the consumer. Preview reads `queues/` and `env.ts` from your project, so keep both as they were when you built the app.

### Environment Variables

Declare variables in `env.ts` and read them with `import { env } from "void/env"`. See [Environment Variables](../../guide/env-vars.md) for local values, production secrets, and public `VITE_*` values.

Read server secrets in a `.server.ts` companion module and import it into your loader or action. Void rejects server-secret reads in route files that also contain browser components.

## Custom Worker Entry

To customize how your Cloudflare Worker handles requests or add named exports such as a `WorkerEntrypoint`, set `cloudflare.main` to a path relative to your app root in `void.config.ts`.

```ts
import { defineConfig } from 'void/config';

export default defineConfig({
  cloudflare: { main: './worker.ts' },
});
```

In `worker.ts`, default-export the Worker handler and forward page requests to React Router.

```ts
import { createRequestHandler } from 'react-router';

const handleRequest = createRequestHandler(
  // @ts-expect-error React Router's Vite plugin provides this module.
  () => import('virtual:react-router/server-build'),
  import.meta.env.MODE,
);

export default {
  fetch(request: Request) {
    return handleRequest(request);
  },
};
```

## Accessing Bindings Directly

You can also access Cloudflare bindings via the `cloudflare:workers` module:

```tsx
import { env } from 'cloudflare:workers';

const { results } = await env.DB.prepare('SELECT * FROM users').all();
```
