Playing Next Lesson In
seconds

Transcript

  1. All right, so as promised, let's discuss what the difference is between server-side rendering and client-side rendering. So as briefly discussed in the last lesson,

  2. server-side rendering comes through with all of the initial markup needed for the initial page load already rendered out and provided directly inside of the document of our response.

  3. Client-side rendering, however, just has the mounting element and nothing inside of the mounting element for our view application, React or whatever it is they may be using there.

  4. Beyond that initial render with subsequent requests, they both will behave the same. They just come through with data needed to update the page accordingly.

  5. So in the back here on the left, I have our actual plot my course application that we'll be building out that we created in the last lesson. Then on top on the right hand side,

  6. I have a client-side rendered application with SSR disabled. So if I were to expand my inertia application over here, go into app, we just have an app.ts.

  7. That would look the exact same as our app.ts on our server-side rendered application, but we do not have a SSR application on our client-side rendered as we don't need it.

  8. Beyond that, the CSS and pages both look the exact same, and they both have a separate TS config from our AdonisJS application as well.

  9. Additionally, resources, views, and our initial layout looks the exact same too. It's got our VEAP method adding in our scripts in our initial inertia page, and

  10. then it also has the inertia head and the inertia tag as well. If I go ahead and do Ctrl+tilde, open up the terminal, and we boot these up, so npm run up there.

  11. All right, so our client-side application is at 3333, and we jump over to our server-side application, command-tilde, npm run dev. This one will be at 52914.

  12. If we go ahead and open up our server-side application now, so I'm gonna command-click on it, and that will open up our browser here. This is our server-side rendered application, and I'll go ahead and

  13. just jump back into Visual Studio Code and hop over to our client-side one, command-click on that to open that one up as well. The left tab here is our server-side rendered application, and

  14. on the right-hand side here is our client-side rendered application. So if we open up the Inspect panel and take a look specifically at our network,

  15. and it's the document request that we're most interested in here, go ahead and give this page a refresh. All right, you can see we have all of our scripts coming through here. That looks fine. Let's scroll up to our document though, and

  16. let's take a look at the response, specifically the role response. Within here, you can see our VEAP scripts getting added in, as well as that initial page component right there.

  17. And then inside of our actual body, we have our InertiaJS application mounting point, and that's provided all of the initial page data as a data page attribute.

  18. So if we scroll over to the right though, we can see that this element contains no information, it's just empty. That's where the main differences between client and server-side rendering happens.

  19. Client-side rendering will come through completely empty for our mounting element. It's up to the client-side to then register all of the scripts for

  20. the scripts to run and to render out the initial content. If we were to jump into our expect panel, and there is our app ID right there, take a look at the child content.

  21. There is all of the content being rendered out, specifically on the client side. None of what's inside of this app element here is rendered and

  22. provided by our server whatsoever, as this element is completely empty. If, however, we were to jump over to our server-side rendered application,

  23. right-click on this, inspect, jump into the network panel, expand this out a little bit, and give it a refresh. All right, we see all of our scripts come through. That's a-okay again.

  24. Let's jump up to our document, take a look at the response, and specifically the raw response. Again, you see all of the scripts come through along with our initial page component.

  25. That looks fine, but inside of our body here now, we have our mounting element, again with all of the initial page data as data page attribute right there.

  26. But if we scroll over to the right-hand side, you'll see that this element is not empty and actually contains all of that initial HTML that we see directly on the page.

  27. This was furthermore insinuated whenever we took a look at the actual preview of the rendered content. We see all of the content come through with the actual response,

  28. whereas with our client-side application, all of the content is completely empty. There's no content coming through with the document whatsoever. And that's because with our server-side rendered application,

  29. it's making that initial render on the server-side, hence the name server-side render. Now beyond the initial render, they both will behave the same. We don't have any additional pages right now, but

  30. it just comes through as an API response with the updated page data. And then the client-side will make that update accordingly. So it's just the initial render here that's happening server-side,

  31. which is the same as any server-side rendered client application. So what are the pros and cons here? Well, server-side rendering requires the HTML generated on the server-side

  32. to match the initial render on the client-side. This is what we discussed in the last lesson as hydration mismatch. Furthermore, packages may or may not support SSR or server-side rendering.

  33. So you will need to check on a per package basis to see whether or not it supports server-side rendering. However, since the rise in popularity of Next and Nuxt, a lot more packages have been taking SSR into consideration.

  34. So this is less of an issue nowadays as it was back when it was a brand new subject. Some of the pros of SSR though is since the HTML is initially rendered on

  35. the server, we're gonna have a quicker initial load time since it doesn't need to load all of the scripts to then render it out. It already has the initial render ready to go directly from the document.

  36. Because of that, SSR is generally recommended whenever SEO or search engine optimization is important for your application,

  37. which is typically important for content-rich applications, stuff like blogs, marketing materials, and things of that nature.

  38. Really anything that you would want search engines to be able to pick up on and recommend your site for. Now, yes, as a caveat, Google has been able to read JavaScript for quite some time now. But the scope and

  39. effectiveness of it being able to read that has been up for debate. And since you do have that initial render time being quicker with SSR, it's still generally recommended.

  40. Client-side rendered apps, on the other hand, are just much more straightforward and easy to work with because you don't have the extra complexities of SSR to worry

  41. about, and you don't have the hydration mismatches to worry about either. It only ever needs to worry about serving content directly inside of the user's browser rather than on your server.

  42. So at a top level, the debate between server-side rendered and client-side rendered for your application comes down to, do you need search engines to be able to read and recommend your content?

  43. In our particular case for our application, we don't need it whatsoever. But because server-side rendering takes some extra steps, we're gonna cover that throughout the series, so we are using SSR here.

