Queue
Send slow work to a queue so your route replies at once, and the work runs later.
Deploys on
server/api/signup.post.tsSame on every host
import { defineEventHandler, readValidatedBody } from 'h3'
import * as v from 'valibot'
import { sendQueue } from 'vite-hub/queue'
// Returns when the queue accepts the job, before it runs
export default defineEventHandler(async (event) => {
const { email } = await readValidatedBody(event, body => v.parse(v.object({ email: v.string() }), body))
const [error, result] = await sendQueue('welcome-email', { email })
if (error) throw error
return result
})Deploy on
Cloudflare: Queues
One file per queue
Create a file in
server/queues. The file name is the queue name, and its handler runs each job.Reply first, work later
sendQueue() returns once the queue accepts the job. The job runs after your route replies.Node, Cloudflare, or Vercel
On Node, jobs run in the server process. With a Postgres Default Database, every instance shares the jobs. Without one, the default
local driver keeps jobs on one instance. Set driver: 'postgres' with QUEUE_POSTGRES_URL to share them. On Cloudflare and Vercel, they run on Cloudflare Queues or Vercel Queues.