TIL Server-side rendering vs Static Generation
POSTED ON:
TAGS: optimization
There's two terms web developers will hear frequently:
- Server Side Rendering (SSR)
and - Client Side Rendering (CSR)
Server Side Rendering (SSR) #
Server-side sends a fully rendered page to the client.
(img via https://www.educative.io/edpresso/what-is-server-side-rendering)For example - visiting a site that's SSR will send you code like this.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example Website</title>
</head>
<body>
<h1>My Website</h1>
<p>This is an example of my new website</p>
<a href="http://example.testsite.com/other.html.">Link</a>
</body>
</html>
You can interact with it immediately.
Server-side:
PROS:
- Search engines can crawl the site for better SEO.
- The initial page load is faster.
- Great for static sites.
CONS:
- Frequent server requests.
- An overall slow page rendering.
- Full page reloads.
- Non-rich site interactions.
Client Side Rendering (CSR) #
(img via https://www.educative.io/edpresso/what-is-server-side-rendering)For example - visiting a site with CSR will send you something like this
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example Website</title>
</head>
<body>
<div id="root">
<app></app>
</div>
<script src="https://vuejs.org"type="text/javascript"></script>
<script src="location/of/app.js"type="text/javascript"></script>
</body>
</html>
You cannot interact with the site, and instead will need to wait for all of the JS to download, to allow the SPA framework to operate.
If that sounds like a terrible experience, it has it's trade-offs.
Both React & Vue by default are Client Side Rendering (more about that later). React/Vue offload an incredible amount of complexity compared to trying to write html/css/js without it.
Client-side:
PROS:
- Rich site interactions
- Fast website rendering after the initial load.
- Great for web applications.
- Robust selection of JavaScript libraries.
CONS:
- Low SEO if not implemented correctly.
- Initial load might require more time.
- In most cases, requires an external library.
The flip-side of things #
I've been a huge fan of Nuxt.js, which has options that can pre-render your Vue apps into either SSR or leave it by default to CSR.
It uses a strategy called Static Generation
, which, in layman terms... flattens out your Vue App's pages into it's core basic html/css/js elements. It's also done during the build process.
REFERENCES:
https://www.educative.io/edpresso/what-is-server-side-rendering
Related TILs
Tagged: optimization