3

I am trying to optimize my website regarding PageSpeed Insights and I am stuck because of the embedded Twitter timeline which lowers my score...

1) Optimize images: Even though I disabled the auto-expand pictures feature (twitter widget page), i still have the error messages:

  • Compressing https://pbs.twimg.com/…mages/1314024178/blablabla_bigger.jpg could save 13.8KiB (86% reduction)
  • Compressing https://pbs.twimg.com/…mages/1314024178/blablabla_normal.jpg could save 5,9 KiB (83% reduction)

2) Leverage browser cache: Even though my .htaccess seems good, i still have the following error:

Please help me!

Thank you

Laurent

2 Answers2

1

Unfortunately the Twitter timeline is a 3rd party service and is outside of your control. You won't be able to improve your PageSpeed score because you are consuming the Twitter timeline and it is not on your own servers.

You could possibly consider asynchronously loading the timeline - if you could provide more implementation detail I might be able to help!

Deano
  • 2,805
  • 3
  • 29
  • 42
  • Hi, thank you for your answer. I guess I could put the scrit to excute after my last script. Here is the code: – Laurent Milleron Jun 08 '15 at 15:42
1

Deano is spot on here:

Unfortunately the Twitter timeline is a 3rd party service and is outside of your control. You won't be able to improve your PageSpeed score because you are consuming the Twitter timeline and it is not on your own servers.

Caution: Borderline over-engineering below this line, but still fun to implement :)


I've been working a lot with WebPagetest lately and came up with an interesting "test hack" for deferring these 3rd party assets. Keep in mind that the purpose of this is to "exclude" resources being "tested" that you have no control over, yet continue to skew your results.

<div id="twitter-timeline"></div>

<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script>
  $(document).ready(function() {
    var createTwitterTimeline = function(screenName, appendToIdSelector) {
      if (!$(appendToIdSelector).length) {
        console.log(appendToIdSelector + ' not found');
        return;
      }

      var block = $('<div />');
      var a = $('<a />').attr('class', 'twitter-timeline').attr('href', 'https://twitter.com/' + screenName).text('Tweets by @' + screenName);
      var script = $('<script />').attr('async', 'true').attr('src', 'https://platform.twitter.com/widgets.js').attr('charset', 'utf-8');

      block.append(a);
      block.append(script);

      $(appendToIdSelector).html(block.html());
    }

    // getParameterByName (http://stackoverflow.com/a/901144/901156)
    var getParameterByName = function(name, url) {
      if (!url) url = window.location.href;
      name = name.replace(/[\[\]]/g, "\\$&");
      var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
          results = regex.exec(url);
      if (!results) return null;
      if (!results[2]) return '';
      return decodeURIComponent(results[2].replace(/\+/g, " "));
    }

    var defer = parseInt(getParameterByName('defer'));

    setTimeout(function() {
      createTwitterTimeline('Interior', '#twitter-timeline');
    }, !isNaN(defer) ? defer : 0);
  });
</script>

What this does:

  1. Waits for DOM to be completely loaded
  2. Recreates HTML markup in-memory for User Embedded Timeline
  3. Looks for defer query string parameter and sets the timeout to this value (else 0 if value is not a number).

The last step basically holds off loading the timeline HTML (and consequently anything from pbs.twimg.com) for a minimum amount of time necessary for WebPagetest to finish running its test.

With this "implementation" you then pass the following URL to WebPagetest, Google PageSpeed Insights, etc:

https://example.com/my-page.html?defer=5000

Of course, at the end of the day, you could also just use the Block tab at WebPagetest and add pbs.twimg.com to the list of requests to ignore. I don't quite see where Google PageSpeed Insights provides you with this option, however. The implementation example I provided above may be useful in creating a more holistic, tool-agnostic approach.

And if you think this is "over-engineering" (or if you simply want to get into more hard-core testing with automation, etc.) you should check out their Scripting Documentation:

Pagetest has a scripting capability that lets you automate a multi-step test (for example, logging into a site or sending an e-mail message). The scripts are contained in files where a single file constitutes a browser session (the browser will be closed after the script is complete). The files are plain text files and can be edited by any text editor.

Matt Borja
  • 1,509
  • 1
  • 17
  • 38