<?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: eshan</title>
    <description>The latest articles on DEV Community by eshan (@eshan).</description>
    <link>https://dev.to/eshan</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%2F388740%2F6227c8a9-76de-4e52-b643-48ed3be49ec3.jpeg</url>
      <title>DEV Community: eshan</title>
      <link>https://dev.to/eshan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eshan"/>
    <language>en</language>
    <item>
      <title>Toward the Postmodern Web</title>
      <dc:creator>eshan</dc:creator>
      <pubDate>Thu, 28 May 2020 22:52:01 +0000</pubDate>
      <link>https://dev.to/eshan/toward-the-postmodern-web-38h</link>
      <guid>https://dev.to/eshan/toward-the-postmodern-web-38h</guid>
      <description>&lt;p&gt;So you read &lt;a href="https://macwright.org/2020/05/10/spa-fatigue.html"&gt;Second-guessing the modern web&lt;/a&gt; and found it compelling. Maybe moving everything to client rendered React SPAs is not the answer. But now what? What comes after the Modern Web? How do we keep the good parts of both server rendering and SPAs? What might the Postmodern Web be? &lt;/p&gt;

&lt;h1&gt;
  
  
  Declarative vs Imperative
&lt;/h1&gt;

&lt;p&gt;React is rightly praised for being declarative in nature.  To review:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Declarative&lt;/strong&gt; - Your boss sends you a spreadsheet to update with new data. You punch it in, and automatically a bunch of formula cells update, some of which you didn't even know about.  You take a long lunch and relax.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Imperative&lt;/strong&gt; - Your boss sends you a spreadsheet to update, but for some reason has exported it to a lifeless CSV.  You punch in your data, you're not sure what these other poorly labeled columns are, and it's going to be a long night. (Or similarly, the server sends you some lifeless html, the user has issued a click event, and now you need to figure out all the jQuery DOM mutations you need to do to get this thing working again.)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Your app as a tree-shaped spreadsheet
&lt;/h2&gt;

&lt;p&gt;How do we get those declarative qualities in a server rendered app?&lt;br&gt;
Below is a toy app written to behave like a spreadsheet.  Try adding items until you get free shipping, or reducing quantity to zero.&lt;/p&gt;


&lt;div class="glitch-embed-wrap"&gt;
  &lt;iframe src="https://glitch.com/embed/#!/embed/sparkly-onyx?previewSize=100&amp;amp;attributionHidden=true&amp;amp;path=index.html" alt="sparkly-onyx on glitch"&gt;&lt;/iframe&gt;
&lt;/div&gt;


&lt;p&gt;When you hit the plus button for apples, that code simply changes the value in the textbox.  The Apple total amount updates itself.  Here's the application code for it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;td&lt;/span&gt;
  &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"subtotal"&lt;/span&gt;
  &lt;span class="na"&gt;data-source=&lt;/span&gt;&lt;span class="s"&gt;"apple"&lt;/span&gt;
  &lt;span class="na"&gt;data-text-content=&lt;/span&gt;&lt;span class="s"&gt;"$num('#apple .unitprice') * $num('#appleqty')"&lt;/span&gt;
