GitHub 37
Skip to content
ViteHub is still experimental. Expect bugs and breaking changes.

Queue

Send slow work to a queue so your route replies at once, and the work runs later.

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
})
server/api/signup.post.ts
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.

Wait before a job runs

Pass delay, like '10m', to start a job later.

Typed names and data

A wrong queue name or bad job data is a type error in your editor.

Failed jobs run again

Queues can retry failed jobs, so write handlers that are safe to run twice.

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.