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.

4 年之前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. title: You’re Missing the Point of Server-Side Rendered JavaScript Apps
  2. url: http://tomdale.net/2015/02/youre-missing-the-point-of-server-side-rendered-javascript-apps/
  3. hash_url: b0d05411a11c9944bff0dee0fb7baa71
  4. <p>There is a lot of confusion right now about the push to render JavaScript applications on the server-side. Part of this has to do with the awful terminology, but mostly it has to do with the fact that it’s a fundamental shift in how we architect and deploy these apps, and the people peddling this idea (myself included) have not done a great job motivating the benefits.</p>
  5. <p>First, let me make something as clear as I can: <strong>the point of running your client-side JavaScript application on the server is not to replace your existing API server</strong>.</p>
  6. <p>Unfortunately, when most people think of client-JavaScript-running-on-the-server, they think of technologies like Meteor, which <em>do</em> ask you to write both your API server (responsible for things like authentication and authorization) and your client app in the same codebase using the same framework. Personally, I think this approach is a complexity and productivity disaster, but that deserves its own blog post. For now, just know that when I talk about server-side rendering in the context of Ember, this is <em>not</em> what I’m talking about.</p>
  7. <p>This post was actually inspired by Old Man Conery, who yesterday on Twitter insisted that us JavaScript hipsters should get off his lawn and just use a traditional, server-rendered application if we want SEO.</p>
  8. <blockquote class="twitter-tweet" lang="en"><p><a href="https://twitter.com/mixonic">@mixonic</a> <a href="https://twitter.com/tomdale">@tomdale</a> I can't believe this conversation. Stick Ember in where it's not needed then figure out how to work SEO because Ember.</p>— Rob Conery (@robconery) <a href="https://twitter.com/robconery/status/563034855945293827">February 4, 2015</a></blockquote>
  9. <p>Rob and I have a good relationship and I like to tease him, but in this case I think he represents an extremely common worldview that is helpful to study, because it can show us where the messaging around client-side JavaScript apps breaks down. “These developers just jammed JavaScript in here for no darn reason!” is something I hear <strong>very</strong> often, so it’s important to remind ourselves why people pick JavaScript.</p>
  10. <h3>Why Do People Love Client-Side JavaScript?</h3>
  11. <h5>The Beauty of the Language</h5>
  12. <p>LOL j/k</p>
  13. <h5>Coherence</h5>
  14. <p>All modern websites, even server-rendered ones, need JavaScript. There is just a lot of dynamic stuff you need to do that can only be done in JavaScript. If you build your UI entirely in JavaScript with a sane architecture, it’s easy to reason about how the different dynamic behavior interacts.</p>
  15. <p>The traditional approach of sprinkling JavaScript on top of server-rendered HTML was fine for a long time, but the more AJAX and other ad hoc dynamic behavior you have, the more it turns into a giant ball of mud.</p>
  16. <p>Worst of all, you now have state and behavior for the same task—UI rendering—implemented in two languages and running on two computers separated by a high-latency network. Congratulations, you’ve just signed up to solve one of the hardest problems in distributed computing.</p>
  17. <h5>Performance</h5>
  18. <p>Client-side JavaScript applications are damn fast. In a traditional server-rendered application, every user interaction has to confer with a server far away in a data center about what to do. This can be fast under optimal scenarios, if you have a fast server in a fast data center connected to a user with a fast connection and a fast device. That’s a lot of “ifs” to be hanging the performance of your UI on.</p>
  19. <p>Imagine if you were using an app on your mobile phone and after every tap or swipe, nothing happened until the app sent a request over the network and received a response. You’d be pissed! Yet this is exactly what people have historically been willing to put up with on the web.</p>
  20. <p>Client-side rendered web apps are different. These apps load the entire payload upfront, so once it’s booted, it has all of the templates, business logic, etc. necessary to respond instantly to a user’s interactions. The only time it needs to confer with a server is to fetch data it hasn’t seen before–and frameworks like Ember make it trivially easy to show a loading spinner while that data loads, keeping the UI responsive so the user knows that their tap or click was received. If the data <em>doesn’t</em> need to be loaded, clicks feel insanely fast.</p>
  21. <p>There’s a misconception that client-rendered JavaScript apps are only useful for more “application-y” sites and not so-called “content” sites; <a href="https://www.vine.co/">Vine</a> and <a href="http://www.bustle.com/">Bustle</a> are two frequently cited examples. But client-side routing, where you have all of the templates and logic available at the moment of the user’s click, provides performance benefits to <em>every</em> site, and users are beginning to become conditioned to the idea that interactions on the web should be near-instaneous. If you don’t start building for client-side rendering today, my bet is your site is going to feel like a real clunker in just a few years’ time. This is going to be the norm for all sites, and in the not-too-distant-future, server-rendered stuff will feel very “legacy.”</p>
  22. <h3>Why Do People Hate Client-Side JavaScript?</h3>
  23. <p>Everyone is different, and if you asked 100 different haters I’m sure they would have 100 different flavors of Hatorade™. But there are some real tradeoffs to using client-side JavaScript.</p>
  24. <h5>Performance</h5>
  25. <p>Yes, one of the biggest advantages of client-side rendering is also its biggest weakness. The performance of JavaScript apps is amazing–once all of that JavaScript has finished loading. Until then, you usually just see a loading spinner. That sucks on the web, where people are used to seeing something interesting within a second or two of clicking on a link. So while subsequent clicks will be lightning fast, unless you do a lot of manual work, the initial experience is going to suffer.</p>
  26. <h5>Unpredictable Performance</h5>
  27. <p>At TXJS many years ago, I had a long chat with Dan Webb at Twitter about client-side vs. server-side rendering. He had just finished working on migrating Twitter away from their 100% client-side app back to a more traditional server-rendered approach, and I was mad <a href="https://blog.twitter.com/2012/improving-performance-on-twittercom">about their blog post</a>. In my experience, client-side JavaScript apps were almost always faster, and the new Twitter site felt like a regression. I just didn’t understand where they were coming from.</p>
  28. <p>Thankfully, Dan helped me see the bubble I was living in. For me, who always had a relatively modern device, this stuff was super fast. But Dan explained that they had users all around the world clicking on links to Twitter, some of them in internet cafes in remote areas running PCs from 1998. They were seeing times of over 10 seconds just to download, evaluate, and run the JavaScript before the user saw anything. (Forgive me for not having exact numbers, this was years ago and there was definitely beer involved.)</p>
  29. <p>Say what you will about server-rendered apps, the performance of your server is much more predictable, and more easily upgraded, than the many, many different device configurations of your users. Server-rendering is important to ensure that users who are not on the latest-and-greatest can see your content immediately when they click a link.</p>
  30. <h5>Crawlers</h5>
  31. <p>Client-side rendered apps require, by definition, a JavaScript runtime to work correctly. There is a common misconception that screen readers and other accessibility tools don’t work with JavaScript, but this is just flat-out not true, so don’t let people shame you for using JavaScript for this reason.</p>
  32. <p>That being said, there is a vast world of tools out there that consume and scrape content delivered over HTTP that don’t implement a JavaScript runtime. I don’t think it’s worth going out of our way to cater to this case, at least not at the sake of developer productivity. But if we can solve the problem and make these JavaScript apps available to JavaScript-less clients, why the hell not? Frameworks like Ember should be solving hard problems like this so developers can get back to work.</p>
  33. <h3>Ember FastBoot</h3>
  34. <p>The project I’ve been working on, Ember FastBoot, is designed to bend the curve of these tradeoffs. Rather than replace your API server, it replaces whatever you’re using now to serve static HTML and JavaScript (usually something like nginx or a CDN like CloudFront).</p>
  35. <p>In a traditional client-side rendered application, your browser first requests a bunch of static assets from an asset server. Those assets make up the entirety of your app. While all of those assets load, your user usually sees a blank page.</p>
  36. <p>Once they load, the application boots and renders its UI. Unfortunately, it doesn’t yet have the model data from your API server, so it renders a loading spinner while it goes and fetches it.</p>
  37. <p>Finally, once the data comes in, your application’s UI is ready to use. And, of course, future interactions will be very, very snappy.</p>
  38. <p><img src="http://tomdale.net/wp-content/uploads/2015/02/traditional-client-side.png" alt="traditional-client-side" class="aligncenter size-full wp-image-972"/></p>
  39. <p>But what if we could eliminate the blank page step entirely, without sacrificing any of the benefits of client-side JavaScript?</p>
  40. <p>We do this by running an instance of your Ember app in Node on the server. Again, this doesn’t replace your API server—it replaces the CDN or whatever else you were using to serve static assets.</p>
  41. <p>When a request comes in, we already have the app warmed up in memory. We tell it the URL you’re trying to reach, and let it render on the server. If it needs to fetch data from your API server to render, so long as both servers are in the same data center, latency should be very low (and is under your control, unlike the public internet).</p>
  42. <p><strong>The very first thing the user sees is HTML and CSS</strong>, avoiding the blank page and loading spinner. Even users with slow JavaScript engines quickly get what they’re after, because no JavaScript has loaded yet. Once the HTML and CSS is delivered, the static JavaScript app payload is delivered, as before. Once the JavaScript loading has “caught up” to the HTML and CSS, your application takes over further navigation, giving you the snappy interaction of traditional client-side apps with the snappy first load of server-rendered pages.</p>
  43. <p>And, of course, for those who don’t have any JavaScript at all (shoutout to all my Lynx users), basic functionality will just work, since Ember generates standard URLs and <code>&lt;a&gt;</code> tags and avoids the <code>onclick=</code> jiggery pokery. More advanced things like D3 graphs won’t work, obviously, but hey, better than nothing.</p>
  44. <p><img src="http://tomdale.net/wp-content/uploads/2015/02/fastboot.png" alt="fastboot" class="aligncenter size-full wp-image-979"/></p>
  45. <p>We are also investigating further optimizations, like embedding the model data right into the initial JavaScript payload, so you avoid another roundtrip to the API server for frequently accessed model data.</p>
  46. <p>These are early days, and making this work relies heavily on having a single, conventional architecture for your web applications, which Ember offers.</p>
  47. <p>Ultimately, this isn’t about you replacing your API server with Ember. I don’t think I would ever want that.</p>
  48. <p>Instead, client-side rendering and server-side rendering have always had performance tradeoffs. <strong>Ember’s FastBoot is about trying to bend the curve of those tradeoffs, giving you the best of both worlds.</strong></p>
  49. <p><em>Big thanks to Bustle for sponsoring our initial work on this project. Companies that sponsor indie open source work are the best.</em></p>