React Server Components from Scratch

Rate this content
Bookmark

React server components (RSCs) are a huge paradigm shift for React. You might even ask if NextJS and server components are the same thing (spoiler: they're not!) This talk demystifies how RSCs *really* work outside the framework. We'll build our own Node server, hook up the RSC renderer by hand, and understand the bundling and routing logic to ship a server component to your browser. Yes, all with live coding. What could go wrong...?

29 min
13 Nov, 2023

Video Summary and Transcription

This Talk introduces React Server Components and provides a step-by-step guide on building and rendering them. It explores the capabilities of server components, including interactivity and streaming. The Talk also covers the process of incorporating client-side rendering and the challenges of bundling and mapping client components. Additionally, it discusses the importance of supporting React Server Components and the ongoing efforts to integrate them with different bundlers.

Available in Español

1. Introduction to React Server Components

Short description:

I'm Ben. I'm a YouTuber and also a full-time person over at Astro. React Server Components from Scratch is what we're building. We're going to have an example repo and learning resources. Our goals are to build a server component from scratch, understand how to handle clients, and stream stuff over the wire and render it in the browser. Let's waste no time. I have an empty server file and I'm going to give us a simple server. Now we need the component. I'm gonna go over to my App folder and give us one called page.jsx. We're going to have a basic component called page with the message 'Hello React Summit, woo!' Now we need to bundle this in order to work in node. I'm going to run ES build, my favorite bundler. We're going to format it as an ES module and put it in a built build folder.

I'm Ben. I'm a YouTuber and also a full-time person over at Astro, which is a great framework for building websites. Yes, you read right. React Server Components from Scratch is what we're building. So if you've been wondering, how do you use server components outside of Next.js, it's not easy but we're going to do it.

So we're going to have an example repo that you can try out at the end of the talk, and also some learning resources. But our goals are to build a server component from scratch, understand how to handle clients, so we're going to actually do some bundling today. We're also going to figure out how you can stream stuff over the wire and render it in your browser as a Shiny server component.

So let's waste no time. I have an empty server file here. And to get us started, I'm going to give us a simple server. You could use Express or COA or whatever you want. I'm going to use Hano, because in my opinion, it's the easiest way to whip up a server on localhost 3000. So I'm just going to run node-server. Gives you the localhost address, and we can see Hello React Summit right there. Pretty cool. So we have a server.

Now we need the component. I'm gonna go over to my App folder and give us one here. I'm gonna call it page.jsx, mirroring the Next.js naming conventions, but you could call it whatever you want. And I'm going to do a very basic component here called page, and we're going to have a similar message, Hello React Summit, woo! So there we go. We have our page. And now we need to bundle this in order to work in node. I know we could use bun or dino or something that has fancy rendering, but we're going to own it ourselves. We're going to say async function build, and inside of here we're going to run ES build, which is my favorite bundler for this kind of thing. It's Go based. It's fast. And I also have a little bit of config here so I don't mess it up on stage. We've got bundle true. We're going to format it as an ES module, and we're going to take that page and put it in a built build folder.

2. Building and Rendering the Server Component

Short description:

We import the necessary dependencies and resolve file paths using a utility. Then, we build the server component and convert it into an element. Finally, we render the component to HTML and see the expected output.

And I also need to put a little import up top for ES build. There we go. And you'll notice I have some file resolvers here. I'm going to pull those in from this little utility. It's very simple. We're just taking our app folder and our build folder and resolving the path to them, because if there's one thing that will derail a live demo, it's node file paths, so we're not doing that by hand.

All right, and I'll import node URL right here, and now we should have everything we need to build a server component. I'm going to also await the build when we start our server, so we always start with something fresh. Restart, and we should see a build folder appear on the side right here with our page now compiled to some JSX functions instead of that JSX syntax. Cool, so now we're ready to actually plop this into our endpoint. I'm going to do a basic import here, so our page is going to be await import, and we'll go grab from the build folder directly, page.js, and then we'll take our component and turn it into an element with react.createElement, use page default export, and then we're going to maybe just return this as HTML because server components are just server rendering, right? Surely, so I'm just going to run render to string, component, and I'm going to import that up top, render to string, there we go, and now if we restart our server and render that out, we should get hello React Summit rendered right there.

3. Server Components and Streaming

Short description:

All right, that's the end of my talk. Server components can do more than just rendering HTML. They can handle interactivity and streaming. Let me show you an example with an albums component that fetches data from a database and renders a list of songs. React Server Dom is a core library provided by the React team. It allows us to create a stream and return it from our server. By using streaming, we can render content incrementally and show a loading state until the data is fetched. Let's restart our server and curl the endpoint to see the output.

All right, that's the end of my talk, thank you, server components from scratch. Thank you. Yes. Yes.

Okay, wait. Wait, actually. So there's a couple things that server components can do that aren't just rendering to HTML. First off, of course, client components. You can render HTML, but you want to have some interactivity with it. And also, what I think is the secret sauce of server components, which is streaming.

So let me show you a simple example of that right here. Say we have an albums component that fetches some albums from a database, literally just calling inside of a component and saying, oh wait, and then rendering it out to a list of songs and titles. And if you wanted to render that, you know, the stream and wait for it to resolve, you can even do react suspense, maybe put a fallback, say albums, and this would render the H1 straight away and show loading until that database call is done. Really cool system that you can do with server components, but you can't do with a flat HTML pancake. We need streaming.

So for that I'm going to reach for a core library in React. React server Dom. This is something that the React team provides. It's got specific versions for each bundler. Webpack is close enough to what we're doing, believe it or not, so we're going to grab from there. And we're going to create a stream by calling stream react server dom.render to readable stream. And to return that from our server, you can actually just do a web standard response, because if you don't know, streaming is part of the platform. If you return a stream, then the receiver knows what to do with it. So now we're going to restart our server. And this time, I'm actually going to curl it to see what output we get. So if I curl our end point, we should see some code coming down the wire. First, we saw that h1 come in, hello React Summit. Then our loading spinner with a little L2 reference here. That number two is referencing what comes down the wire later, which is going to be all of our albums. So it knows to hot swap loading with list of albums.

4. Rendering the Server Component and Client Logic

Short description:

Let me show that one more time. We can see heading comes down. Then the database call comes down. And all of your stuff appears. But obviously, this isn't HTML, is it? It's not like our old example. This is actually React Virtual DOM. If you didn't know, server components are kind of a full stack thing. We've got part one done, now we need part two, which is the client logic. Let's go ahead and create our client listener. And now I'm going to turn our home page into an HTML document that's going to load up that script I'm alluding to. And that's where we're going to render our server component. And we're going to use this client file in order to do it. So let's go ahead and write this file right here, underscore client. At the top, I'm going to import some resources. First, we need createRoot, which is the way in React to render something into a div.

Let me show that one more time. We can see heading comes down. Then the database call comes down. And all of your stuff appears. But obviously, this isn't HTML, is it? It's not like our old example. So if I pull it up in my browser, you can see it's kind of this wall of text? Browser doesn't really know what to do with this. That's because it's not pure HTML. This is actually React Virtual DOM. It's telling React, I want to render an h1. And then I want you to replace this loading spinner later. Because we have all that hot swapping with suspense, we need React in the client to receive everything coming from the server and turn it into Virtual DOM. I've actually heard this called Virtual DOM over the wire. If you didn't know, server components are kind of a full stack thing. The server sends all the puzzle pieces, and then the client pulls them together and assembles them into the website. We've got part one done, now we need part two, which is the client logic. So let me go ahead and delete this curl thing over here so we don't have to look at it. Let's go ahead and create our client listener. For that, I'm going to update this endpoint to be slash RSC, just to put it on a separate endpoint. We can call it whatever. And now I'm going to turn our home page into an HTML document that's going to load up that script I'm alluding to. So let's go ahead and create that. And we'll return some HTML inside of here. I'm going to do a very basic template. So we have DocType.html, a simple title, a script for some tailwind classes just to make it look nice, and then a div with an ID root. And that's where we're going to render our server component. And we're going to use this client file in order to do it. So let's go ahead and write this file right here, underscore client. So let me put that right here, underscore client.jsx. And at the top, I'm going to import some resources. First, we need createRoot, which is the way in React to render something into a div.

5. Server Components and Client-Side Rendering

Short description:

You'd probably be using it forever, whether you knew it or not. We also have the client side of the server DOM story. This is createFromFetch, a really amazing helper from React Core that takes in a fetch call to that server component we just made and turns it into virtual DOM for us. So in order to do that, first I'm going to make my root. We do need to update our server though. We're going to bundle the client file and expose our build folder on the network. Now we should have everything we need to at least see the client file up here. So we should see in our build folder, underscore client appears and all that beautifully bundled React goodness.

You'd probably be using it forever, whether you knew it or not. And we also have the client side of the server DOM story. This is createFromFetch, a really amazing helper from React Core that takes in a fetch call to that server component we just made and turns it into virtual DOM for us. So in order to do that, first I'm going to make my root. It is going to be a document getElementByID root. And we also need to call createRoot on that. There we go. And we do see a little TypeScript error. I'm just going to say be quiet. We know it could be undefined, but if it's undefined our whole app crashes. And we can't do much better than that. So let's say create from fetch and then we'll do fetch from that slash RSC. And then whenever a component streams down the wire, however many times it needs to resolve suspense render into that div the component, the updated stream. And that's all you have to do, believe it or not. We do need to update our server though. First off, in order to build this file, I'm going to go down to my ES build and I'm going to add another call to build the client file. This is going to have a slightly different set of settings. So I'm going to run it differently here. Client config, this is going to have the same bundler and turn it into ESM stuff we had before except now we're also going to bundle NPM packages. Obviously you don't have to bundle NPM in Node but in the browser, you need to bundle all your dependencies. So that's what we're going to do. And we're going to bundle the client file and at the top here, we're going to... Oh, what are we going to do? Oh, we also need to expose our build folder on the network because in HTML, you see we're going to slash build slash client and our server needs to respond with the file that we just built. So for that, I'm going to add app.use and then if you try to get anything out of the build folder, serve it as a static asset. And this is a really nice helper that Hano gives us called serve static and that's going to just build everything statically. So let me grab that import, serve static. And now we should have everything we need to at least see the client file up here. So we should see in our build folder, underscore client appears and all that beautifully bundled React goodness. It's about 50 or 60k to get started. So server components aren't 0js.

6. Server Component as Client Receiver

Short description:

The server component can also act as a client receiver, rendering elements and providing interactivity. We can add styles to make the setup look nicer, such as flexbox and tailwind. Additionally, we can incorporate interactivity, like a like button, using the use-client directive. This directive offloads the component separately to prevent accidental server rendering. To enable this functionality, we need to create bundler logic and a resolve-client-imports plugin.

You are being lied to if you believed that. It actually is a client receiver as well. So if we go up here, we can see now it is loading everything, and our album stream into view, but this time it's rendering elements. Look at that. So if I refresh this, you can see the loading appears and then the rest of it comes in. And I'm also going to add some styles, just to make that look a little bit nicer. I wrote them before to show, because you all don't have to watch me center a div. So up here we have flexbox, some tailwind basic stuff, and if I rerun that, we should see a nicer looking setup right here, called a Spodifant, which is fake Spotify. I don't know. And we see we have our post and all of our songs rendering out. Cool.

So we used React to do what PHP has done for two decades. What about interactivity? That's what React is meant for. So we're going to need to add in some interactivity. Let's do the basic example of a like button. So we'll make like.jsx, and for this I'm scaffolding out the basic counter that you've seen in every demo before. Keep track of likes, and down here we have set likes and display to the user how many likes we have incremented from there. Everything looks the same, except at the top here we have a magical use-client directive. You might have heard of this. This is the new flag in React to say when you hit a component that starts with this, offload it separately so we don't accidentally server-render it in the stream. So now we need to create some bundler logic to look for use-client. So let me go back here in order to do that. We're going to need a plugin. This is a feature in every major bundler. I'm also going to scaffold out a basic plugin for us. T-plugin, there we go. It's going to be called resolve-client-imports, and this is a basic check to say every time you try to import a .jsx file like, oh, well let me actually do that inside of my server component. Let's say every album is likable. We can see like.jsx is imported here, and our bundler will say anytime you get one of those fancy imports, oh, it looks like I also need to save something. There we go.

7. Reading and Tracking Client Files

Short description:

You should read the file contents and check if it starts with 'use client'. If it does, keep track of those client files and add them to the list for bundling. Tell ES Build to treat them as external and put them in the build folder for the browser to import.

You should read the file contents. So say contents, oh, I wish I could do dno.readfile. I have to use Node promises. So let me do a read file from there, and then we can grab the contents and say if the file contents starts with. I'm gonna do double quote, single quote, use client. Also check for double quotes by the way. I've been burned by that in the past, but I know we're using single quotes here. So if it starts with use client, first I want to keep track of all of those client files. So let me create a little map up here. This is gonna be the client entry points, and I'll make it a set. This is an array that removes duplicates basically. And in here I'm gonna say, hey we found a client component, add it to the list and we'll bundle it later. We'll also tell ES Build to kind of early return here, because we want to handle this ourselves. So I'll say, it's gonna be the relative path, but replace it with the built output path. So we're gonna build our JSX to JS in a moment. So tell ES Build you're gonna find it here. Treat it as external. Don't try to bundle it into our server bundle. And then we're going to put it in our build folder for the browser to import.

8. Building Client Components

Short description:

We stubbed out the client components and added them to the entry points. Using esmodule-lexer, we crawl the exports of each file and tag them as client references. This allows React to recognize client components and handle them accordingly.

Okay so now we've stubbed that out, we need to go build our client components. So let me go down here in our entry points. We're gonna add our client entry points to the list. And we're gonna turn off file rights, because now I want to own the files.

We're gonna do a little post processing on the file contents. So let me say, here are the output files. They come out of esbuild. And for each of them, let me do a little loop, async, file. For each file in the loop, we're going to first crawl all the exports from that file. Because in React you could export any number of components. We need to tag all of them as client references. Oh man, I wish I was using Deno, stop reminding me.

So at the top here, I'm gonna import this cool module called esmodule-lexer. This is a go-based compiler that's able to go through a file and parse out all the export names so we don't have to do all the crazy regex-ing ourselves. So I'll say, here are the exports. They come out of parsing the files' contents, the text contents, that is. And for each of the exports, for ext of exports, we're going to write to the file some extra tags on those exports. So I'm gonna make a mutatable variable here. New contents, we're gonna start it as the file contents. And we're gonna append some new information. So new contents, plus equals. And then down here we're gonna say, for each export name, we're gonna add a type of decorator. And this is gonna have a special value. Symbol for react client reference. This is something React's gonna look for as it's rendering all the components in the tree. If it ever finds a component function with the tag react client reference, it knows, oh, I hit a client component. Tell me what to do. So first we told it that it is a client component. Now we have to tell it, okay, when you hit a client component, don't try to render it. Instead, stub it out with an import and we'll import it from our server.

9. Tagging and Mapping Client Components

Short description:

Inside the browser, we add a special key to each file path plus export name to create a unique identifier. This is written to a global client component map, which is passed to the server. The client component map is used during rendering to generate import statements for the client components. The metadata for each component includes the server file path, name, empty chunks array, and async true for async import. Troubleshooting is ongoing.

Inside the browser. So that way we can run use state and make it interactive. So, how do we tag it? Well, we're gonna add a special key to each of these. And the key can be any unique identifier. But an identifier that's pretty easy to use. Just the file path plus the export name. Because that should be very unique. And we're gonna write this to a map that we're gonna pass up to our server.

I'm gonna make it a global. Don't use globals at home, but it's a demo. So we're gonna do that. We're gonna say client component map, empty object, and down here, ooh, I should probably make that singular. There we go. Down here I'm gonna pass in the component map. So as it's rendering the stream, it has this map of all of the client components that exist, so it can turn it into import statements. Down here I'm also gonna update our client component map at key. And it needs a bit of metadata. So first we're gonna tell it where to find it on the server, which is gonna be inside of slash build slash like dot js. And we need a little massaging in order to get that kind of path. So yeah, resolve it from the build folder, take the file path. Just know that this is gonna turn our like button into slash build slash like dot js. So we can import it from this build folder in your browser. The rest of it is pretty straight forward. The name is gonna be export name in the file. We're gonna have chunks as empty array, because that's a web pack feature. Turn that off. And we'll also do async true, which means do an async import. Oh no! Ah! Dang it, we were getting there. Okay. Wire? Yeah, let me check that.

10. Troubleshooting and Final Steps

Short description:

It looks like I've got it there. Hmmm, is it an overheating issue? Perhaps. Perhaps. Should I wait? I will await the board! That's all folks. Live share, onto your machine! Everyone join the live share! We've got our client map, right? We're keeping track of all our client components. We tagged the type. The last thing we need to do is tell it, whenever you find this export, go look up this ID on the map. Hopefully, this should be everything we need in order to render it. We see a couple more chunks pop up here. We have the like button, bundled up for the browser. And we see the like export was tagged with our client reference. So now, if we visit this in our browser, we'll see getting albums, and then an error. But this one is expected. So down here, at least I hope it's the one. So if we look here, we see webpack require is not defined.

It looks like I've got it there. Hmmm, is it an overheating issue? Perhaps. Perhaps. Should I wait? I will await the board! Hey! That's all folks. Yeah, don't stream out of here early. Y'all have good reactions, okay. Live share, onto your machine! Everyone join the live share! Oh god, do I do that? That feels like the least secure thing ever. But I could, I could. Okay. Well, we can wait it out. The problem is I can't keep talking about my stuff. Y'all need to see what I'm doing. Hey! All right! That's good, we're almost towards the end.

So, oh god, okay! We'll keep the pace, we'll keep the pace how we had it before. So, okay, we've got our client map, right? We're keeping track of all our client components. We tagged the type. The last thing we need to do is tell it, whenever you find this export, go look up this ID on the map. So we have our map right here, and we can see JsonStringify, the key, just put quotes around it basically. So that way, any time it crawls the component, it knows to look up this import in order to resolve it in your browser. So that should be everything we need. Last thing I have to do is just write the file to the file system, because we're owning that process now. And hopefully, fingers crossed, this should be everything we need in order to render it. So we see a couple more chunks pop up here. We have the like button, bundled up for the browser. And we see the like export was tagged with our client reference. Very cool! So now, if we visit this in our browser. We'll see getting albums, and then an error. But this one is expected. So down here, at least I hope it's the one. So if we look here, we see webpack require is not defined.

11. Webpack Hack and Album Rendering

Short description:

To make webpack work with browser standards, we can add a webpack hack to the client file. This allows us to import the server-built file instead of using webpack require. After restarting the server, the albums appear with clicky buttons.

That's because webpack does not use browser standards, unsurprisingly. So if we jump over to our client file, we can add this little thing. I just wrote it down, webpack hack. This is saying anytime it tries to go to the window and do a webpack require, just use the platform and say, do an import to my server. I have the file built for you. And the ID is gonna be slash build slash like.js. So there we go. Now, if we restart our server, and head over here, we should see getting albums. And then our albums appear with clicky buttons. Look at that. Yes. All right. So it's actually the first run through where nothing went wrong except the projector.

12. Expanding the Demo and Additional Resources

Short description:

There's a lot more you can do from here, like setting up a file router or making a query parameter with a search box to fetch a new list of albums. I discussed these topics in a guest stream with Dan Abramov, which you can find in the readme on the repo I'll share. The video is long, but it's divided into chapters for easy navigation.

Okay. So that's everything that I wanted to show you for the base demo, but of course there's a lot more that you can do from here. For example, you might have been thinking, well that's just one route. How would I set up a file router? And also how would I request this component again? Say I have a search box and I want to like make a query parameter with the search and fetch a new list of albums from the server. I actually went through all of those things on a guest stream with Dan Abramov a while back. So if you want to go check out this resource, it'll be in the readme on a repo that I'll share out at the end. It's a long video. Yes, it's four hours, but there are a lot of chapters in here to jump to your favorite part. So if you want to just say learn how to set up a search box or learn how to set up a router Or you just want to hear Dan Abramov and I chat about GraphQL. All of that is available here and you can thumb through to find a resource.

QnA

Conclusion and Q&A

Short description:

Otherwise I have one minute videos, which you know you could watch. You can find all of those on my YouTube channel and also on Twitter. I'm at bholmesdev on all the major platforms. So if you found this useful at all, go follow me there and I share out a lot of resources on React and also Astro. Thank you for your time. Is it worth using RSEs outside of Next.js? I would say it's too early probably to use them outside of Next.js. I work on a framework called Astro, which is at Next.js. Any notion on when that might actually happen? I don't know. I'm trying to get like a plug out, you know, or something... Look in the next five fiscal quarters. Maybe, I don't know.

Otherwise I have one minute videos, which you know you could watch. How many is that? Sixty times four shorts in the time of that video. You can find all of those on my YouTube channel and also on Twitter. I'm at bholmesdev on all the major platforms. So if you found this useful at all, go follow me there and I share out a lot of resources on React and also Astro.

Okay. That's everything that I wanted to show you all. Thank you for your time. Give it up guys, come on.

Let's go with this one. Is it worthy using RSEs outside of Next.js? Is it worthy? Worthy... Okay. Are we worthy? Is it worth using them outside? Is it worth using them? Yeah. I would say it's too early probably to use them outside of Next.js. The only reason that I went through all the hours to figure that out, I work on a framework that is at Next.js called Astro. So I was very curious how can we get this working inside of V popular bundle... Or, say, Astro. Or Astro. Or, yeah. Yeah. Any notion on when that might actually happen? I don't know. Okay. All right. Yeah. I'm trying to get like a plug out, you know, or something... Exactly. You know? Yeah. Look in the next five fiscal quarters. Maybe, I don't know. I don't know how you measure things, professionally.

Integrating React Server Components with VT

Short description:

We're working on putting React Server Components into VT, which is a bundler for plain React apps. It's an ongoing effort with a whole core team involved. However, server actions present a difficult problem that would require significant changes. Currently, it's only supported by webpack.

Some amount of, you know... Some amount of time. It will pass. Right. We're built on VT, though. So, it's an ongoing effort to put it into VT, which is a bundler for just plain React apps. Astro, Sveltekit and Nuxt are also built on VT. So, it's a whole core team of people that are all interested in trying to figure it out. And VT uses the bundler that I was showing off. So, there's a lot that we could do in order to figure it out. But server actions sort of wrench in things, it's a very difficult problem. That would take some big changes. So for now it's a webpack only sort of thing.

Importance of Supporting React Server Components

Short description:

React server components should be supported by anyone using React or major frameworks. It will shape the ecosystem, and many libraries are transitioning to use server components. Even if your framework doesn't have users writing server components day to day, it will be needed in the next one or two years. Library authors should try to get server components working and collaborate using a shared bundler like VEET.

Well, I got to say, I guess the number one voted up question is a line from Jurassic Park. Have you ever wondered whether our scientists were so preoccupied with whether they could, they never stopped asking if they should. But that's an interesting question. In the context here, now that you know so much about React server components. Are you like, uh, that seems like a good idea or, uh, I don't know. I've stared in the belly of the beast. I was scared of what I saw. It did this, like, stare back? Yes. Yeah, with server and client errors. Oh yeah. Yeah. Um, so I feel like it's definitely something that should be supported by anyone that is using React or the major frameworks because it's going to shape the ecosystem from here. Because a lot of libraries are already doing the transition to use server components and put that use client decorator so that if you are in that context, it still works as you would expect. So I think even if your framework doesn't have users writing server components day to day just to support major React libraries, it's going to be needed in the next maybe like one or two years. I'm definitely curious feelings on the server components panel when we get to that. Absolutely. But, yeah, it feels like we're early days. We got the patterns. We have the primitives. People are trying them in production. Now the ecosystem needs to move that way and that will take more time. I thought I saw a question, I don't see it here, but about what library authors should do with RSCs and any thoughts on that? Yeah. I mean it really depends what your library is for because I know that there are libraries like Redwood and Remix that have all been I think trying it out in very, very early stage ways to see does this make sense with the model we have right now because people have happily done server rendered React apps for a while and you don't need server components to build effective apps. They're just this enhancement to do them a bit differently. Maybe do them server side only for things that need it. So I think it is something every framework author should look at, try to get working, and hopefully we can work together using a shared bundler like VEET, so that if one person gets really curious and builds it, we can all benefit and all the framers can try it out. Lots of love for VEET. Give it up for VEET. Yeah, VEET. Come on.

Understanding Server Component APIs

Short description:

Our server component APIs are not yet documented. The process for figuring them out involves diving into the source code and reaching out to experts like Dan Abramoff. Framework authors are receptive to genuine questions and efforts to understand server components. Asking questions and exploring the source code should lead to better documentation and a deeper understanding of server components.

VEET. All right. I love this particular question. So our server component APIs are not yet documented. What was your process for figuring this out? Source code. Yeah, right. I mean, that's really important. As a developer though, it's a good skill to learn to get into it, figure it out. Yeah. And also casually DMing Dan Abramoff. Yeah. Well, OK. That was very helpful. More helpful than you would think. Well actually you would think that, I'm sure. But I think framework authors would be interested. I mean, if you have genuine real questions for folks that are meaningful and like, hey, I did the effort here. Can you help me out? Yeah, they're up for it. Yeah, the meta team is really, really receptive right now. Actually got another message from Matt on the meta team that is trying to work on education resources. How do you understand server components at the root level? So I think at the very least, asking all these questions should lead to better docs that explain what's going on under the hood. That's what I'm hoping for. And also, yeah, I mean, diving into source code is not... Well, okay, it is pretty scary, but it's not as scary as you might think. Like reading a good mystery novel. Yeah, and there are some resources already. Like Core Team has a React flight demo. Flight is like their internal name for server components from years back. There's repos in there that I went through to understand how the pieces fit together. It's how I found the readable stream library and create from fetch, which is the most magical function, it does so much for you.

Resources and Micro Frontends

Short description:

So there's resources. They're out there. You don't have to go totally on your own. Can we do the same thing for micro frontends? How do RSCs fit into the micro frontends or islands model? Islands are a way to put a client-side component inside a server-rendered template. Server components are like islands architecture. Server components are a new way to do islands. Do you foresee any security issues with server components? SQL injection always comes up. We've got tools for that.

So there's resources. They're out there. You don't have to go totally on your own.

Alright, how about, can we do the same thing for micro frontends? Or I guess, how do RSCs fit into the micro frontends or islands model? Right, yeah, islands, TM. That's something that... Astro only feature. We invented islands. No, it was actually a blog post from Preact Team, I think, that talked about islands a while ago. Okay, yeah. I'm just gonna dump their screenshot explaining islands and put it in our docs. This exists. It's fine.

Yeah. Brief primer on what islands are, it's a way to put a client-side component inside of a server-rendered template. So you have some HTML, it's rendered server-side. And then, at this little div right here, it's like, I want an image carousel here and the rest of it's just HTML. And that's the point where a bundler can say, oh, that's an island. I'll use Preact to load a component into that div. it or choose when to load it, all of that stuff. So server components are a bit tougher, because server components are kind of like islands architecture, because when you saw the use-client directive, you are creating this little client island where it's like the Like button is gonna load JavaScript, but the rest of it doesn't. That's really the basis of what an island is. So it feels like server components are a new way to do islands. And you could have a server component island inside of HTML that also renders more stuff. You could go crazy, but it is just another way to do it, compared to what Astra is doing.

Do you foresee any security issues with server components? SQL injection always comes up. I don't know why. We've got tools for that. Right, in that Versal demo that they showed, which wasn't – Classic. – SQL injection. I remind myself of it.

Security Challenges and Safeguards

Short description:

Security for server actions is a tough challenge. Bundling server actions can lead to accidentally exposing sensitive information to the client. Safeguards have been implemented, such as restricting server actions within the use client directive. Libraries like the taint reference provide additional protection by marking potentially unsafe code. Safety features like these are valuable in preventing unintended exposure of sensitive data.

It was. Yeah, I think that – No, it wasn't. It wasn't? Oh, it wasn't. No, because they handle that – Right, right, right, they use the template thing. Right, yeah, they use the – If you don't know what we're talking about, don't worry, it's not a thing. But yeah, I think security for server actions is a really tough one.

We didn't get to demo that because bundling server actions is a whole other can of worms, but the weird part about them is you can write a server function, and you can write it kind of inside the component. So you could write a handler that says, write to the database, and you can pass it to a form, and you could have a closure there where you wrote your backend inside of a component, so if you had these variables that are being passed into that function, you could mistakenly bundle those variables to the client, and there's now a lot more safeguards around that. Like, if you have use client, you can't put a server action in there, you have to put it a little higher up, and there's a lot of funny libraries, like the taint reference where you can – Yeah, I know how to taint it. Like, yeah, you can mark something as like, if this ever gets into the client, explode the build, which is – it's a nice, honestly – Safety feature. Safety feature, I would have used it regularly. Yeah.

All right, big hand for Ben, thank you so much.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

React Advanced Conference 2023React Advanced Conference 2023
27 min
Simplifying Server Components
Top Content
Server Components are arguably the biggest change to React since its initial release but many of us in the community have struggled to get a handle on them. In this talk we'll try to break down the different moving parts so that you have a good understanding of what's going on under the hood, and explore the line between React and the frameworks that are built upon it.
React Advanced Conference 2022React Advanced Conference 2022
25 min
A Guide to React Rendering Behavior
Top Content
React is a library for "rendering" UI from components, but many users find themselves confused about how React rendering actually works. What do terms like "rendering", "reconciliation", "Fibers", and "committing" actually mean? When do renders happen? How does Context affect rendering, and how do libraries like Redux cause updates? In this talk, we'll clear up the confusion and provide a solid foundation for understanding when, why, and how React renders. We'll look at: - What "rendering" actually is - How React queues renders and the standard rendering behavior - How keys and component types are used in rendering - Techniques for optimizing render performance - How context usage affects rendering behavior| - How external libraries tie into React rendering
React Summit Remote Edition 2021React Summit Remote Edition 2021
33 min
Building Better Websites with Remix
Top Content
Remix is a new web framework from the creators of React Router that helps you build better, faster websites through a solid understanding of web fundamentals. Remix takes care of the heavy lifting like server rendering, code splitting, prefetching, and navigation and leaves you with the fun part: building something awesome!
React Advanced Conference 2023React Advanced Conference 2023
33 min
React Compiler - Understanding Idiomatic React (React Forget)
Top Content
React provides a contract to developers- uphold certain rules, and React can efficiently and correctly update the UI. In this talk we'll explore these rules in depth, understanding the reasoning behind them and how they unlock new directions such as automatic memoization. 
React Advanced Conference 2022React Advanced Conference 2022
30 min
Using useEffect Effectively
Top Content
Can useEffect affect your codebase negatively? From fetching data to fighting with imperative APIs, side effects are one of the biggest sources of frustration in web app development. And let’s be honest, putting everything in useEffect hooks doesn’t help much. In this talk, we'll demystify the useEffect hook and get a better understanding of when (and when not) to use it, as well as discover how declarative effects can make effect management more maintainable in even the most complex React apps.
React Summit 2022React Summit 2022
20 min
Routing in React 18 and Beyond
Top Content
Concurrent React and Server Components are changing the way we think about routing, rendering, and fetching in web applications. Next.js recently shared part of its vision to help developers adopt these new React features and take advantage of the benefits they unlock.In this talk, we’ll explore the past, present and future of routing in front-end applications and discuss how new features in React and Next.js can help us architect more performant and feature-rich applications.

Workshops on related topic

React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Top Content
Featured WorkshopFree
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
React Advanced Conference 2021React Advanced Conference 2021
132 min
Concurrent Rendering Adventures in React 18
Top Content
Featured WorkshopFree
With the release of React 18 we finally get the long awaited concurrent rendering. But how is that going to affect your application? What are the benefits of concurrent rendering in React? What do you need to do to switch to concurrent rendering when you upgrade to React 18? And what if you don’t want or can’t use concurrent rendering yet?

There are some behavior changes you need to be aware of! In this workshop we will cover all of those subjects and more.

Join me with your laptop in this interactive workshop. You will see how easy it is to switch to concurrent rendering in your React application. You will learn all about concurrent rendering, SuspenseList, the startTransition API and more.
React Summit Remote Edition 2021React Summit Remote Edition 2021
177 min
React Hooks Tips Only the Pros Know
Top Content
Featured Workshop
The addition of the hooks API to React was quite a major change. Before hooks most components had to be class based. Now, with hooks, these are often much simpler functional components. Hooks can be really simple to use. Almost deceptively simple. Because there are still plenty of ways you can mess up with hooks. And it often turns out there are many ways where you can improve your components a better understanding of how each React hook can be used.You will learn all about the pros and cons of the various hooks. You will learn when to use useState() versus useReducer(). We will look at using useContext() efficiently. You will see when to use useLayoutEffect() and when useEffect() is better.
React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
React Advanced Conference 2021React Advanced Conference 2021
145 min
Web3 Workshop - Building Your First Dapp
Top Content
Featured WorkshopFree
In this workshop, you'll learn how to build your first full stack dapp on the Ethereum blockchain, reading and writing data to the network, and connecting a front end application to the contract you've deployed. By the end of the workshop, you'll understand how to set up a full stack development environment, run a local node, and interact with any smart contract using React, HardHat, and Ethers.js.
React Summit 2023React Summit 2023
151 min
Designing Effective Tests With React Testing Library
Top Content
Featured Workshop
React Testing Library is a great framework for React component tests because there are a lot of questions it answers for you, so you don’t need to worry about those questions. But that doesn’t mean testing is easy. There are still a lot of questions you have to figure out for yourself: How many component tests should you write vs end-to-end tests or lower-level unit tests? How can you test a certain line of code that is tricky to test? And what in the world are you supposed to do about that persistent act() warning?
In this three-hour workshop we’ll introduce React Testing Library along with a mental model for how to think about designing your component tests. This mental model will help you see how to test each bit of logic, whether or not to mock dependencies, and will help improve the design of your components. You’ll walk away with the tools, techniques, and principles you need to implement low-cost, high-value component tests.
Table of contents- The different kinds of React application tests, and where component tests fit in- A mental model for thinking about the inputs and outputs of the components you test- Options for selecting DOM elements to verify and interact with them- The value of mocks and why they shouldn’t be avoided- The challenges with asynchrony in RTL tests and how to handle them
Prerequisites- Familiarity with building applications with React- Basic experience writing automated tests with Jest or another unit testing framework- You do not need any experience with React Testing Library- Machine setup: Node LTS, Yarn