We have only server-side rendered stuff now. So, what about the client? And here's where the server component magic comes from. Let's say that I have this comments component, which we already saw, but then inside I have this use state, which is showing the comments only when the user wants to see them. So, it's important to say here that even though this is supposed to be a client component, it's completely renderable on the server, meaning that the server is not going to export or anything if it's this use state. This function is fired only once on the server, and it will be always false, meaning the state visible, and there will be always just the button.
Even though the data is there, what will reach the client, the browser, is actually only this button. So, if we move forward and if we have to ship this to the client, we have to say that I can't really call this function get comments by post ID every time, because when this state changes here, this function will be fired multiple times, meaning that creating the same promise multiple times. So, there needs to be a cache of the promise, and it's getting really complicated. Let's say that it's good practice that we send the promise as a prop to the comments, meaning that we want to send the same promise every time to the comments component. Of course, we have to use client. This is to instruct the bundler, basically to know that this is going to the client. It needs to be incorporated into the bundle, because if we're so, so far, we don't have a bundle at all.
Everything is just on the server, and there is no bundling. So, when I was going through this whole journey, I realized that the tool chain, in my case, is that I have the source folder. I have a compiler that basically translates my TypeScript to valid JavaScript, and based on that, it's creating a bundle as well. Out of this folder, I'm running my Node server, and now when I have the bundle, I'm shipping this bundle to the browser. This is basically how my tool chain looks like. If I use a framework, it's nothing like that. Everything is really short because I just have my TypeScript files, and then I run the server. But what happens with me now? Well, I'm thinking how to implement that, how to make everything this works.
Comments