Server-Side Rendering (SSR) vs Client-Side Rendering (CSR)

@tomgobich
Published by
@tomgobich
In This Lesson

We'll discuss the differences between InertiaJS in a server-side rendered (SSR) and a client-side rendered (CSR) application. We'll then compare when you would want to choose one over the other and the pros and cons of both.

⏳ Chapters

00:00 - Introduction
00:35 - Comparing the Project Structures
01:40 - Comparing the Initial Document Render
04:20 - Pros/Cons

Join the Discussion 6 comments

Create a free account to join in on the discussion
  1. @fauzan-habib

    This is exactly what I need, did a marathon on this series and I can't wait for the next lessons coming in

    1
    1. Responding to fauzan-habib
      @tomgobich

      Thanks so much, Fauzan!! I'm ecstatic to hear that! 😁

      0
  2. @tmuco

    @tomgobich

    Thank you for this great series. Are there any advantages to combining Edge with Inertia versus just using Inertia SSR? Let’s say I want the initial page and maybe the blog to be server-side rendered, while keeping the rest of the app as a SPA?

    1
    1. Responding to tmuco
      @tomgobich

      Hi Tresor! Yeah, there's some benefits to that, but also cons. The benefits are that you can easily ensure those pages where SEO is important have as small of a weight to them as possible. Many times marketing materials have a different look and feel from the internal application as well, so having them completely split between your Edge pages & Inertia app can help define that barrier.

      Though Google and other search engines have been getting better about reading JavaScript, you'll also have piece of mind in knowing exactly what markup is being sent when using Edge as well.

      The main con is for those instances where there isn't a visual separation between marketing materials and the internal application. For example, say we're using a UI library in the SPA app but we still want that same look on the Edge pages. We'd need to replicate that UI library's CSS for our Edge pages in order for the look and feel to remain the same. Of course, that may not be an issue if your UI library is framework agnostic or uses web components, but if you're using something like ShadCN, Nuxt UI, etc then that can be a real issue.

      In those cases, or even if you just prefer to have everything reside within Inertia, you can specify which pages in Inertia should use SSR. The SSR allowlist resides within the config/inertia.ts configuration file and accepts an array of page names or a function that returns a boolean.

      Below are two examples from the linked documentation for this.

      import { defineConfig } from '@adonisjs/inertia'
      
      export default defineConfig({
        ssr: {
          enabled: true,
          pages: ['home']
        }
      })
      Copied!
      import { defineConfig } from '@adonisjs/inertia'
      
      export default defineConfig({
        ssr: {
          enabled: true,
          pages: (ctx, page) => !page.startsWith('admin')
        }
      })
      Copied!
      1
      1. Responding to tomgobich
        @tmuco

        Oh, thanks, I didn’t know about the SSR allowlist. My main concern was with the UI library since I couldn’t easily replicate the same look on the pages that use Edge. I’ll go with the SSR allowlist for now. Appreciate it!

        1
        1. Responding to tmuco
          @tomgobich

          Yeah, that can definitely be a major headache depending on the UI library! Anytime!! 😊

          1