Previews on Laravel Cloud
Serve project previews and shells through a Cloudflare Worker when OneDrop runs on Laravel Cloud with Blaxel or Runtime sandboxes.
Serve project previews and shells through a Cloudflare Worker when OneDrop runs on Laravel Cloud with Blaxel or Runtime sandboxes.
On a server, Caddy serves each project’s preview and shell at its own address and checks every request with OneDrop (Previews and shells on a server). Laravel Cloud has no Caddy, so a Cloudflare Worker does the same job in front of the sandbox’s provider:
| Address | Goes to |
|---|---|
preview-<sandbox id>.<domain> | The project’s app, on Blaxel or Runtime |
shell-<sandbox id>.<domain> | The project’s web terminal |
<name>-<project id>.<domain> | A project published to your domain |
relay.<domain> | The device relay: projects running on a user’s computer, through the desktop app |
Blaxel and Runtime protect private previews with a cookie on their own domain. Browsers limit cookies for another site shown inside a page, so inside OneDrop’s Preview tab:
Through the Worker, the browser only deals with addresses and cookies under your own domain, and the provider’s token never reaches it.
preview-<id>.<domain>/__onedrop/enter with a short-lived token. The Worker asks OneDrop to check it, and OneDrop sets that address’s own cookie. The app’s login cookie never reaches these addresses.X-OneDrop-Host), because the provider sees its own address. With it, the sandbox can tell the page’s own Origin apart from another site’s, so apps that check it (Phoenix LiveView in Plausible, Vite’s hot reload) accept their WebSockets.A project published publicly to your domain needs no cookie, so the Worker shares that answer between visitors for a minute. A privately published one answers with a redirect to sign in to OneDrop, which the Worker passes on. Names under your domain that only look like a published app, but aren’t one, go on to their own origin.
Only the Worker can ask OneDrop where a sandbox lives: it proves itself with a shared secret.
You need your domain’s DNS on Cloudflare, with a proxied wildcard record (*.<domain>). Keep your app’s own records (the root, www, docs) on DNS only.
Use a domain of its own for the gateway, not the one your app is on: OneDrop’s hosted install is at onedrop.io and serves previews, shells and published apps from onedrop.dev. Then nothing a sandbox serves is on your app’s site, so a published app can’t phish under your app’s name or set cookies for it, and a flagged app doesn’t get your app’s domain flagged with it. Put that domain on the Public Suffix List too, so browsers keep each app’s cookies to its own address.
openssl rand -hex 32
Edit infra/cloudflare/preview-gateway/wrangler.toml for your domain (routes, APP_URL, GATEWAY_DOMAIN), then, with a Cloudflare API token that can edit Workers scripts and routes:
cd infra/cloudflare/preview-gateway
npx wrangler deploy
npx wrangler secret put GATEWAY_SECRET
Laravel Cloud runs web requests and queue jobs on separate instances, each with its own local disk. Attach an object storage bucket to the environment as its default disk, so share cards, project icons, chat attachments and code backups are saved where every instance can read them.
Set the managed queue’s visibility timeout to at least 3700 seconds. Making a task’s copy of the app, importing a repository, deploying or backing up the database can each take many minutes; with Cloud’s default of 180 seconds, the queue hands the job to a second worker partway through and it fails as “attempted too many times”.
Turn on the Scheduler for the environment’s App cluster, so database backups and old sandbox image clean-up run. On Laravel Cloud, OneDrop leaves out the scheduled tasks that only matter on a server of your own (the Docker idle check and server monitoring), so the app can still sleep when nobody’s using it.
Users can point their own domains at apps published to your domain (Custom domains). Through the Worker, each one is a Cloudflare for SaaS custom hostname on your zone: the user adds a CNAME to a fallback origin of yours, Cloudflare validates it and issues the certificate, and the Worker serves the app. A custom domain no app has gets a “not connected” page.
In the Cloudflare dashboard, open your zone, then SSL/TLS → Custom Hostnames, and enable it. The first 100 custom hostnames are free; more cost $0.10 a month each.
Add a proxied DNS record for the name users will point at, such as domains.onedrop.dev (an AAAA record to 100:: works, since the Worker answers every request), and set it as the Fallback Origin.
Uncomment the */* route in wrangler.toml, which sends custom hostnames to the Worker, and deploy it again. Cloudflare refuses it until Cloudflare for SaaS is on, and then sets none of the routes. The app’s own host passes straight through.
Set SANDBOX_GATEWAY_CLOUDFLARE_ZONE_ID (the zone’s ID), SANDBOX_GATEWAY_CLOUDFLARE_TOKEN (an API token with SSL and Certificates: Edit on the zone) and SANDBOX_GATEWAY_DOMAINS_TARGET (the fallback origin, e.g. domains.onedrop.dev), then redeploy.
Without these, Tools → Domains says custom domains aren’t set up for apps published to your domain. Hosted apps don’t need them: they use Fly.io certificates and Workers custom domains in the hosting accounts.
To serve the gateway from a new domain, keep the old one’s wildcard route in wrangler.toml and set FORMER_GATEWAY_DOMAIN to it beside the new GATEWAY_DOMAIN. The Worker then sends every old preview, shell and published-app address to the same name and page on the new domain (a permanent redirect), so links people shared keep working, and lets the old domain’s other names through. Deploy the Worker, change SANDBOX_GATEWAY_DOMAIN in OneDrop and redeploy: it moves each app published to the domain to its address on the new one. The desktop app picks up the new relay address the next time it reconnects.
The desktop app can run a project’s sandbox in the computer’s own Docker. The hosted app can’t reach a computer behind a home router, so the Worker carries a device relay: a Durable Object per computer (DeviceRelay, bound as DEVICE_RELAY in wrangler.toml), at relay.<domain>, which the wildcard route already covers.
The first npx wrangler deploy with the relay creates the Durable Object class. Durable Objects need the Workers Paid plan, or the free plan’s SQLite-backed objects, which is what the relay uses. Without a Worker gateway (an install behind Caddy), OneDrop doesn’t offer running projects on a computer. The relay’s protocol is described in the developer notes.
The Worker’s own tests run with node --test infra/cloudflare/preview-gateway/worker.test.mjs infra/cloudflare/preview-gateway/device-relay.test.mjs.
The wildcard record sends every name under your domain to the Worker, including ones bots make up (analytics.<domain>, kanban.<domain>), and each counts against the Workers free plan’s 100,000 requests a day. When it runs out, every preview and Shell answers error 1027 until midnight UTC. Add a WAF custom rule (Security → WAF → Custom rules) with the action Block, which runs before the Worker, so blocked requests don’t count:
(ends_with(http.host, ".<domain>") and not (http.host in {"www.<domain>" "relay.<domain>" "domains.<domain>" "docs.<domain>"} or starts_with(http.host, "preview-") or starts_with(http.host, "shell-") or http.host wildcard "*-*.<domain>"))
Add any other subdomain you use to the list. On a gateway domain of its own, the list is just relay.<domain> and domains.<domain>.
On Laravel Cloud, set SANDBOX_GATEWAY_DOMAIN to your domain and SANDBOX_GATEWAY_SECRET to the same secret (as a Secrets Manager secret), then redeploy.