5

The task sounds trivial but bear with me.

These are the buttons I'm working with:

  • Google (+1)
  • Facebook (Like)
  • Twitter (Tweet)
  • LinkedIn (Share)

With a little testing on webpagetest.org I found that it's incredibly inefficient if you grab the snippet from each of these services to place these buttons on your page. In addition to the images themselves you're also effectively downloading several JavaScript files (in some cases multiple JavaScript files for just one button). The total load time for the Facebook Like button and its associated resources can be as long as 2.5 seconds on a DSL connection.

Now it's somewhat better to use a service like ShareThis as you can get multiple buttons from one source. However, they don't have proper support for Google +1. If you get the code from them for the Google +1 button, it's still pulling all those resources from Google.

I have one idea which involves loading all the buttons when a generic looking "Share" button is clicked. That way it's not adding to the page load time. I think this can be accomplished using the code described here as a starting point. This would probably be a good solution but I figured I'd ask here before going down that road.

3
  • 1
    Could you potentially load them after the page load with an ajax call?
    – HellaMad
    Feb 16, 2012 at 23:10
  • @DC_. Yes, perhaps. At least 3 of the buttons above also offer iframe solutions (I'm not sure about LinkedIn yet). But I could make an AJAX call to a page on my site containing nothing but the buttons. I'm already including jQuery so it'd be an extremely small amount of code to accomplish. Good call. Feb 16, 2012 at 23:28
  • You should not embed them directly anyways for privacy reasons. The legality of this in some countries(for example Germany) is also dubious. Feb 16, 2012 at 23:28

2 Answers 2

6

I found one possible solution if you don't care about the dynamic aspect of these buttons. In other words, if you don't care to show how many people have +1'd or liked your page, you can just use these links...

https://plusone.google.com/_/+1/confirm?hl=en&url={URL}
http://www.facebook.com/share.php?u={URL}
http://twitter.com/home/?status={STATUS}
http://www.linkedin.com/shareArticle?mini=true&url={URL}&title={TITLE}&summary={SUMMARY}&source={SOURCE}

You'd just have to insert the appropriate parameters. It doesn't get much simpler or lightweight than that. I'd still use icons for each button of course, but I could actually use CSS sprites in this case for even more savings. I may actually go this route.


UPDATE

I implemented this change and the page load time went from 4.9 seconds to 3.9 seconds on 1.5 Mbps DSL. And the number of requests went from 82 to 63.

I've got a few more front-end optimizations to do but this was a big step in the right direction.

3
  • Cool, you learn something new every day. It's nice that all the services have RESTful links you can use to avoid the 3 second hit, which is almost criminal. Plus one for this approach.
    – Matt
    Feb 17, 2012 at 19:54
  • Yep, I like this approach. I had to run it by someone first since this isn't my site... it's just a site I'm working on. But it's implemented now and I've updated my answer with details. Feb 17, 2012 at 20:14
  • 1
    I know this works for twitter - twitter.com/share?url=... - see more instructions here: dev.twitter.com/docs/tweet-button
    – yoavram
    Dec 11, 2012 at 7:47
2

I wouldn't worry about it, and here's why: if the websites in question have managed their resources properly - and, come on, it's Google and Facebook, etc... - the browser should cache them after the first request. You may see the effect in a service where the cache is small or disabled, but, in all likelihood, all of your clients will already have those resources in their cache before they ever reach your page.

And, just because I was curious, here's another way:

Here's the snippet of relevant code from StackOverflow's facebook share javascript:

facebook:function(c,k,j){k=a(k,"sfb=1");c.click(function(){e("http://www.facebook.com/sharer.php?u="+k+"&ref=fbshare&t="+j,"sharefacebook","toolbar=1,status=1,resizable=1,scrollbars=1,width=626,height=436")})}}}();

Minified, because, hey, I didn't bother to rework the code.

It looks like the StackOverflow engineers are simply calling up the page on click. That means that it's just text until you click it, which dynamically pulls everything in lazily.

2
  • Some of the resources are cached, but many of them aren't. It's insane but a repeat view on a page of my site just seconds later takes 3 seconds. The social media resources account for at least 1 second of that. I can see all of this with webpagetest.org. Feb 16, 2012 at 23:22
  • Thanks Matt, that code from StackOverflow led to me finding the appropriate links for all 4 services, which I added to my answer. I'm still not sure if I'll go this route, but I might. Feb 17, 2012 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.