Deploy Preview Environments
Is there a recommended pattern to making Convex work well with Vercel Preview Deployments?
31 Replies
There's a community pattern that @void has used, to use your dev deployment to power the previews. It's not perfect, we're thinking about ephemeral deployments for this kind of thing.
Currently you only get two Convex deployments for a Convex project with one member: the production deployment and a personal dev deployment. You can use a Vercel environment variable config to choose which backend to deploy to (CONVEX_DEPLOY_KEY) with the build command and which backend URL the cliend should talk to (CONVEX_URL)
environment variables in .env files can be overriden per environment in Vercel with this UI
The downside here is that changes you make to your dev deployment locally will also apply to all of your preview deployments! So it's not safe to e.g. send this link to a client to preview the changes unless you hold off on local development.
Got it. That's what I was thinking of doing, but it has downsides obviously. It would be cool to be able to create ephemeral deployments, perhaps that only live max 7 days or something.
Thanks @Tristan that's definitely something we've thought a bit about before. Your vote for this helps the prioritization process!
@ballingt @Indy checking in on this. I've set CONVEX_DEPLOY_KEY and CONVEX_URL to be the url and key for my dev environment but it is still trying to deploy to production. convex.json is on disk though and has the prodUrl set to the prod url. I can't remove this file (or remove prodUrl) or it won't even try. It was my assumption that setting these environment variables would allow convex to deploy to my dev environment. This is also for a vercel preview environment. Any ideas or am I barking up the wrong tree here? thanks!
What's the command you're running to build?
"npx convex deploy" currently always deploys to prod
it sounds like you'd like to deploy to dev?
Convex doesn't currently support preview deploys, so there's no good solution here
ahh, yea. I would. otherwise I'm not sure how to make vercel preview environment work from a build pov
some users have tried using their dev environment for this, which works, but 1) there's only one, so only the most recent deploy preview is going to work for sure, and 2) there's only one, which is shared with local development, so local dev will clobber the deploy preview
yea, how do they even make that work though?
npx convex dev --once
ahh, --once.
is that an undocumented argument?
another option is not deploying convex functions, so you get to preview your frontend but not you backend functions. You can use your dev or prod backend for this.
Yes, it's come up a couple times so we can unhide it
ok, yea. 🤯 some documentation around this would be nice - even if to say it's not supported
helpful to hear, yeah absolutely
either way it sounds like I need to add conditionals to the vercel build steps
We'll have something better here before long, but in the meantime communicating these patterns better makes sense
thanks, appreciate it.
yeah exactly, the most common setup is preview builds not pushing functions at all
and the VITE_CONVEX_URL specifies which backend to use from the frontemd, prod or dev
how/where is that value used?
In our demos we pass it to the Convex client
VITE_CONVEX_URL?
there's nothing automatic about it, it's whatever you set up
Or NEXT_PUBLIC_CONVEX_URL
or whatever your bundler requires you to use
Are you using Convex in the frontend or backend?
and that's used for "dev"? using next for the frontend via vercel and convex for the backend
And you call your Convex functions from the frontend, with hooks like useQuery?
yep
What does the code where you build a ConvexReactClient look like?
(usually in pages/_app.tsx/jsx)
I'll dig that up. unfortunately I need to run. will be online later. thanks!
It probably reads an environment variable, called NEXT_PUBLIC_CONVEX_URL - that's controls which backend the convex client talks to.
ttyl!
yea we get it via NEXT_PUBLIC_CONVEX_URL in a vercel env variable. that's the easy part! I added a check to not deploy if in the preview build. seems to work. Would be really nice to have support for preview environments (or integration builds, etc) so we can have a true CI/CD system in place. looking forward to that announcement 😉 !
@tylerkohn @Tristan preview deploys are out in 1.5 🎉 https://news.convex.dev/announcing-convex-1-5/
Convex News
Announcing Convex 1.5
Convex 1.5 adds support for preview deployments, improvements to the Convex runtime, and more.