Rich Harris explains why SvelteKit pushes for Server Side Rendering (and against SPA / CSR)
Tags: sveltekit, ssr, node, jamstack
Published: 2022-05-12 16:41:55

Since I began my journey into headless / jamstack concepts and modern javascript frameworks, rendering modes were always a tricky part to get my head around. There is for example this informative, but possibly difficult to understand comparison on [web.dev/rendering-on-the-web/](https://web.dev/rendering-on-the-web/):

![Comparison of rendering modes SSR, CSR, etc.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rnpd4ekqcbg731n6cwna.png)

I totally get most aspects technically, but the real question is: "How should I build my web app?!?!". #DecisionFatigue 🤯😉 

Therefore it was super interesting to hear [Rich Harris](https://twitter.com/Rich_Harris), creator of Svelte(Kit), reacting to this topic. He collected questions in a reddit thread called ["What frustrates you in using Svelte?"](https://www.reddit.com/r/sveltejs/comments/u6ji0z/what_frustrates_you_in_using_svelte/) and answered some of them in his talk at SvelteSummit (great format!). 

Just for context beforehand - a simplified version of my personal journey: 

- In regards to [Sustainable Web Design](https://sustainablewebdesign.org/) I initially had the goal to use "full Static Site Generation (SSG)". This works fine for many use cases - but I also learned that this has its own challenges, e.g. on a site with > 1000 blog posts. 
- The next mode which is easy in regards to hosting the final site is the Single Page Application (SPA) mode. This is essentially a <html> page placeholder with some (rather big) .js files which will do all the data fetching and page generation after page load (= CSR = Client Side Rendering). While many hate these "SPA monsters", they sure are easy to deploy: Just upload the html file with the .js files. 
- And there is server side rendering (SSR) which requires a (sometimes pricy) special webspace provider with NodeJS support, e.g. [Vercel](https://vercel.com/), [Render](https://render.com/), etc. Or you need to use a Virtual Private Server with NodeJS (which can be managed via [cleavr](https://cleavr.io/) or - as I learned recently - [LaravelForge](https://codeburst.io/how-to-deploy-a-nodejs-server-using-laravel-forge-a9a46dbdc761)).

Here is the question which resonated to me because I was always somehow sceptical about Server Side Rendering (SSR) because of the hosting requirements (and costs):

> It's a sveltekit thing, but I really dislike how heavily SSR is pushed. Search engines do actually run JS so CSR isn't a big deal for SEO, and the slower first page speeds do negatively impact googles search rankings. And to turn it off I need to write a hook function and install an adaptor. It's not very ergonomic. 
> To be fair, Next.js also heavily pushes SSR because it's maintained by the folks at Vercel.

Here is the insightful reaction of Rich Harris at [4:50:00](https://youtu.be/qqj2cBockqE?t=17407):

{% youtube https://youtu.be/qqj2cBockqE?t=17407 %}

So, quick notes about this, according to Rich Harris:

- Don't use SPA unless you have a very good reason
- Advantages of SSR:
   - google search bot does run javascript, but this process is less reliable (and happens less frequently) than pure HTML scanning (without javascript) => server side rendered content will be indexed more reliable and more frequently
   - SSR is good for performance, data can be fetched instantaneously 
   - resilience: javascript can fail for various reasons client side, because you don't have control of how it is executed

Thanks to this I do understand the push for SSR better and it sure has advantages as well. Most noteworthy is the advantage  that we would never have to discuss SPA monsters again in future ;-) 

More resources about this regarding SvelteKit:

- [SvelteKit adapter node for SSR](https://kit.svelte.dev/docs/adapters#supported-environments-node-js)
- [SvelteKit appendix for all rendering modes](https://kit.svelte.dev/docs/appendix)
- [SPA mode docs in adapter-static (with warning)](https://github.com/sveltejs/kit/tree/master/packages/adapter-static#spa-mode) 

Important: Single Page Application (SPA) mode should not be confused with Static Site Generation (SSG) via adapter-static. SSG wasn't mentioned in the talk, but seems fine (with some limitations) according to https://kit.svelte.dev/docs/appendix#ssg:

> Static Site Generation (SSG) is a term that refers to a site where every page is prerendered. This is what SvelteKit's adapter-static does. SvelteKit was not built to do only static site generation like some tools and so may not scale as well to efficiently render a very large number of pages as tools built specifically for that purpose. However, in contrast to most purpose-built SSGs, SvelteKit does nicely allow for mixing and matching different rendering types on different pages. One benefit of fully prerendering a site is that you do not need to maintain or pay for servers to perform SSR. Once generated, the site can be served from CDNs, leading to great "time to first byte" performance. This delivery model is often referred to as JAMstack.

But as already mentioned it has some challenges and some use cases are easier to solve with SSR. 

Would be interesting to look into SSR vs. SSG (and all the variations of these modes, e.g. prerendering) in regards to #SustainableWebDesign. 🌱

To be continued ... Please let me know if you have interesting resources about this as well!

PS: There is also a recent episode on the Changelog podcast, didn't listen to it yet: [Were SPAs a big mistake?](https://changelog.com/jsparty/224) and some insightful links in the latest [JAMstacked newsletter #55](https://jamstack.email/issues/55). In regards to web accessibility Manuel Matuzović wrote a summary: [https://www.matuzo.at/blog/2023/single-page-applications-criticism/](https://www.matuzo.at/blog/2023/single-page-applications-criticism/).

New resources:
- [Deploy SvelteKit with SSR on Coolify (Hetzner VPS)](https://dev.to/mandrasch/deploy-sveltekit-with-ssr-on-coolify-hetzner-vps-24c5)
- [Host SvelteKit apps with SSR-support via ploi.io (on Hetzner Cloud)](https://dev.to/mandrasch/host-sveltekit-apps-with-ssr-support-via-ploiio-on-hetzner-cloud-1cpa)
- [Hosting SvelteKit as SSR on mittwald SpaceServer](https://dev.to/mandrasch/hosting-nodejs-ssr-sveltekit-apps-on-mittwald-spaceserver-1a3g)