RJ
RJ•3y ago

Convex with Remix 💿

I've just hooked up Convex to a 💿 Remix app (woohoo!), and ran into one small hiccup—Convex seems to think Remix is using Vite (probably because Remix uses esbuild, which Vite also uses), so every time I run npx convex dev, it asks me if I want to ? Create a .env.local file and add VITE_CONVEX_URL to save dev URL? (Y/n) . This is annoying because I have to turn it down every time I run that command, and also because running npx convex dev with concurrently (i.e. non-interactively) then doesn't work! I discovered the --no-save-url flag, which allows non-interactive execution again, and which is why it's only a minor hiccup 🙂
10 Replies
ballingt
ballingt•3y ago
Cool, then yeah I think your explanation is right: it uses vite, so assumes VITE_CONVEX_URL How does Remix handle enviroment variables, what would you like Convex to do here?
RJ
RJOP•3y ago
Remix uses .env for local development (https://remix.run/docs/en/v1/guides/envvars#local-development), so I perhaps want something very similar to what the CLI is trying to offer for Vite.
ballingt
ballingt•3y ago
actually dunno, not sure why it's assuming vite — I think the code checks for vite in the package.json dependencies
RJ
RJOP•3y ago
I can confirm that there is definitely no Vite in my package.json!
{
"private": true,
"sideEffects": false,
"scripts": {
"build": "remix build",
"dev": "concurrently 'convex dev --no-save-url' 'remix dev'",
"start": "cross-env NODE_ENV=production netlify dev",
"typecheck": "tsc"
},
"dependencies": {
"@fp-ts/schema": "^0.1.3",
"@netlify/functions": "^1.3.0",
"@remix-run/netlify": "^1.12.0",
"@remix-run/node": "^1.12.0",
"@remix-run/react": "^1.12.0",
"concurrently": "^7.6.0",
"convex": "^0.9.1",
"cross-env": "^7.0.3",
"fp-ts": "^2.13.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@remix-run/dev": "^1.12.0",
"@remix-run/eslint-config": "^1.12.0",
"@remix-run/serve": "^1.12.0",
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.8",
"eslint": "^8.27.0",
"eslint-plugin-simple-import-sort": "^10.0.0",
"typescript": "^4.8.4"
},
"engines": {
"node": ">=14"
}
}
{
"private": true,
"sideEffects": false,
"scripts": {
"build": "remix build",
"dev": "concurrently 'convex dev --no-save-url' 'remix dev'",
"start": "cross-env NODE_ENV=production netlify dev",
"typecheck": "tsc"
},
"dependencies": {
"@fp-ts/schema": "^0.1.3",
"@netlify/functions": "^1.3.0",
"@remix-run/netlify": "^1.12.0",
"@remix-run/node": "^1.12.0",
"@remix-run/react": "^1.12.0",
"concurrently": "^7.6.0",
"convex": "^0.9.1",
"cross-env": "^7.0.3",
"fp-ts": "^2.13.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@remix-run/dev": "^1.12.0",
"@remix-run/eslint-config": "^1.12.0",
"@remix-run/serve": "^1.12.0",
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.8",
"eslint": "^8.27.0",
"eslint-plugin-simple-import-sort": "^10.0.0",
"typescript": "^4.8.4"
},
"engines": {
"node": ">=14"
}
}
Barely edited from the Remix's Netlify template
ballingt
ballingt•3y ago
cool, bug noted, thanks! Still thinking about what should happen here. For Remix, I think that - npx convex dev/npm run dev should check for a .env file (and even offer to create one, at least if this is a new project), and offer to store the URL of the development Convex deployment in that .env file as CONVEX_URL - npx convex deploy should print out the deployment URL, but not save it anywhere
ballingt
ballingt•3y ago
I see that Remix doesn't want you to commit the .env file, so that should definitely be where the dev key goes
No description
RJ
RJOP•3y ago
For production there is no standardized approach, it depends on where you're hosting the Remix app. Yeah, I think that sounds right to me
ballingt
ballingt•3y ago
Here's what adding Convex to a Remix project will look like, lmk if any feedback
No description
Dima Utkin
Dima Utkin•3y ago
@RJ the interesting part for me it how do you use Convex in Remix's loaders? Are you building an http enpoint for everything? or you're not using loaders at all?
RJ
RJOP•3y ago
Looks good IMO! Another option for the prod scenario would be to echo the Remix guide and tell them to store the prod URL according to their host, and/or to link them directly to the relevant section of the Remix docs (https://remix.run/docs/en/v1/guides/envvars#production) I’m just using the root loader to load the Convex URL, and then using the corresponding root component to insert the Convex React client, as outlined in https://discord.com/channels/1019350475847499849/1019350478817079338/1074870246965522442. I think you could use the Convex HTTP client with other loaders for specific routes if you wanted to do any SSR, and then you could have the React client take over the same query once the page has been served (as Tom described to me the other day), but I haven’t tried that yet.

Did you find this page helpful?