This is a vinext starter app that uses Replicate to generate images with FLUX.2 [klein]. It deploys to Cloudflare Workers.
vinext is a drop-in replacement for Next.js built on Vite. It reimplements the Next.js API surface so your existing app/ directory, route handlers, and next/* imports work as-is, but the build runs on Vite and deploys to Cloudflare Workers with a single command.
This project is the vinext equivalent of replicate/getting-started-nextjs.
- app/page.tsx - React client component that renders the prompt form and displays generated images
- app/api/predictions/route.ts - API route that calls Replicate's API to create a prediction
- app/api/predictions/[id]/route.ts - API route that polls Replicate's API for the prediction result
- app/api/webhooks/route.ts - API route that receives and validates webhooks from Replicate
- worker/index.ts - Cloudflare Worker entry point with image optimization
- vite.config.ts - Vite config with vinext and Cloudflare plugins
- wrangler.jsonc - Cloudflare Worker configuration
Install dependencies:
npm install
Create a .dev.vars file for local secrets (this is how Cloudflare Workers handle environment variables in development):
echo "REPLICATE_API_TOKEN=<your-token-here>" > .dev.vars
Replace <your-token-here> with your Replicate API token. The .dev.vars file is git-ignored.
Run the development server:
npm run dev
Open http://localhost:5173 in your browser.
npm run deploy
This builds the app and deploys it to Cloudflare Workers. You'll need to be logged in to Cloudflare via npx wrangler login.
After deploying, set your Replicate API token as a secret:
npx wrangler secret put REPLICATE_API_TOKEN
Webhooks provide real-time updates about your predictions. When you create a prediction, you can specify a URL that Replicate will send HTTP POST requests to as the prediction progresses.
- The
WORKER_URL(orNGROK_HOSTin development) environment variable is used to construct the webhook URL when creating a prediction in app/api/predictions/route.ts. - Replicate sends POST requests to the handler in app/api/webhooks/route.ts as the prediction is updated.
To test webhooks in development, you need a secure tunnel so Replicate can reach your local server:
- Download and set up
ngrok - Run ngrok:
ngrok http 5173 - Add the ngrok URL to
.dev.vars:NGROK_HOST=https://your-id.ngrok.app - Leave ngrok running
- In a separate terminal, run
npm run dev - Open localhost:5173 and enter a prompt
For production, set WORKER_URL to your Worker's URL (e.g. https://getting-started-vinext.you.workers.dev or your custom domain):
npx wrangler secret put WORKER_URL
- Get your signing secret:
curl -s -X GET -H "Authorization: Bearer $REPLICATE_API_TOKEN" https://api.replicate.com/v1/webhooks/default/secret - Add the secret to
.dev.vars:REPLICATE_WEBHOOK_SIGNING_SECRET=whsec_... - Now webhook requests will be validated in app/api/webhooks/route.ts.
For production:
npx wrangler secret put REPLICATE_WEBHOOK_SIGNING_SECRET
