Skip to content

Latest commit

 

History

History
131 lines (89 loc) · 4.26 KB

react-router.md

File metadata and controls

131 lines (89 loc) · 4.26 KB
description Add Void beside the React Router Vite plugin for databases, storage, and deployment, without a separate Cloudflare adapter.
outline deep

React Router v7

Add voidPlugin() alongside the React Router v7 Vite plugin to use Void's database, storage, and deployment features. You don't need a separate Cloudflare adapter.

Setup

1. Create a new project

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

2. Install dependencies

npm install -D void

3. Configure vite.config.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.

4. Deploy

void connect --platform cloudflare
void deploy

void connect signs you in to Cloudflare, asks which account to use, and saves it as the deploy target. To deploy to a Void platform run by your team instead, see Deployment.

If you use React Router 8 and react-router.config.ts sets prerender, tell Void to build with React Router's CLI:

import { defineConfig } from 'void/config';

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

Otherwise void deploy runs vite build (or vp build with Vite+), which doesn't exit after React Router 8 prerenders (remix-run/react-router#15427), so the deploy never reaches the upload.

Using Void Features

Most Void platform features work with React Router. Use them in loaders and actions:

Void-managed auth is not supported in framework mode yet. Use Better Auth's official React Router integration directly for now.

Database

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

Use KV, object storage, and AI from server-side code with the same imports as a Void app. Add cron jobs in crons/ and queue consumers in queues/.

In dev and preview, send() succeeds but doesn't run this app's queue handler. In dev, run the handler with the manual queue trigger, using the dev server's port and the token in .void/dev-trigger-token. Preview reads queues/ and env.ts from the source tree, so don't change them between the build and preview.

Environment Variables

Declare variables in env.ts and read them with import { env } from "void/env". See Environment Variables 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.

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.

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:

import { env } from 'cloudflare:workers';

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