why is SSR faster than SPA and vica versa?
Asked Answered
N

3

11

I've been reading a lot about SPA vs SSR and maybe I understand its real idea or maybe not. I'd really appreciate someone experienced who can tell my if my assumptions are right or mean something.

Observation 1)

SPA - client requests www.example.com, this goes from browser to server. Server returns index.html which has just <div id="app"></div> and the script source for javascript file. Browser again makes another request for that bundled js file, server returns it. After that returned js file kicks in and starts executing. When compiled and done, the page is shown to the user.

SSR - client requests www.example.com from browser to server. Server does all the things, making any api calls or other things, puts everything in html and returns html. if these html has some styles or other js sources, browser will make requests for these too.

What I think - Why is SSR faster? Is it because in the SPA case, it had to download the whole js file of the whole website ? and in the SSR case, only the content of that specific page user is entering gets returned ?

Observation 2)

SPA - if the page got loaded and user clicks on one of other routes, it won't make any request to the server to get the html to show it to the user. all the route's js are already downloaded so no need to make request to the server unless there's an Ajax call for some dynamic data from database.

SSR - this would again make the request to the server to get the html file for the new page.

What I think SPA is faster in this case, even though SPA will still need to make the ajax request for some data. Ajax request for some data seem to be faster than the request to download the newly rendered html which would also need that ajax call on the server.

I know SSR is good for SEO, but i am only interested about performance. What do you think? Everything correct about what I said?

Neckband answered 3/8, 2019 at 14:15 Comment(0)
M
10

SPA

Load once, navigate freely. Sounds great, in theory at least. The initial load time can be a burden. If you have 6 different screens in your application, each taking .5s to load. Would you rather wait .5s everytime you get to a new one, or the initial 3s ? For most user any long loading time is unacceptable, so it's often better to load things incrementally.

On top of that, you often have the burden of actually running the framework to create pages inside the client browser for most modern JS framework (angular, vuejs, reactjs, etc...). That can create performance issues in some cases.

SSR

Generate everything server side, serve content dynamically. Sounds better for code splitting and letting the user load only what he needs. On top of that you can run your framework on a powerful server rather than the client computer/phone/fridge.

However, as you stated, you need to request more content more often. To avoid that, most modern framework are able to create partial routes, dynamically loading page fragments inside a fixed layout if only part of the page need to update when routing.

But that's not all, introducing

Service worker

Service workers and SSR are working very well together. If you establish a cache first strategy, that means that every time your user goes from page A to B to A again, you'll load applications fragment for A and B only once. You service worker will recognize that you are using the same fragment again and pull it from the user cache rather than making a new network request.

It makes things feel lightning fast.

On top of that you can also preload content. A user if hovering a link ? Start loading the app fragments used in this route. It might only save a few hundred ms, but when you are loading small apps fragments it can feel instantaneous for your user.

What's the catch then ? Well first of all it can actually be worse if you have a static application. Cache exists also for SPA, and if your app is small enough you might not care about the few ms saved during the initial loading time. Even with a larger application, it's mostly dependent on your user base.

But mostly, SSR requires an actual server to run the app. A lot of hosting services, like firebase or aws, allow you to host static files like you could use for a PWA, as well as handle a database easily from the client side, for a really cheap, if not free, cost. SSR will require a server, so you will have an increased running cost as those services.

Maracaibo answered 3/8, 2019 at 14:39 Comment(4)
Thanks for this. I want to add something: 1) is what I said correct? 2) Nuxt.js as I can see when using server-side-rendering with it still downloads js files and also pre generated html. THe reason it does this is because when user first goes to website, nuxt provides already generated html which is faster. but in the background it still downloads js files so that when changing route links, it can use that download js because it already got compiled/downloaded in the background. what do you think?Neckband
It downloads js files to manage all the interactions you used in your components, but also to be able to manage the DOM and updates fragments of it in a process called hydration. Otherwise yes your observations seem to be accurate.Maracaibo
yeah, but when user first goes to the page, pre generated html gets returned. to show that page, there's no need for that whole huge javascript files. those javascript files will be better used when navigating to different components after first page load.Neckband
Except your application won't know what to do when you are navigating if you don't have those js files. SSR still isn't a classic server serving static html pages as you could get from a PHP server. The js files track exactly what has already been loaded to minimize the loading time for the next route.Maracaibo
F
1

In regards to your second observation, an SPA doesn't have to download everything up front . Through dynamic import an SPA can download assets on demand when a user switches route. This is an optimization to prevent the overhead of downloading everything up front.

As far as SSR vs SPA, SSR generally gives a better user experience. The server can send ready to be rendered HTML through SSR, so the application is mostly making dispatches and rendering the results with minimal client-side logic. If an SPA is designed with a framework, most assets downloaded will have to be processed prior to rendering. However, an SPA can be designed such that the minimal amount of code is provided on initial load and then all remaining assets are downloaded in the background.

You can do an awful lot with an SPA, but having the ability to offload processing to a dedicated server is almost always a win. However, if you are worried about user bandwidth or if you are developing a mobile application then an SPA might be worth considering. An SSR app can still do well here, but if you can get the assets onto the user device up front then you should have less latency-related issues.

Again everything comes down to design decisions, but from my experience an SSR application is more performant and scalable.

Feliciafeliciano answered 3/8, 2019 at 14:39 Comment(0)
H
1

SSR provides the end user with a perceived performance improvement. If you are using SSR and SPA together, then SSR will give the end user something to look at quickly until the SPA is booted and the page is ready to be interacted with.

Hickerson answered 15/11, 2019 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.