<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rajender Joshi</title>
    <description>The latest articles on DEV Community by Rajender Joshi (@rajenderjoshi).</description>
    <link>https://dev.to/rajenderjoshi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F252563%2F29ebb9c9-d55c-4c33-9bfe-5e6f44444b86.jpg</url>
      <title>DEV Community: Rajender Joshi</title>
      <link>https://dev.to/rajenderjoshi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rajenderjoshi"/>
    <language>en</language>
    <item>
      <title>A realistic SSR ready web app with Svelte + Sapper + StarWars API</title>
      <dc:creator>Rajender Joshi</dc:creator>
      <pubDate>Tue, 07 Jan 2020 20:19:35 +0000</pubDate>
      <link>https://dev.to/rajenderjoshi/a-realistic-ssr-ready-web-app-with-svelte-sapper-starwars-api-5fff</link>
      <guid>https://dev.to/rajenderjoshi/a-realistic-ssr-ready-web-app-with-svelte-sapper-starwars-api-5fff</guid>
      <description>&lt;p&gt;How I built a SSR ready &lt;a href="https://keen-clarke-08ba4f.netlify.com/"&gt;Star Wars app&lt;/a&gt; with Svelte in just few hours.&lt;/p&gt;

&lt;p&gt;Svelte - As everyone know it's a "new" cool kid in the block. If you are still living under the rock, I recommend you to watch this presentation by Rich Harris - the creator of Svelte.&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/AdNJ3fydeao"&gt;
&lt;/iframe&gt;
&lt;br&gt;
Sapper? Sapper is a &lt;code&gt;Next.js&lt;/code&gt; equivalent for Svelte. It creates a bare-minimum boilerplate with routing, code-splitting, service worker and what not.&lt;/p&gt;

&lt;p&gt;Recently I decided to try my hands on Svelte to create an app that supports both server side and client side rendering with those fancy shimmer effects.&lt;/p&gt;

&lt;p&gt;So, let's get started.&lt;/p&gt;

&lt;p&gt;Implementing SSR in Sapper is fairly simple and straight-forward. All you need is this code-block in your svelte files.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Adding &lt;code&gt;context="module"&lt;/code&gt; in a script tag with an exported preload function implements SSR and CSR with pre-fetch(optional) like waving a magic wand. But there's a problem.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JFfEXQjo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i.imgur.com/hFNopCO.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JFfEXQjo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i.imgur.com/hFNopCO.gif" alt="Lagging effect"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When I click or hover(with &lt;code&gt;rel=prefetch&lt;/code&gt;) on a menu item, Svelte tries to fetch the route chunk and resolves the api calls and only then navigates to the next page. It gives an impression that the app is either frozen or lagging which can be a frustrating user experience.&lt;/p&gt;

&lt;p&gt;Clone the repo down below and checkout to the problematic commit hash(&lt;code&gt;4bb9d18&lt;/code&gt;) to experience the issue.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vJ70wriM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo-ba8488d21cd8ee1fee097b8410db9deaa41d0ca30b004c0c63de0a479114156f.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/crup"&gt;
        crup
      &lt;/a&gt; / &lt;a href="https://github.com/crup/svelte-ssr-swapi"&gt;
        svelte-ssr-swapi
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Demo project to integrate SSR with Svelte with workaround for preload lag. https://keen-clarke-08ba4f.netlify.com/
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git clone https://github.com/crup/svelte-ssr-swapi.git
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;svelte-ssr-swapi
&lt;span class="nv"&gt;$ &lt;/span&gt;git checkout 4bb9d18
&lt;span class="nv"&gt;$ &lt;/span&gt;yarn &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; yarn dev
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;How to fix this? With only a couple of hours of experience in Svelte, I don't know the right way but this is how I solved it me.&lt;/p&gt;

&lt;p&gt;Create a Svelte store and define a key which will be an identifier for for distinguishing if the page is SSR or CSR.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;In your routes file, instead of resolving the promise and returning the resolved response in &lt;code&gt;cards&lt;/code&gt; key, read the SSR state and and return promise if &lt;code&gt;isSSR&lt;/code&gt; is false. &lt;br&gt;
Here's the boilerplate:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;On your page level component set &lt;code&gt;isSSR&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt; and resolve &lt;code&gt;cards&lt;/code&gt; if it's a promise. Setting &lt;code&gt;isSSR&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt; on first client-side render will enforce script tag with &lt;code&gt;context="module"&lt;/code&gt; to return a promise instead of response.&lt;/p&gt;

&lt;p&gt;With this approach we can now resolve apis on client side, show loaders/shimmers without any lag.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;Now comes the worst part - rendering this list items. Since we now have an array(server-side) and a promise(client-side). I had to write handlers for both array and promise.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;The final version is in &lt;code&gt;master&lt;/code&gt; branch and it looks like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UqjJjjLg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i.imgur.com/HQuCLuc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UqjJjjLg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i.imgur.com/HQuCLuc.gif" alt="Final result"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;a href="https://keen-clarke-08ba4f.netlify.com/"&gt;Demo&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Disclaimer: &lt;br&gt;
This is my very first attempt at Svelte and chances are that I may have done massive blunders like improperly accessing/updating/unsubscribing store, duplicating code in rendering cards and routes. There's a also a room for refactor in final branch. So, use this code at your own risk.&lt;/p&gt;

&lt;p&gt;If there's a better way to achieve the same, feel free to create a PR and suggestions are welcome. :)&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vJ70wriM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo-ba8488d21cd8ee1fee097b8410db9deaa41d0ca30b004c0c63de0a479114156f.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/crup"&gt;
        crup
      &lt;/a&gt; / &lt;a href="https://github.com/crup/svelte-ssr-swapi"&gt;
        svelte-ssr-swapi
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Demo project to integrate SSR with Svelte with workaround for preload lag. https://keen-clarke-08ba4f.netlify.com/
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;


</description>
      <category>svelte</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>sapper</category>
    </item>
  </channel>
</rss>
