There's a really good talk by Rich Harris called, Single Page Application Ruined the Web, and generally there's a really good talk in the Twitter sphere, or in the web, about Single Page Application versus Multipage Application, where some people say that multipage applications fall short because they don't give the experience that we want, and other people say that single-page applications, because they load really slow, don't give the performance that we want. That's an ongoing debate.
But in order to understand a little bit what we are talking about, we need to understand what we are talking about when we talk about rendering. So, rendering 101, we talk about who builds the markup. It can be either the client, the server, or the edge in between. How does it become interactive? Is it the client that makes it interactive? Or is it the JavaScript code that makes it interactive? Is it the native browser functions? Or is it the React that makes it interactive? When is it being built? Is it being built in build time or in runtime? And, of course, where is it served from? Is it being served from some sort of an edge network, or a cache, or the server itself?
So, if we think about a simple blog, for example, which is very static, then we know that it is being built in build time, it is being interactive. We're using JavaScript code. The server is building it, and it's being served from the cache, right? Because you don't need to rebuild it in runtime. But if you have a complex web app, that's something that the client is building, probably using React or some other frameworks, and it requires server. It requires a runtime server in order to operate. So, if we think about those two parts, those two edges of you have server-side rendering for very static, simple pages, on the one hand, and you have client-side rendering for very complex web apps, on the other hand, you can put them on a spectrum, because those are not the only two options that we have. So, in static rendering, which is the server-side rendering in build time, you're getting the same page for everyone, and it can be easily cached, but it cannot be personalized. And you cannot have variants, because you need to build everything in build time, and it requires a build for every update, whereas in the client-side app, which is the most dynamic, it can be easily personalized because you do it on demand in the request, but it cannot be cached, and it has slow first loads and slow data fetches, because you have to go to the server for everything, but it has fast in-app responsiveness. But we need to remember, this is a spectrum, so, if I want to build a listing page, and I want for every page in the listing to be different, because I have 10,000 properties that I want to list, then I can use something that's called incremental static regeneration, which is not just generating everything on build time, but generating some of them in build time, and some on demand. If I want to build something that is more personal, like my bookings, or my account, or things that need my data, need my cookies in order to present, then I would have to go with real-time server-side rendering, because I can't cache anything, and, on the other hand, if I want to build a web app, but most of the parts of the web app are static, and only parts are dynamic, I would go with React level components. We will talk about all these strategies.
So, when we talk about SSL strategies, or the multiverse of madness, we need to remember that when we speak about static rendering, we no longer talk about only the client and the server. We need to put something in between, which is the edge network, or the cache, and then we can discuss about the relations between the client, the edge, and the server. In classic static rendering, which is like we saw, the blog pages, the server generates the page on build time, and it sends the page to the cache, to the edge network, and then it goes out of the picture. We don't need the server anymore. It just generated the page, and that's it. The client asks the cache or the edge for the page, and it gets the page, it gets the blog post, and it's important to understand. It's important to notice that all of the clients will get the same page because they all ask it from the cache. Like we said, the pages are easily cached, and they do not require a server, and they have great performance, but they do need rebuild for every update, and there's a problem with data fetching.
In incremental static rendering, the server builds the page, sends it to the cache to be cached, but it also builds a lot of variations, a lot of other variations in the cache, and when the client asks the cache for a page, they get the page, and another client can ask for another page, and they will get another page, but if a third client asks for a page that does not exist on the cache or in the edge network, then it goes directly to the server. The server generates it in real time and sends it to the cache, caches it, and then the client talks to the cache and gets this page. It is like the best of both worlds, but you do still need a server. It can also be triggered by the cache. You can do an invalidation time. For example, if you have a page that needs to be revalidated or refreshed every couple of minutes, then when the client asks the cache for the page, the cache can say this is the page that you want, and the next client, the cache can say, hey, the page is stale, asking the server to regenerate it, while at the same time serving the stale page to the client, and then the server regenerates a new page, replaces it in the cache, and then the next client will get the updated page, which is pretty nice.
Comments