A place to cache linked articles (think custom and personal wayback machine)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.md 9.8KB

4 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. title: Building with Friction
  2. url: https://timkadlec.com/remembers/2020-03-18-building-with-friction/
  3. hash_url: 331eb17ffb3f4fbb5fdd8123c0dc1eeb
  4. <p>I <a href="https://timkadlec.com/remembers/2020-01-02-making-the-right-thing-easy/">recently wrote about how important it is to make the right thing easy</a>. The opposite is also true: it’s important to make the wrong things difficult. I did allude to it in that post a little bit, but I thought it was worth calling out explicitly. It’s important to introduce some friction in our workflow to help prevent the wrong actions.</p>
  5. <p>Continuing on the health-related analogies, friction is a big part of how I manage my sweet tooth. I work by myself in a small office. Nothing is preventing me from constantly snacking on a bunch of sweets, and <em>wow</em> would I ever love to. I’m a sucker for just about anything with sugar.</p>
  6. <p>But I discovered something else about myself: I’m also kinda lazy. So I take a two-part approach. The first is to make the right thing easy. I have apples, oranges, almonds, dried cranberries, and all sorts of healthier snacking options right next to my desk. If I’m hungry, I don’t even have to move. I reach out my arm, and there they are.</p>
  7. <p>But the second part of that process is just as important. I make the wrong thing harder. I do have some sweets, but they’re tucked away in an adjacent room. It’s not difficult to get to them, but it does require more effort than the healthy alternatives right next to me. It doesn’t stop me from having sweets, but it means that reaching for that chocolate involves a conscious decision to put in more work than if I decide to have an apple. It’s just enough friction to change the way I snack.</p>
  8. <p>A lot of modern workflow improvements have been around <em>removing</em> friction. We want to make it easier to deploy rapidly. Tools like npm make it <em>very</em> easy to gain access to any and all modules we could think of. Tag management enables folks to very quickly add another third-party service.</p>
  9. <p>All of these things, on the surface, provide some value, but the consequences are tremendous. Because these processes remove friction, they don’t ever really give us a chance to pause and consider what we’re doing.</p>
  10. <p>Re-introducing some healthy friction, some moments of pause, in our processes is critical to ensuring a higher level of quality overall.</p>
  11. <p>For example, let’s tackle the trouble with <a href="https://www.npmjs.com/">npm</a>.</p>
  12. <p>npm transformed the way we build, but I don’t think anyone can argue that it hasn’t wreaked some serious havoc in the process. The ready availability of a JavaScript module for pretty much anything you can imagine has lead to security issues, accessibility concerns and overall bloat. It’s made it <em>too easy</em> to add more code to our sites without ever considering the trade-offs.</p>
  13. <p>I’m with <a href="https://twitter.com/slightlylate/status/1239404640656998400">Alex on this one</a>. Adding more code should be a very intentional decision:</p>
  14. <blockquote>
  15. <p>JavaScript should be a *deeply” intentional choice on the client. Tools that remove intentionality, whatever else they may have done for your team, probably sunk your perf battleship.</p>
  16. </blockquote>
  17. <p>Here’s an example of how we could introduce some friction into the process to help with the performance challenges by focusing on two critical points in our workflow: install and build/deploy.</p>
  18. <h2 id="during-install">During install</h2>
  19. <p>The first thing we can do is introduce some friction when we first install a script. After all, the easiest issues to fix are the ones that haven’t happened yet.</p>
  20. <p>I like <a href="https://github.com/AdrieanKhisbe/bundle-phobia-cli#bundle-phobia-install"><code>bundle-phobia-install</code></a> for this. <code>bundle-phobia-install</code> is a wrapper around <code>npm install</code> that uses information from <a href="https://bundlephobia.com/">Bundlephobia</a> to conditionally install npm modules. It does this by comparing the size of the package against some predetermined limits. It defaults to a size limit of 100kB overall (as in, the total of all dependencies), but you can configure that however you would like.</p>
  21. <p>You can also set up limits on individual packages.</p>
  22. <p>For example, the following settings (configured in a <code>package.json</code> file) would ensure that no individual package with a size of over 20kB could be installed, and that the total size of all dependencies can be no more than 100kB.</p>
  23. <div class="highlight"><pre><code class="language-javascript" data-lang="javascript">...
  24. <span>"bundle-phobia"</span><span>:</span> {
  25. <span>"max-size"</span><span>:</span> <span>"20kB"</span>,
  26. <span>"max-overall-size"</span><span>:</span> <span>"100kB"</span>
  27. },
  28. ...
  29. </code></pre></div>
  30. <p>Now, if we were to try to install, say, <code>lodash</code>, the install would fail because <code>lodash</code> exceeds our individual package size limit.</p>
  31. <figure class="banner">
  32. <img src="https://timkadlec.com/images/friction-bundle-phobia.png" alt="Running bundle-phobia-install instead of npm install lets us enforce size limits on npm modules, preventing us from adding significantly heavy dependencies to our site."/> <figcaption>
  33. <p>Running <code>bundle-phobia-install</code> instead of <code>npm install</code> lets us enforce size limits on npm modules, preventing us from adding significantly heavy dependencies to our site.</p>
  34. </figcaption>
  35. </figure>
  36. <p>You could still install <code>lodash</code>, but that now requires you to run <code>bundle-phobia-install</code> with the interactive flag (<code>-i</code>) and manually approve the install despite the fact that it exceeds your limits. It turns an unconscious decision into a conscious one.</p>
  37. <h2 id="during-build-deploy">During build/deploy</h2>
  38. <p>By having some friction on the install process, we help to provide a better base for size of our JavaScript. It’s still critical to put some friction on the build and deploy process, though. For one, our install approach is only limiting npm modules, not really our own code. We also don’t really know the exact shape of our bundles at install—that comes later.</p>
  39. <p>For webpack-driven projects, you can take advantage of webpack’s <a href="https://webpack.js.org/configuration/performance/">performance hints</a>. There are two hints available to us: <code>performance.maxEntrypointSize</code> and <code>performance.maxAssetSize</code>. <code>performance.maxEntrypointSize</code> lets us set a limit for all webpack produced assets for a given route. <code>performance.maxAssetSize</code> lets us set a limit for any individual webpack produced assets.</p>
  40. <p>By default, the hints are just that—hints. They show up as warnings but don’t do anything concrete. You can change that by setting the <code>peformance.hints</code> property to <code>error</code>.</p>
  41. <p>So, given the following configuration, webpack would throw errors whenever an individual asset exceeds 100kB or all total assets for a given route exceed 150kB.</p>
  42. <div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span>module</span>.<span>exports</span> <span>=</span> {
  43. <span>//...
  44. </span><span/> <span>performance</span><span>:</span> {
  45. <span>hints</span><span>:</span> <span>'error'</span>,
  46. <span>maxEntrypointSize</span><span>:</span> <span>100000</span>,
  47. <span>maxAssetSize</span><span>:</span> <span>150000</span>
  48. }
  49. };
  50. </code></pre></div>
  51. <figure class="banner">
  52. <img src="https://timkadlec.com/images/friction-webpack.png" alt="webpack&amp;rsquo;s performance hints let us throw errors if individual assets are too large, or if all assets for a given route get too heavy."/> <figcaption>
  53. <p>webpack’s performance hints let us throw errors if individual assets are too large, or if all assets for a given route get too heavy.</p>
  54. </figcaption>
  55. </figure>
  56. <p>If you’re not using webpack, or if you are and still want to augment these hints, we can also introduce some bundle size checking at the pull request or deploy levels. <a href="https://github.com/siddharthkp/bundlesize">Bundlesize</a> is a common choice here.</p>
  57. <p>With Bundlesize, we setup maximum sizes for each bundle we want to track. Then we can run Bundlesize against those limits on every pull request or during our continuous integration process to stop us from deploying if any of those bundle sizes have been exceeded.</p>
  58. <figure class="banner">
  59. <img src="https://timkadlec.com/images/friction-bundlesize.png" alt="Bundlesize will check each bundle against the limits we set so that we can break the build if any of those limits are exceeded."/> <figcaption>
  60. <p>Bundlesize will check each bundle against the limits we set so that we can break the build if any of those limits are exceeded.</p>
  61. </figcaption>
  62. </figure>
  63. <h2 id="building-with-friction">Building with friction</h2>
  64. <p>Healthy friction in our processes, paired with automation and reporting where appropriate, can have a substantial impact on what we ship. When we force ourselves to take these moments to consider the implications of what we’re about to add to our codebase, when we make it hard to add more bloat to our applications by default, we not only change the way we build, but we change the way we <em>think</em> about building. It’s the observer effect applied to the way we code.</p>
  65. <p>When we have to consider the weight of every module we add to our project (or which vulnerabilities are included or what accessibility concerns they bring along), we start to inherently pay a little more attention to at least a part of performance every single day. It won’t magically fix all our performance woes by itself, but it certainly gets us pointed in the right direction.</p>