Swizec Teller - a geek with a hatswizec.com

    A lesson about client-side templating

    Preona Muffin Making

    The past few months have seen an explosion of client-side MVC frameworks. Wikipedia lists eleven of these things! And yet even a year ago the majority of developers was perfectly content with jQuery.

    But as browser apps become ever more powerful it only makes sense to realize callback soup isn't fun and hey, wasn't there some paradigm we use on [servers](http://en.wikipedia.org/wiki/Server%28computing%29 "Server (computing)") to make this web insanity work?_ Yeah, there is.

    A few months ago I gave a talk entitled Tools that get you laid at WebCamp Ljubljana. The basic premise was that client-side MVC is so insanely awesome you are sure to get laid for using it. The main benefits I talked about were:

    1. Server is just a DB interface, means less melting servers
    2. Effect similar to progressive loading, means users see something immediately - makes waiting easier
    3. Using powerful client machines to do the heavy UX lifting
    4. I may have had some other points :P

    The lesson

    The past four days have taught me an important lesson though - perhaps none of this is true.

    Since Thursday my blog has seen two months' worth of normal traffic - more about the shiny stats tomorrow - it was only natural that poor ol' wordpress would crumble under the pressure ... right?

    Well, actualy, no, it totally shouldn't have. There's all this fancy caching going on that what readers see are pretty much just static pages. There's no more PHP or server involved!

    A stats screenie

    But none of that helped. Even redirecting the URL with most traffic straight to the stored static page didn't help. Something strange was happening. Something strange happens every single time this blog gets some traffic. This time it was particularly bad so I investigated.

    Turns out the problem is in the small bit of client-side templating I do in my footer.

    Because the pages are static, loading my latest foursquare checkin, the last four instagram pics, and the list of my github repos must happen client-side. It would be stupid to generate all this server-side every time somebody visits my site.

    To achieve this I'm using jquery template, it's a bit archaic and I may have started working on the footer before finding out about backbone and other cool MVC frameworks ... but it works and it's so small I don't think it would even benefit much from using a framework. The basic premise of most javascript templating is simply substring replacement, like so:

    <img title="${the_title}" src="${url_var}" alt="" />

    Everything marked as a variable gets replaced with its value and everyone can be happy.

    Try to guess what happens with that src="\\\${url_var}" bit in there. Yep, the browser tries to load it. Firing a request to something that always returns at least a 404 after wordpress decides that, hey, this isn't something I know of!

    This means that the url is never cached either server-side _or _client-side!

    Turns out my blog was firing two such requests every time somebody came to the page. Every. Single. Time! In the latest case this meant 8 useless request per second at its peak. To the point most of my site just started throwing a 500 error and only that one redirected static page was still up ...

    All I have to do now is find a permanent solution. Any ideas?

    Enhanced by Zemanta

    Did you enjoy this article?

    Published on December 19th, 2011 in Client-side, Clients, JavaScript, jQuery, Server, Uncategorized, Web page

    Learned something new?
    Want to become an expert?

    Here's how it works 👇

    Leave your email and I'll send you thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

    Join Swizec's Newsletter

    And get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.

    "Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

    ~ Ashish Kumar

    Join over 14,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. ✌️

    Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

    Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

    Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

    Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

    Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

    By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️

    Created bySwizecwith ❤️