And that HTML, well, it can contain script tags. And those script tags can actually move the HTML into position and serialize any data and trigger hydration. So all this work can happen independent of the framework actually even being loaded on the page.
I took this demo off Marco's landing page, but it works very similar to how we do this in Solid. And to do that, I have another code example. But not really another. What I did actually was I'm using Solid's metaframework Solid Start, but essentially now we have HTML in the head and a few other things, but what you're going to see is almost the same demo again where I have a suspense boundary with loading, wrapping our router, and then I have a header and a footer component to actually prove that I'm not making this up. And if I go over to our routes and get our index route, what you're going to see is the component looks very similar to the one we just authored where we have our two resources fetching the stuff, and a nested suspense boundary to show the exact same stuff. And the reason I want to show this is when you reload the page, it looks actually really similar to what we just did. The only difference here is now the data fetching is happening on the server. It's exactly the same code, but it works straight ahead in SSR. And the kind of thing that's cool here, and I'm going to show this really quick, hopefully, is we get our HTML back, you know, we got our head and some CSS getting loaded here, but what's really interesting here is the main body has the header, and has the footer, like I showed, and then you actually see the loading in here, and some placeholder comments and stuff, some scripts to get started, but then we close the body and the HTML, so the document's done, but then what's this at the end? A template where we have John Lennon, and loading posts, so we have the next UI state, the data we need to hydrate it, which is just this John Lennon, and this actually is the code here, that just moves the stuff from the bottom of the document and inserts it, and then underneath that, well, as it completes, we get another template, and this has our listing of posts, and again, the data we need to hydrate it, so it just incrementally comes in on the page. Now this is obviously pretty cool, but maybe you don't want to show the page until the title only shows up, and there's an easy enough way to do that. We can just, to our resource, go defer stream true, and now, when I reload the page, you can see it actually waits until John Lennon is there before, so we get complete control over when we start streaming. We can, you know, do our auth first, make sure the person's all good, and then stream in some of the content that takes slower to load perhaps, but if you notice again, it's all the same code that we have been using in the other example, whether you're using SSR, you don't need server components or any kind of fancy stuff. This is just suspense plus resources.
Loading placeholders are helpful, but they can sometimes be jarring, pulling the user out of the experience. When the page initially loads, we have nothing to show, so, showing a loading placeholder is, you know, a great affordance, but on subsequent navigation, we already have content to show, the current page. So, I already mentioned that keeping things in the past is a little bit trickier to do. We can't just block an async, we can't just make an async component and expect it to work. That's because some unrelated state change or end-user action might interact with the page, and it might see that inconsistent state when it is reading it. And, you won't get your expected outcome. Perfect example is, pretend you navigate to the next. You have a carousel and you navigate to the next page.
Two minutes? Really? Oh. All right, sweet. So, there's like an eight-minute countdown over here. Okay. Well, I want to at least show a transition off here.
Comments