Hi, my name is Julian and I am super excited to be speaking at React Summit this year. Now, 20 minutes isn't a lot of time, so let's get right into it because I want to talk about Suspense. I'm a huge fan of Suspense, basically ever since they started demoing early versions of it in 2016, so quite a while.
But for those of you who don't know, Suspense basically allows you to render boundaries within your application to then render fallback UIs like loading states whenever anything underneath those boundaries is still waiting for promises to resolve. So those could be API calls, but it could also be lazy little JavaScript chunks or any other asynchronous tasks that you're doing in your application. Now, that's pretty cool. But I think Suspense on the server is even more impressive, and I would even go as far as saying it's the unsung hero of React server components because without it, server components the way we know them wouldn't really be feasible. And today, I hope I can show why I think that is and also demystify a few things that are happening underneath the hood.
Now, before that, I want to take a quick look at how we even got to server components in the first place, how Suspense ties into all that just to get everyone on the same page. To do that, we have to take a quick look at the history of web rendering at the last 15 to 20 years. And now, this will make me sound like an old person, but I hope a lot of us still remember the days when we were writing plain HTML and then eventually dynamically creating it on the server with languages like PHP. Now, that came with a drawback, especially if we had to do a lot of work on the server to get that HTML, that initial server response time could be really slow. And that's obviously bad for user experience. So when we started introducing JavaScript to our apps to make them more dynamic, we also introduced concepts like AJAX.
So AJAX allows us to even after the initial server response is finished, to still go back to the server and do stuff. So that means we can offload a lot of that heavy lifting, still have a quick server response, initial server response, and render some kind of loading state while we're going back to the server and fetching more data or whatever we need to do. Right? And that was so convenient that we started doing it more and more until we eventually arrived at what we now know as single page applications. And with it came all the frameworks that we know and love like React, like Vue, like Angular. And to get the best of both worlds, those frameworks very quickly reintroduced the concepts of server-side rendering and static site generation. But we did carry over some of the problems that we had, namely, the first one being the JavaScript bundle size. So ever since single page applications, JavaScript bundles just kept increasing and increasing. And that's not great. And it also caused somewhat like the second problem, which is hydration. So even if your server side render your app, React still needs to send all of the JavaScript to the client and needs to run all of the JavaScript to then hydrate your whole application, just in case some parts of it need client side logic, be it state, be it effects, or event listeners and that kind of stuff. Now, hydration can be really slow. So in fact, that started becoming one of the performance bottlenecks that we're seeing in modern web applications. The third problem is we're back on the server now. So we have the original problem again, where if we're doing a lot of work on the server, that initial response time can get really sluggish. So introduce server components. The main point of server components is for us developers to be able to tell the bundler which components in our application are static, only ever need to be run at once and which components are dynamic and actually need to be run in our client and also need to be hydrated.
Comments