&lt;span class="nt"&gt;&amp;gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;That's all the application code needs, just like a spreadsheet cell. The rest is handled by the "library" code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;data-text-content&lt;/code&gt; attribute is the formula for the textContent property of the &lt;code&gt;&amp;lt;td&amp;gt;&lt;/code&gt;, which could be any Javascript expression.  The formula is put into a render function on the node.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$num()&lt;/code&gt; is a convenience function that takes a CSS selector, finds a matching node, and returns its value as a number.  This formula is simply multiplying unit price and quantity to get a subtotal.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;data-source attribute&lt;/code&gt; is the id of a DOM element to monitor.  A MutationObserver watches this node, and if there are changes, issues an event that triggers the render function. (There's probably a way to infer this from the formula itself, the way a spreadsheet would, and not need this at all.)&lt;/li&gt;
&lt;li&gt;Also, I had to "fix" text &lt;code&gt;input&lt;/code&gt;s a bit.  By default, Javascript changes to their values do not trigger change events or persist to the DOM; I modified their prototype so that they do.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the plus button modifies the quantity text box (and that's it). The &lt;code&gt;#apple .subtotal&lt;/code&gt; cell notices a change has occurred in its source, so it reruns its formula. Meanwhile, the &lt;code&gt;#itemtotal&lt;/code&gt; cell has been watching all the &lt;code&gt;.subtotal&lt;/code&gt; cells, so it reruns its formula, and so on, all the way down the &lt;a href="https://en.wikipedia.org/wiki/Directed_acyclic_graph"&gt;DAG&lt;/a&gt; of the application.  (This is similar to using &lt;code&gt;setState()&lt;/code&gt; in React and having props drill all the way down.)&lt;/p&gt;

&lt;p&gt;Say you wanted to add a new feature that tells clients how close they are to getting free shipping. This is it:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;data-source=&lt;/span&gt;&lt;span class="s"&gt;"footer"&lt;/span&gt; &lt;span class="na"&gt;data-text-content=&lt;/span&gt;&lt;span class="s"&gt;"
  $num('#shipping') &amp;gt; 0 ? 
  'Only $' + (25 - $num('#itemtotal')) + ' until free shipping!' : ''
"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This is one possible way to bring a declarative style back to server rendered apps, without having to completely upend everything. More subjectively, it feels &lt;em&gt;web-y&lt;/em&gt; to me. &lt;/p&gt;
&lt;h1&gt;
  
  
  Animated Transitions
&lt;/h1&gt;

&lt;p&gt;But wait, &lt;a href="https://dev.to/richharris/in-defense-of-the-modern-web-2nia"&gt;what about about animated transitions?&lt;/a&gt; This one probably matters more for big consumer facing brands, but one of the things you can do with client side routing is have slick animated transitions between "pages" instead of the page flash you typically get between two server rendered pages.&lt;/p&gt;

&lt;p&gt;However, with some helpful libraries, you can layer transitions on top of server side routing. Here's the toy app again; try clicking some of the links and you'll see a "shared element transition" where an element from one page will seemingly accompany you to another page with a smooth animation:&lt;/p&gt;


&lt;div class="glitch-embed-wrap"&gt;
  &lt;iframe src="https://glitch.com/embed/#!/embed/sparkly-onyx?previewSize=100&amp;amp;attributionHidden=true&amp;amp;path=index.html" alt="sparkly-onyx on glitch"&gt;&lt;/iframe&gt;
&lt;/div&gt;



&lt;h2&gt;
  
  
  How does it work?
&lt;/h2&gt;

&lt;p&gt;There are a few parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Server rendered pages - in this case they are just static pages&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/turbolinks/turbolinks"&gt;Turbolinks&lt;/a&gt; - this library does most of the work: 

&lt;ul&gt;
&lt;li&gt;Intercepts clicks on a link to another page&lt;/li&gt;
&lt;li&gt;Gets the contents of that page via XMLHttpRequest&lt;/li&gt;
&lt;li&gt;Does a  swap into the current page&lt;/li&gt;
&lt;li&gt;Changes the URL, makes a history entry, etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;DOM diffing (of a sort) - I check if any elements on the current page are supposed to persist into the new page and collect their locations and sizes&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://css-tricks.com/animating-layouts-with-the-flip-technique/"&gt;FLIP technique&lt;/a&gt; - For each persistent element, I take the new instance, "rewind" to the position and size of the old instance, and then let it animate to the new position and size.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is a fairly naive implementation but hopefully it gets the point across: you don't need to go all the way to a SPA just for page transitions. (There are other libraries as well out there that handle "bigger" page transitions such as &lt;a href="https://barba.js.org/"&gt;barba.js&lt;/a&gt;.)&lt;/p&gt;

&lt;h1&gt;
  
  
  Looking ahead
&lt;/h1&gt;

&lt;p&gt;So, is there anything of value here? What else might the Postmodern Web look like? Will web components play a role? Let me know what you think in the comments!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
