I’m new to Convex and have some
I’m new to Convex and have some questions about the larger picture of an application that uses Next.js.
Convex describes itself as the missing backend for your frontend. But, given a react frontend, there still seem to be some advantages of using Next.js (eg for SSR or SSG for SEO) that Convex and react alone cannot provide. Am I thinking about that correctly?
But, there is some overlap in concern between Next.js and Convex. For example, I could deploy actions or HTTP APIs to either the Next.JS side or the Convex side.
Which raises an interesting question about auth: which side should be primarily responsible for enforcing it? I tend to think the Next.js side should use and know about the Convex side, but not vice versa. So, that leads me to think that authentication should primarily be the responsibility of the Next.js side.
That said, I do have a strong preference of storing authentication data and user profile data in the Convex database. I have worked in systems before that stored user profile data in an external service like auth0 or clerk, and I dislike it because I cannot include any of that data in a transaction boundary.
What’s unclear to me is how to think about custom authentication when both Next.JS and Convex are in the picture. I would appreciate any general guidance (or correction) to my thinking. Thanks!
2 Replies
Welcome!
You are correct that Convex doesn't handle rendering strategies for your app / site, and personally I think if I were to use Next.js + Convex, Next.js I'd have all of my auth in convex as close to the queries / mutations as possible, and only use it in Next.js for navigation / rendering purposes.
That said, I'm partial to SPA + Convex for the reduced complexity, but there are a lot of Meta Framework + Convex enjoyers so I might defer to them!
(I haven't used it myself but there's also Vite SSR)
Hey Adam,
I would recommend using Convex where convex is made for so have the http api and actions be at Convex side and only use the auth as its provided in the Convex docs. It doesn't mean there is a single perfect way to do thing. Convex isn't build to handle SSR but you can ofcourse still fetch data from Convex like you would do with any other backend service during SSR. It is good to leverage Next.js where Next.js excels and use convex where convex excels. Doesn't mean that if for your case it seems better to handle something in Next.js what else could also been done in convex you should go out of your way to do in Convex instead. I'm not a Next.js pro by any means since I use RRv7/Remix over Next.js but the principals remain the same.
If you have any further questions feel free to ask.
I've used RRv7 and Convex together and it works like a charm. So should Next.js I know tools like next are powerful and can do a lot. But just because it has those capabilities its still okay to not use all of them. Use how you like to work and which way makes most sense for your business. There isn't one correct way to build an app and most apps even though using the same tech stack might look totally different in the way they are used. I do a lot of client side logic since it makes a lot of sense to me but I see competitors in my field only using server sided logic for the same things, which end up creating extra loading states for them but create more state management for me. Creating software contains a ton of choices and most of the time the choice you make does not have to be a perfect one but rather and understandable one.