- Podcasts
- CodingCat.dev Podcast
- Code with CodingCat.dev: Supabase, Next.js and Builder.io
Code with CodingCat.dev: Supabase, Next.js and Builder.io
CodingCat.dev Podcast
- June 30, 2024
- 1 h 12 min
About this episodehttps://codingcat.dev/podcast/supabase-next-js-and-builder-io
https://codingcat.dev/podcast/supabase-next-js-and-builder-io
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Code with CodingCat.dev: Supabase, Next.js and Builder.io
Are you sure in Australia it's not Friday? Well, yeah, that's what I was thinking. I was trying to work out the time zone and then I was like. Hmm, am I saying this correctly? Yeah, so. Usually in the future in Australia, not you, yeah. Exactly. Yeah. So perfect segue, John, you are the developer advocates at Superbase. Is that the correct title?
Yes, one of the developer advocates. So we have we're we have growing very quickly now. We've got like four people, maybe five soon. Yeah. So one of the developer advocates at Superbase building fun examples and cool stuff. Look, branding changing again. No, I just, I love kind of messing with this while you're talking just to throw you off completely. So I am the the developer advocate currently at Builder IO and we are kind of going to do like this this mash up thing We're we're growing too, but I don't know if we have quite the Series B funding yet that you guys just announced, right. Yeah.
Australia, you got you got the right country. I mean, you could travel to. That's true. They could fly me up to Sydney. Nice. So so we're talking about today, we're kind of going to do this mash up where we're going to go through and create like a, a super base, kind of probably mostly a generic app using next JS. And then I'm going to plug in builder iOS React component and we're going to show how to like use our visual editor to like build out pages.
But John and I were talking before the thing I'm like, how do we how do we merge these two? And I think we have a really good idea. So I have a starter repo set up that we'll probably talk through like how we kind of got there. But then the cool part I think is going to be, John, when we do, we'll hook up like the authentication components that you guys already have. And we can kind of register those in builder and those can become part of the page.
So we'll kind of play around with this and and see where we end up. Yeah, yeah, I I haven't actually played with Builder IO yet. I've been excited about it for a while, so this is going to be the perfect introduction. Great, love it. All right, I'm going to attempt the screen share again. Hopefully it doesn't doesn't die off all over again. I'll try to go as big as possible. I know my 4K doesn't allow for for great screen share on here folks.
If if you're watching, I'll try to watch the comments as we go along here. If you want to join me, just DM me on Twitter and you can join us too. John, I was going to mention to you, did you want to code along with me? I I had set up VS Code live share. Live share Yeah. If you want, I can go over too. Let me let me kick that off.
And again, anyone that wants to join, just ping me and I'll shoot it over to you too. I have to remember how to do all of this. I have done it once. We we tried it out earlier on the discord. I was like, oh someone make sure this works. I think. I think. This is let's start going through it and I'll try and I'll try and sort out. Sent you the the link on stream yard, so just let me know if you can't get it.
All right, cool. So the first thing that that we kind of that I walked through prior to getting all this going was just the getting started on super bass. So for any of you that don't know what super bass is, I I just want to quickly kind of go through that. I know there's there's some super bass fans out here already that you know, so I don't want to spend a whole lot of time, but if you've ever experienced anything like Firebase or AWS, like amplify where it's like hosted, the goal of Superbase is to use all these open source tools kind of grouped together and make up like a firebase type of solution.
I guess here are the different projects that we have running. And I just created a new one for us. And you can see Superbase is hosted on AWS. So I'm in US E, which might be a little slower for John, but not a whole lot, I wouldn't think. And here you can see the the database and the auth part I have set up storage is is technically there. We're not doing a lot with it except for like storing a a user image at this point.
And then we'll talk about how the real time piece works too. And Oh, I don't even think I have to approve you. Cool. I have your account. Any super based project in the world I can just join? No, I'm joking. That would be that would be really cool. I will also add you to builder once we kind of get down that road a little bit too.
So to 1st show this off and John steer me if I'm like talking too fast or need to head a different way. The goal with this I, I quickly wrote this today and just threw it together. Basically want to show you like the authentication portion of this just to show that I am authenticated right now. What this allows you to do is kind of like, I'll call it an admin.
It's a pseudo admin that that's allowing basically, if you're logged in, you're allowed to click this add sports button or add animals button and that adds to the table in the back end. And when I'm logged out, so it's signed out, it says please log in. And that's that's kind of the long and short of this easy demo.
Oops. So yeah, I guess I can, you know, ramble about Oauth while you're while you're doing this. So yeah, Superbase comes with a collection of different Oauth strategies out-of-the-box. So by default it just has e-mail and password enabled. So you can, you can just add an input field for your e-mail and an input field for your password and register a new user. But it looks like what?
It's very hard to remember all those passwords. So Magiclink works very well in that you can just put in an e-mail and then it will send an e-mail to you to to verify that you're actually who you say you are. But then you don't need a password. Once you click the link in the e-mail, it takes you back to your Superbase app and then you are signed in.
We, a company I used to work for a bank had a an API, like a public API that people could use to interact with their banking data. And yeah, that was all done with just QR codes. So you, yeah, scan AQR code on the screen and it gives you an API key. Yeah. Any of those kinds of QR, Cody or Flow things are very cool. So let's let's take a look just kind of at this next GS set up as a whole.
I was just telling people that now that I've started this dev advocate role, my my typing skills have like degraded probably tenfold. It's it's, it's kind of nuts. That is interesting. Yeah. I feel like my typing speed has reduced dramatically but my like typing accuracy while not looking at the keys I think has has improved. That's true.
That's a really good point. My video editing skills maybe have improved. Wow, this thing is moving slow. Come on. Ah, OK, so let's just delete this for a second. Hopefully this is cool. Hey, I see you. Cool. It's me. I'm here. Let's try this again. OK, so here is like a traditional index page that you would start out like a beginner application with. The the key here that we're kind of talking about though, is how to like utilize some of the Super base back end with builder and the part that I want to like just go through right now without like confusing anyone.
And this is pretty traditional, right John? We're just pulling in the the JavaScript function for create client. And then we're just giving it a URL and a a non key. And so both of those values come in from our super base dashboard. Do you want to show where they are? Oh well, maybe. For sure it's no big deal.
I I can always burn this thing down if we leak any keys or whatever. So I was going to settings right? Yeah, so settings and then API and then that a non public value up the top is your a non key. And so this is safe to use in the front end once you have RLS enabled, which we don't don't have enabled in this in.
This not yet. Not yet, not yet, but yeah, so that the anonkey is what you would use to connect to your super based client and you can expose that in the front end using the next under score public under score prefix which makes it available to your next JS front end app. If you leave off next under score, public under score, it is only available in your server side stuff.
So that's like get server side props, get server, get get get, get gets and the and the API routes as well. Yeah. Awesome. Since we want to use this in our front end, which you know, if you're doing any kind of real time or anything like that, then you will want to be able to use this in your front end.
We're not doing anything fancy with static props or like anything like that. It's just just straight up React essentially. And what we have going on here is this state or this session that we're keeping in state. And what Superbase does is when we have this Superbase piece initialized, we have this off beast and it has this on off state changed handler. And at that point we can actually set our session state and pass that around wherever we want. So we take and when there isn't a session, we'll just display, please log in.
And when there is, we'll actually display the entire sports page at that point. And this is where it kind of starts to get a little more interesting in my mind at least. So let's ignore kind of this for a minute. Like let's just go like that and just pull it out of our brains completely. What what we have here is a simple button for AD sports. So let me bring that side up maybe. OK, so we have this ad sports button right here. And that's that's pretty simple.
Let's straightforward onclick. What we'll notice here that is that we have Superbase and then this is always interesting to me. This from right here, it's from off at first because I'm like oh are we getting data from this? Yeah, it doesn't read very well with the insert when you're when you're selecting something makes a little bit more sense if you scroll up to where you're where you've got your select.
So like you're saying like from this table, so from the sports table, I want to select these columns. And so in that case, you're just selecting all of the columns. So yeah, it reads a little bit more nicely with with a select on the insert, a little bit more clunky. So from that's from the sports table, you want to insert this value. It kind of works, but yeah, it's basically. Instead of doing like what it just said, super base that table or something.
Well, that's true, that would work. The first one is selecting, so obviously we're reading the values from the database and then this insert is writing data to our super base instance. Cool. And so when we click on add sports, we're just throwing a little faker image in there and some faker just names.
Don't use this in production. It will kill your site. It's huge. But for our purposes this works well. So we have that kind of set up. And then the other piece to this and kind of the important. So we are using a couple other things I guess next image call that out. So we get nice images just unload and things like that. The other big piece to call out in here though. So let's take a look at our sports table at this point. So we have this sports table and of course every time we click. So if I this is poor, let's see yeah, I gotta quit with all these tabs tonight, John.
All right, so with this sports page right here, if I if I start clicking that it actually we click refresh, we'll see the ID increment right here. And it's cool. So that's kind of how that table update is going through now, unlike Firebase, Firebase is kind of tracking that directly within their own local database. And then it's doing a sync and it's it's kind of creating that relationship.
What's the word I'm looking for? It's not lazily updating. My brain's shutting off tonight. At some point it will update. Eventually consistent database stuff. Exactly eventual consistency. So when we talk about Superbase in the back end, the table is actually Postgres. And so how does, how does this functionality work when we're actually inserting like this? Is the eventing system occurring through like a Lambda or like what's what's throwing that event back at us? Yeah, So Superbase has a few services all kind of grouped together.
And one of those is Postgresed with AT. So Postgres. Yeah, Postgres T as in capital REST at the end. Yeah, this is this is a better way to do this. So Postgresed is a REST API that can sit on top of your Postgres database. And so this is one of the the kind of awesome superpowers of of Superbase, I guess is that this Postgres layer kind of pipes through all of the API key like who the user is that's signed in information so that we can then use that in our in our Postgres policies or in our Postgres functions or anything like that. We can know who the user is that's currently trying to to perform this action.
And so when you're calling Superbase dot from and then a particular table and then calling SELECT or INSERT, that's making a query to or sending a a request to this Postgres server. So it's basically forming a REST style HTTP request for you, which hits this layer and then goes off to a Postgres database.
And so, yeah, really it's it's just making a POST HTTP request, I guess from the front end that's being intercepted by this and and then writing that data to the database a. Little bit more. Probably that architecture that you're used to where you have a server and a client and you're just sending Http://requests between the two.
Yeah, exactly. And the cool part in my mind, every time you set up a table like this in the back end, like you don't have to do any of that plumbing, right. So we automatically get all of these rest services out-of-the-box. John could probably breakdown RLS for us a little more. We're not quite ready for that, but we'll we'll get there. So that that's how we limit data kind of going back and forth. And if you've used Firebase, that's your security rules essentially on the back end.
Well, if you want to, if you want to jump into it, it could be, it could be a good time. While we're talking about data and we're talking about auth and all of those things. So row level security, what this does by default is just denies all actions. So all select, update, insert or delete actions, Superbase will just say, Nah, don't trust you don't know, don't know what you're doing or Postgres will say that, but you know. And so then if you want to access any of that data, you need to write a policy to be able to access it. So you need to enable each of those actions. So if we were to enable this on the sports table, for example, and then you go back and refresh your sports page. Oh sorry, this page.
Yeah, it's. Not gonna see it. Everything is broken, so all of the this action is is now denied by default. You're not allowed access to your own super bases. No, if you does that throw like a A500 or like 401. It'll just give you back an empty array because it's not that you've done anything wrong, it's just that. So the way that row level security works is it will check on per row that you're trying to select. Is there a policy that allows you to see this? And if not, then moves on to the next one. Is there a policy that allows you to see this? And so it's not actually an error, it's just that you don't have the authorization level to see any of the rows. So you just get back an empty array. So if we want to make it that you can actually select some of that data, we just need to create a policy and we'll say. So if you click get started quickly, there's a few templates that we have that you can use.
Sure. And so we can select which action we want to enable. So in this case we want to enable select and then we'll write a separate policy for insert. And so this one we could just say, well, we could just enable it for anyone. How about we do that? How about we enable or select for everyone? And then our insert policy can just be if you're signed in. So it's as simple as just writing in the expression part. This is just an expression that evaluates to either true or false.
So you can put any, any kind of condition in here that's going to give you a boolean result. And so since we want to just enable it, we can just write true, which is just going to enable all, all select access. So now it anyone can read this data. So now if you click review, we can see that the SQL that this generates actually isn't that scary. It's really just the fields that we just entered. And yeah, that's that's one thing that I think we try and do as much as possible at Superbase is try and surface what's actually the the SQL that's been generated and running behind the scenes because we give you full access to the Postgres instance, so.
And have you handled it nicely in the I? I haven't thrown any messages. There you go, and. Now this one should give us an error because what we're trying to do, we're not allowed to do. So with SELECT, it's it's checking every row to see whether we're allowed to see that specific row. But it it, it could be fine that we can't see any of those rows. But if we're trying to insert, we're explicitly not allowed to insert anything.
So we get this new row, row level violation. Something, something new row violates row level security policy for table sports. Anytime you see that error, it should trigger in your mind. Hang on, I probably need a policy for this. Anytime you see row level security, anything, then yeah, it's time to go write a policy.
So let's go to the Postgres. Sorry, Superbase instance. So the the auth side of this, John, like for the most part, we're passing JWT tokens around. So it tells us who it is. So on a REST call, it's typically like a auth header bearer token that you guys use and that gets passed in, passed in. Yeah.
So all of that plumbing happens for you. So because you're signing in through Superbase, in your account lives in Superbase, and we're the ones that have said, Yep, cool, you're signed in. All of that stuff gets plumbed through to any of these queries as well. So we can write RLS policies that can check who that user is, which is exactly what we're going to do for our inserts.
So let's do full customization again. And then this will just be that a user can insert an authenticated user. Let's let's just do authenticated because just like, yeah. OK. Yeah. And then insert is what we're writing a policy for. And then we can use a special helper that Superbase gives us, which is auth dot UID, which is a function.
So we do the curly brackets to call it. And so this is going to give us back the user ID for the user who's trying to make this request. So whoever is signed in. So then we can check whether that equals a particular column of the row that's trying to be selected. So if we say equals, well, space equals and then no, just single equals, we're in post grassland.
Right, I haven't, I haven't written sequel in like 6 years. So we want to check whether that auth dot UID that that ID that we get back is equal to Oh, actually, no, sorry, we don't want to do this. We don't want to use UID. We, I was, I was trying to think, hang on, why? Why are we checking their ID? What column we're checking it against? All we want to check is that they're actually signed in. So we can use another helper function which is auth dot roll and we just want to check whether that equals the text. So single quotes, I think authenticated and just click review and we'll see if we if everything's all good. So we'll do save policy.
I guess we'll find out. We'll find out very quickly. Let's click that add sports button. Hey, awesome. So, so the auth dot role, that's another helper function that we get from Superbase that just checks whether what their role is. And so if they don't have a role, then they're not signed in. If they have the role authenticated, then we know that Superbase has given them the thumbs up. And then, yeah, we could extend that and add our own role. Roles.
Yeah, cool. Let's let's talk a minute. So that's, that's kind of like how we're pushing data in how we can read a table. Now we have like roles set up to like secure our database the way it should be. There's this one tricky part with like the difference on Firebase. Here is how you actually see messages. So in in Firebase land, like we're just hitting that same table and as it updates, it just kind of filters down a synchronous what's the word?
I'm looking for RXJS observable type of situation, right? So we don't have to really do much on there. However, in in the Superbase world, we actually need to do this subscription side of things. So can you talk through kind of what like Superbase from and then this subscription does? Yeah, so one thing actually, we can show kind of the problem of, of what's going on here, 'cause if we if you click add sports again, we get a new sport.
And so this is adding a new sport. But if you open this up, I don't know if you can do this, but can you open up like a a private tab or something and go back to local host? Maybe you? Might need to create a a second account. You have another one. Oh cool. If you want to sign in as the other user, actually it shouldn't matter. You could sign in as either.
this is the problem, folks with too many profiles and emails and things. OK, I think we're good. So we now have. So if you go to sports, you wouldn't I Yeah, if you go, if you go to. The you know what triggy Parjan is with this magic link I got to do it in this Incognito windows. Yeah, and now you get an error because you've already used it in the other one.
Yes, see this is this is some of the fun. All right, my bad. And so, yeah, what we're just to explain why you're going through this, what we're trying to simulate is having, even though it is the same user signed in, just having 2-2 people using the app at the same time. Here I've got another one. I'll do this.
Make sure you copy and paste it. I know, I almost clicked it again. It's. Just it's just begging to be clicked. I mean, so inviting. So if you go to sports now and you should see all of the same sports 'cause they're they're all available for both users or that they've got different pictures, but. Yeah, ignore the faker images folks. Sorry about that.
Probably not an example. If you click add sports on this window, we'll see that the window. Oh no, it does. Have we already wired it up? Have you already got? Yes, yes. So I could I could turn that off. Let's comment that out for a second and save and we can just show. I don't know if you can hear that big truck that went past, but my office is shaken.
OK, So yeah, when you do the insert, you can get back the data that you're that you're inserting in and then spread it into the object. But that's not what we want to do because then that would only work for one user was what I was trying to trying to demonstrate is just like the one user who's signed in clicking add sports, it's going to update because they're the one that inserted it, but the other one doesn't know anything about that. So if we want to subscribe to changes that are happening in the database, rather than just needing to keep a track of that state in our, in our front end, then that's why we would use subscriptions.
And so Superbase has a real time server that just basically pumps out all of the any changes in the database through web sockets. And so we get to use magical web socket technology without needing to actually set that up at all. And so I'm going to actually set the subscription instead of setting the data, I'll just do a console log just so we can take a peek at like what it's actually sending back. It's a good call.
So now if you click the ad sports, So what this is doing is it's going and inserting into the database and then the subscription is listening to any changes on that table that are happening. Yeah, and I noticed you can do like a from like star, like any change over any table, you can check for updates. So you can like be listening to everything, which is pretty wild.
Yeah, well, I just, I just noticed you're. So what you're doing currently is you're, you're setting up a subscription and anytime. So you're you're setting a subscription on the sports table. Yeah, I've got a, I've got a cursor. Yeah, you're, you're setting a subscription on sports and you're saying you want to oh, sorry, up here, what am I doing?
So you're saying from this table you want to subscribe to any events. So this star means anything that changes that's inserts, updates, deletes. And then anytime something does change, this function is going to be called by Superbase JS. And So what you're doing here is you're going and selecting all of the sports again, like, which is identical to what you're doing here.
But in this payload, we actually already have the the data of the thing that that has happened, the event that has happened. So how about we comment out this section and we just console log is a great way to do it. Live coding, that's where it's at. I'm telling you, this is so cool, isn't it? Awesome.
And so now if we click add sports again and then I think our heads are in the way of being able to see it on the we'll. Get us out. Of the way for me. We'll come back OK. We'll miss this too much, folks. Good. So yeah, we've got our payload and then this is what's actually changed. So you'll see we have this new object and this old object. So this is the new data because we're subscribing to all events. If this was an update event, then you would have old would be set to the old record and you would be set to the new record. And so because we're inserting old is is blank because there was no record and new is set to this new inserted row. And you'll see we have all of the data from our from that row. So all of the columns of of that newly added data. So we don't actually need to make another call to Superbase to get all of the sports back, which is what we were doing on in this code that I've commented out because it's already in the payload.
So we can just do the, yeah, we'll need to manually do some magic here, but we can set the sports to being, we'll get the current sports. The value of the current sports is that's going to be that array of all of our sports. And then we just want to create a new array where we spread in the current and then our payload dot new is going to be that new object. Does that make sense?
Yeah, totally. I'm curious on that though that sorting might be. Yeah, we'll see. We'll see. So now if we click add support again, do it, does it come in at the top or at the end? I. Think the end. OK, so all we need to do is just reverse the position of these two. So if we take this and put this as the first value in our array and then we spread in the rest of the array and. Now you go we're.
Back to the same that same functionality. And so the venting mechanism, like if we were to leave this page how it is just for other folks to understand this, if you were on another page and you were, you were creating the sport, it's still going to pick up that same event, even though it's two different people causing the event.
So this this code still works. So it's like if you're adding to Twitter and you're getting that live page update coming in, same deal like boop, boop, boop. Yeah, cool. Yeah. So no matter who is is is causing that event to happen anytime every user that's signed in is now subscribing to or the the UI is subscribing to any changes that are happening in the in the the database.
The cool thing about this though a a fairly recent addition maybe like 3 months ago or so, but is that this now honours RLS as well. So these real time subscriptions now run through the same same kind of piping as any regular select statement. And so if we had an RLS policy that said that we weren't allowed to to read this particular. Like I can't.
I can't see John's updates. It would. Yeah, exactly. Yeah. If if we had an RLS policy that said I can like John can only select sports that he's created, then that the subscription will automatically only subscribe to or only get published events that John is actually allowed to see and so you wouldn't get my sports. That's that's exactly how the functionality works in Amplify too.
So when you're at that level for them inventing, that's really cool. They're they're doing those through app sync. Yeah, App sync, right? And so, yeah, that's the benefit of everything you just mentioned. And Superbase, like they all take care of oath for you and the data layer. And so because you know who that user is, we can do cool things in the data layer to know what that user should be able to see.
Awesome, love this. So now is kind of the the next phase of this that we'll get into Basically we want to start to like utilize what we have in Superbase now. So kind of think of this as an admin type of back end, if you will, like only logged in people. Even though we're kind of allowing everyone, typically we'd have like an actual role base for Adam admin.
But what we want to do for this homepage now, instead of doing that index, I'm going to go ahead and put back this path, restore that, pull out the index, see how bad this complaints. OK, so this page and this is probably new for John, it sounds like. So definitely ask me questions. This is this is helping me learn as we go as well. So this is going to set up our builder IO feature, I guess of our our page.
So what we have here is in builder IO. So if, if you haven't done this before, go up to builder IO, you can get, you can go to get started and you'll sign up and you have an account and then you create a space. And so for us, this is our our new space that we created. I've created a home page here and I'm gonna walk through that again, just so like everyone understands how this works.
I can actually pick any template out here that we want to and throw that in here. So I'm just gonna pick. I don't know what. All looks good, John. I like the yeah those animated purple people. These people love them. I like those. Very hip, very in. I think that's from Undraw so shout out to what's her name? I can never remember her name.
The undraw person. It's very cool. So at this point what we're actually doing now is previewing and the only way you can really notice at this point is we have this home user sports animals. So we're we're sitting on local host 3000 at this point. And if I do publish, like if I, if I don't do publish right, we're still getting a four O 4 and we won't get any page here. However, I can go into this draft view and it throws all these fun.
I don't know why this is happening by the way. I think I know why this happens because I think we are currently trying because I've now enabled RLS. I've broken your entire everything I think. This feels like it's a builder side of things though, which is. Maybe, but it might because you're oh what is oh, that API key is a builder API key. Yeah, and it should be expecting. I yeah, I don't know why, why that's coming through. So ignore that for now.
We'll we'll get there. So at this point, you can actually publish this page and then on this side, we should get our page. Cool so within within builder, you're also seeing all these boxes kind of light up right? So all of this is these are just different layers sitting inside of here. So if I want to remove make things great, boom, I removed it and now our page will update and this is just like whatever WordPress web flow, you name it, visual editing cool right. So this is not the aha moment yet like we, we always call it the aha moment what we want to look at in here and I'll, I'll just blank this entire page out now. So we just start with this ad block and that's that's the only thing we have.
And so John was telling me about kind of some of this these off components that we can get into. And I believe let's see here. Are any of these the pre canned ones in your examples? Probably not. Are we? Building an off page specifically Or should we just build a like pulling the the data back out and displaying it in builder? So that's where I was originally heading. I don't know if we might have broke that with the RLS piece. But let's no, no, we'll we'll just need to, we'll, we'll, we'll need to nudge it slightly, but it'll work. It'll.
Work. So the cool part, let's talk about that a minute on the next GS side. So I'll make this a little bigger. Hopefully we can kind of check this out for a minute. So let me go back into the path area here and talk through this just for a second. So on path, what we have here is our builder set up and our builder API key.
So we have the API key being registered here. And basically what this is asking for, and actually this might be what's broke breaking it 'cause I forgot I put that personalization stuff in already. What what we're basically saying here is go into builder and where the URL path matches slash in our case, 'cause there's nothing after it, go find that page and then provide that data back to us in this page parameter.
So get static props is happening on the server. It fetches that page on the server and then it provides that page data back to us and that page data comes in as a param. What's the word I'm looking for? It is late, John. I'm telling you parameter, I guess. Parameter. Yeah, I think prop, there's the word immortal in in React world. So that comes in as a prop.
And basically we're saying, OK, if there isn't a page, go ahead and throw an error which we saw the 404. If this page data is coming through, go ahead and put that into this builder component. And then we have our full content page that it creates on the server. So you're actually creating static pages based on the Jason data that we're pulling from Builder itself.
So that's kind of the cool, the cool part, right? Yeah, that's awesome. Now the the next part of this is we can pass any type of state based data as well. And I have a a cool diagram. Maybe I'll share it after this how this works. I'm gonna actually bring up Coding Cats really quick just to show everyone how to how we do this. So we actually moved everything over on Coding Cat not that long ago and I have the same kind of setup. It's a little probably different though. And the the reason I'm looking for this is because we pass all kinds of data in and I want to make sure I'm getting the right builder piece here.
I believe it's this model data. OK, yeah. So in here, the way that we can pass state is through this data prop. So anything we want as an object we can pass into that prop from data that we're pulling. So at this. And then that appears in the builder IOUI. Yeah, so this is this is going to break your brain a little. Bit.
Or at least you might get it. It broke my brain. It's already broken my what? It's already impressed my brain. Fair enough, fair enough. So I'm just going to drop that data in so I remember what it is. But basically at this point, and this is the part I kind of need help with on your side, when we're doing our get static props, I actually want to call out to Superbase to be able to pull data. And this is where you're like, let's just stick to client side. So I don't know if you're ready for this. No, I am ready for this.
So the reason that I was the, the reason that I thought it would, it would take a little while to set up is that all of our data fetching currently is happening client side with Superbase. And because we're signing in client side and all of that stuff, Superbase already knows all of that our client set up correctly.
When we go through to the server side, we need to explicitly tell the server who our user is if we want to be selecting things on behalf of the user. But because this is just building a static page, this data is just like, it doesn't matter who the user is that's signed in this, this, these pages are available to everyone. So they're not behind like an oath wall or anything like that. So in this case, we can use you remember before when we're in Superbase and I was pointing out the a non public key, we can actually use the service role key in Superbase to create our client, which yeah, you might you might want to trash this instant or re roll your keys after we've I'll probably finish the stream.
But yes, this service role secret key will bypass RLS and and basically just give access to everything. So if we go to your dot EMV dot local file, I'm guessing is where yeah. And then if we create a Superbase service key so we don't want the next under score public in front of it. And this is because we don't ever want to expose this value to the client.
So this this should be treated like your API keys. In most other services, the unknown key can can be exposed to the front end, but the service role key never should. And just for those who haven't used next JS before, when we and this, this can be really confusing if you're just getting started with it. When we are calling get static props. This is happening server side whether it's on. So there's get server props I believe and get. Server side props get static props and get initial props. I think will run on the server as well as anything in your API folder.
So any API route that you have in next chance? So all of those even though we're in a React page essentially and you see React down here client side, this stuff is actually happening on server. Just clarification. OK, cool. So how do? We, and it's also because we're using, get static props to. To further confuse this, this isn't just happening when the user loads the page, but this is happening when the when the server is building the application and. So or during ISR, so even further computers. Yeah, yeah, true.
So it is kind of happening when they refresh. But anyway, let's let's just say, let's not dive into that. This is happening when when we first create the page, which means we don't even have a user at that point. Like maybe we don't even have users sign into our application. So it doesn't make sense for us to use the currently signed in user because they don't exist at build time.
And so because this is like just happening on the server and is like a server function generating pages, that's the perfect use case for when we would use the service role key in Superbase and and sidestep RLS. So it means that we need to create a new client, though we can't use the helper that you that you used in in the front end, this one, because that's got that a non key baked into it. We could create a function where we could optionally pass it in. But yeah, let's just let's just create it from scratch in our we.
Cool. And now we want to pull in this super based server. And just just for clarification, John, there's not like a create server, it's always create client. It's just generic. Client, yeah, yeah. So it's a Superbase client, but whether we're using our a non key or our service role key determines what sort of access privileges we have when we actually make those requests to Superbase.
And then did you want to copy across your other sorting logic? Oh yeah, I guess it doesn't really matter, but. OK, Yeah. Sure, we'll just actually steal this whole thing, right? Yeah, that works. Cool, so then we're going to await this I would assume? Yep. And then get rid of the dot then and yes, and then sports. The the variable sports becomes data in curly brackets. Yeah.
Cool, can you do? I was phrasing. Can you do like? Yes. So you call that sports. Yep. And now you'll have a variable called sports. And so maybe, yeah, let's just pass it through. And we can pull it in down here where we're usually it's that. And then I guess we want to pipe that straight through as our data. Perfect, I think.
I think we're good. So let's so. My magic mixes nicely with your magic and gives us like ultra magic. Like ultra magic. I like it. Yeah, we're like going. To play super magic. And then I realized, you know. We're building super magic, right? I use the Super adjective too much. Like just prior to working at Super Mase, I always have to catch myself out. I I always say perfect, like perfect for everything now. So same deal.
We got it the the cool thing here. Now we'll take a look at this. I just wanted for clarification on the client side, we had the RLS pieces where we're now like checking and blocking things. Do we have anything when we're using that server key? It's just wide. Open, Yeah. So it completely bypasses RLS.
So it's it's exactly the same as if you hadn't enabled RLS for the. So be careful folks, is what we're saying. Like you can do anything with this table now. Yeah, which is why we're only doing it on the server side, because otherwise we would be giving clients an access token that they could use to do anything to our database. And so we're only doing this in that controlled environment of the server.
Awesome, OK now I hope this works. It's been a minute since I actually played around with the the builder side of this. There should be on this side now. What's happening? What did Alex mess up? I wonder if we're getting an error on the superbase side of things. 500 internal error Superbase client Yeah, I thought this might might be a problem.
So because we are importing Superbase in this, I think it's still trying to run that code on the client side. Where our? Oh actually did you restart the dev server Whenever we add a dot amb dot. Yeah. Value. There we go. Now let's see if that just magically works before I start explaining the thing that I was about to explain. We keep getting that, which I still don't get, but we'll figure that out at some point.
OK, so I think let me refresh the whole page just to make sure I'm not completely going crazy. So at this point, builder actually has functionality built into in this whole I frame craziness world that we're living in where we're not only viewing our local components that are sitting within this whole next GS side. But we did just send data up to it as a state object as well. And if we go over here, we should be able to see that state.
So here is our sports array showing up on the builder side. That is so cool. So here's where we get a little crazier, right? So like if you, if you as a developer say to your marketing team, I will pass you this API data, do whatever you want with it. This is this is kind of fun piece now, So I'll make this side a little bigger just so hopefully people can see it.
This gets a a little tricky 'cause there's a window within a window and it wants to capture a A+ key. So I gotta figure out how to do this. I think if I go up here, yeah, I was gonna say I figured this out the other day. OK. And it's so cool that all of this is working through like local hosts like. Yeah, it's very cool.
There's some scary things that people occasionally bump into that we have to kind of work through, but it's all documented out there somewhere, so you should be able to find it. But also just on that note, once you get this set up, like we could deploy this to Versailles right now and it would work the same way, you could then give it to marketing and say, OK, still go do your stuff 'cause they're good and they can preview on their own site. Yeah.
That's awesome. OK, so now we're gonna do something a little crazy wacky, and we'll see if this works. I'm gonna drop in just what's called a box. And for folks, this is just like a, a division basically. And it has some base kind of CSS properties built into it. And what I want to do on this is actually probably make, I'm going to just clear this whole thing for now.
And I'll, I like leaving this layers open. So we're going to call this a grid. And on my CSS properties, I'm just going to do display grid and then do a grid template, not column, row, row, column. Let's get this screwed up like one FR1R1 Fr. We'll see what that does. OK, And now I can add another new box inside of this grid. And if I just put text in it for this moment, so you can see. So there's my text.
If I were to duplicate this, I did do it the wrong way. I told you. Every time, John, it's grid grid template columns, not rows. OK, there we go. So now we've got these four things, but ideally we don't want them to do that. What we want to do is actually create this dynamically. So I'm going to delete those back out. I'm going to look at this box and say, OK, let's go over to data and let's do a data binding to this. And we are going to do get no, no, no, no, I'm doing this wrong.
Hang on repeat. So repeat for each in these sports, I assume. So I'm repeating all of the sports as if they're like at these boxes. Now what I can do with inside of those is say, OK, for this text right here, I'm going to bind from sports the text in here and sports item name. So now I've got all my names.
Great. Did you just say that moment? That is so cool. We you missed it. You weren't fully on screen. Hang on, let me make you pick. So this is this is the like, this is the crazy part. Like all of your server stuff's happening. You're passing state over, and then we get to manipulate state up in the builder side and do all this fun stuff with it, right? Marketing's gonna go wild.
Exactly. You're gonna have so many pages. You'll you'll. Never get bothered by marketing anymore like like we're good. You don't have to like bother us. Thank you we'll we'll talk to you later. So this is kind of this is cool because we're like pulling it through the API. But the other cool piece and this is are you are you good on time? I know.
So these also can be bind. You're not currently sharing if you think you're sharing. Thank you always. I always do that. So I threw some image boxes in there and I am also going to get this from the image. Cool now because I just screwed that up with my app to refresh ones I get image. I do not know why that's working. Not work.
We're just going to pretend that worked just just to move on with the next thing. That's right. Magic, super magic is happening in builder. OK, the next cool piece. So we've done this kind of just with generic like division based data on our API, which is fine. Like that's, that's pretty cool. The next thing I want to do is take and register a React component that Superbase wrote and use it in my UI editor. So it'll show up over here under custom components. OK, Are you ready for this one?
There we go all right so on this one, what we can actually do is use builder and we don't even have to do the dynamic side to this. We can because it like makes it more performant, but I'm gonna show you how to actually run this. So let's just grab one of these. I guess it's this whole thing so I should be able to jump back.
Yeah, so this is what it'll look like this primary button. OK, so let's drop that in here. So this is the use of it. All right, Just double check there. The button is kind of interesting a little bit because we're almost going to have to kind of wrap this because you're probably going to want like text that you provide into the button, right. So on this, let me jump back again. I have a couple of these where I I think I call them like wrapped for next GSI use like next GS link to drag around this right here.
OK. And now because of this button, how is button? Yeah, OK, All right. I'm like, what is happening? OK, So if you're following wrong folks, basically what I'm I'm going to do here is this is just we call this a higher order model. I still suck. I react this this is like. This would be a higher order component because it takes a.
No, it would just like a. Wrapper component, let's just call it that. I'm not good with all these React terms yet. So what we're going to do, instead of using dynamic, we then have this whole code block right here. What this takes is just the component. So we're going to call this the Super button and then we have all this other stuff.
So we'll call this the SUPA button. And then we actually don't need anything. But let's so input is just gonna be text. Actually, I think, I don't know why that's long text. I'm not familiar with our long text. So let's do that string default value just that John. Click the John button. That's right.
OK, so theoretically now, now I've registered a component in our next GS site and it's exposing that component up to builder and go over here. Yes, we probably need to install Superbase UI. Yeah, so you'll need to do an NPM install. So this is a completely separate package from superbase. This is just a a.
Maybe I'm wrong. OK, see Super button. We have a super. Button and actually we can get even fancier if you have a super image, but we'll we'll do that some other day. So instead of these little brackets over here you can have other stuff so actually on all of our different API people we can now drop in a super button.
There's our John button themed. What if we want to change that text though, Alex? How would you do that? I don't want all my buttons to just say John I. Love this. I love when John's just right there. OK, so the other cool part? Instead of this text being here, let's take and bind our button text instead. So we're going to grab that name from button text. But what?
Yes, another head explosion. How many heads? Do I have left? You are going to need ibuprofen. I don't know if they have that in Australia, but maybe like kangaroo dust. I I don't know what you guys think. It's night time where you are, but it's it's only like midday here. But I think I'm just going to need to go take a rest and just like close my eyes. This has been.
Just lie down, hang out for a while. So that's, that's like the most probably basic example, but that's how you can register a component and actually tie out any inputs that you could in a normal React situation. This is all just kind of mixing. Together, I recommend anyone who is watching that wants to take this a little bit further, try doing that with the auth component.
Because I, I kind of you know, in the, the fact that our time is getting a little bit short. Just wanted to go with something simple like a button. But if you did use the auth component, which basically just gives you a whole form that that talks to to superbase and that takes in like the superbase and non key and URL and all of that stuff.
That would be a really good extension to to try out. Yeah. So like basically if you're, if you're building something like with Superbase UI or a Tailwind UI or Tailwind's kind of interesting just because of the compiling piece to it, but I won't get there. If you're building things with a component library, so like a material UI or something like that, you can take and create and register all of these things and give them over to again, your marketing team, your like dev team in this case, if you're creating tools like this and you can go to town just creating different pages. And the cool part is we just hooked up Superbase AP is to it. And we could actually expose everything out that Superbase has too, because it's just, it's just a React component at the end of the day.
Yeah, well, that piping in between is just so cool. Yeah. It's not like it just takes over everything and you just use the UI for builder IO. Like being able to tie into that next JS app in the middle is just, yeah, very powerful. Yeah, and a big, a big part of the builder IO story too is I've mainly stuck kind of with the page example, but we do have like sections.
So like, let's say you have an existing app and you only want to like do a sidebar or something like that, you can just drop it into that area and that's all you have to work on. Like you don't have to rewrite your whole app and which CMSS, you can use three different CMSS. They're pulling data from other places.
So it's all it's all pretty cool. That's so cool, is. There anything else we missed? Oh, we did miss a big portion. I I should I should back up and we we named this one personalization at the edge and I knew like we might be a little tight. I'm getting to that, but if John promises he might have time for a a Part 2, I will promise I have time.
All right. If you will promise, I will promise if you will be there, I will be there. Because yeah, that would be very cool to see the personalization side of this and how and you said personalization at the edge, which makes me think this is maybe happening at the CDN layer and very, very quick for your users.
So if I go all the way down here, when we when we start to sorry, am I sharing? Yeah, I knew it. I knew it as soon as I started talking about and saw myself I was like, that's not right. Let's do this. So let me just go back up. This this video right here is from Steve, our Co founder. As you go down here, we talk kind of through some different parts to it, but the cool part here on the the middleware, we could do anything.
And now all of a sudden we know that by a UTM parameter or something like that super basically go grab all the data, pass it to next GS. And then that same way we use that state, we can then show you a page just based on you for the personalization. And that's the edge piece that we're talking about, that middleware.
Yeah, you've, you've intrigued me enough to come back. I'll, I'll come back for that for sure. I knew it would take like a little bit of of pushing, so I was really happy we finally got there. Yeah, Don, I really appreciate it. I always have a blast talking to you. I'm glad I I made your head explode.
