Music from Our Tube; “In the Dirt” by S. Carey

I’ve had “In the Dirt” in my YouTube favorites for some time, but forgot about it until I heard it at the end of an episode of “The Good Wife“;

S. Carey – "In The Dirt" (Official Video)

Watch this video on YouTube or on Easy Youtube.

Sean Carey is the drummer and supporting vocalist of Bon Iver, which puts this song right into context. The almost harsh percussive force and sometimes weird rhythms contrast beautifully with the piano and the layered voices. And there’s some warm woodwind & viola in to even things out.

Some HTML DOM parsing gotchas in PHP’s DOMDocument

Although I had used Simple HTML DOM parser for WP DoNotTrack, I’ve been looking into native PHP HTML DOM parsing as a possible replacement for regular expressions for Autoptimize as proposed by Arturo. I won’t go into the performance comparison results just yet, but here’s some of the things I learned while experimenting with DOMDocument which in turn might help innocent passers-by of this blogpost.

  • loadHTML doesn’t always play nice with different character encodings, you might need something like mb_convert_encoding to work around that.
  • loadHTML will try to “repair” your HTML to make sure an XML-parser can work with it. So what goes in will not come out the same way.
  • loadHTML will spit out tons of warnings or notices about the HTML not being XML; you might want to suppress error-reporting by prepending the command with an @ (e.g. @$dom->loadHTML($htmlstring);)
  • If you use e.g. getELementsByTagName to extract nodes into a seperate DomNodeList and you want to use that to change the DomDocument can result in … unexpected behavior as the DomNodeList gets updated when changes are made to the DomDocument. Copy the DomNodes from the DomNodeList into a new array (which will not get altered) and iterate over that to update the DomDocument as seen in the example below.
  • removeChild is a method of DomNode, not of DomDocument. This means $dom->removeChild(DomNode) will not work. Instead invoke removeChild on the parent of the node you want to remove as seen in the example below
// loadHTML from string, suppressing errors
$dom = new DOMDocument();
@$dom->loadHTML($html);

// get all script-nodes
$_scripts=$dom->getElementsByTagName("script");

// move the result form a DomNodeList to an array
$scripts = array();
foreach ($_scripts as $script) {
   $scripts[]=$script;
}

// iterate over array and remove script-tags from DOM
foreach ($scripts as $script) {
   $script->parentNode->removeChild($script);
}

// write DOM back to the HTML-string
$html = $dom->saveHTML();

Now chop chop, back to my code to finish that performance comparison. Who know what else we’ll learn ;-)

Music from Our Tube; A/T/O/S – “What I Need”

Last Saturday shortly before midnight I was listening to the radio, my ear-buds plugged in tightly and slowly falling asleep only for this tune to awaken my auditive senses, urging me to wake up and really listen;

A/T/O/S – What I Need (DEEP MEDi MUSIK)

Watch this video on YouTube or on Easy Youtube.

A/T/O/S or “A Taste of Struggle” is the project of Belgian producer Truenoys and singer Amos. Their debut album is released on Deep Medi Musik.

Triphop, dark soul and silence as integral part of the music (à la James Blake). Not your average summertime sing-a-long, but a very impressive track nonetheless!

Music from Our Tube; Fatboy Slim & Co raving like it’s 1999

I’ve been to a couple of tech-house events back in the days and I must say this 2013 track does bring back some of those memories;

Fatboy Slim & Riva Starr Ft. Beardyman – Eat, Sleep, Rave, Repeat (Lyric Video)

Watch this video on YouTube or on Easy Youtube.

Eat, sleep, rave, repeat by Fatboy Slim, Riva Starr and Beardyman. Coz it’s Friday after all!

How to keep Autoptimize’s cache size under control (and improve visitor experience)

Confession time: Autoptimize does not have its proper cache purging mechanism. There are some good reasons for that (see below) but in most cases this is not something to worry about.

Except when it is something to worry about off course. Because in some cases the amount of cache-files generated by Autoptimize can grow to several Gigabytes. Why, you might wonder? Well, for each page being loaded Autoptimize aggregates all JS (and CSS) calculates the hash of that string and checks if an optimized version is in cache using that hash. If there is a difference (even if just a comma), the hash is not the same and the aggregated CSS/ JS is cached seperately. This behavior typically is caused by plugins that generate javascript-variables (or CSS-selectors) that are specific for each page (or even worse, for each page request). That does not only lead to a huge amount of files in the cache, but also impacts visitors as their browsers will have to request a different optimized CSS- or JS-file for each page instead of reusing the same file for several pages.

This is what you can do if you want a healthier cache both from a server- and visitor-perspective (based on JavaScript, but the same principle applies to CSS);

  1. Open two similar pages (posts).
  2. View source of the optimized JavaScript in those two pages.
  3. Copy the source of each to a seperate file and replace all semi-colons (“;”) with semi-colon+linefeed (“;\n”) in both files.
  4. Execute an automatic comparison between the two using e.g. diff (or “compare” in Notepad++), this should give you one or more lines that will probably be almost the same, but not exactly (e.g. with a different nonce or a postid in them).
  5. Now disable JS optimization and look for similar strings in the inline and the external JavaScript.
  6. If you find it in the inline JavaScript, try to identify a unique string in there (the name of a specific variable, probably) and write that down. If the variable JS is in a file, jot down the filename.
  7. Go to the autoptimize settings page and make sure the advanced settings are shown.
  8. Now add the strings or filenames from (6) to “Exclude scripts from Autoptimize:” (which is a comma-seperated list).
  9. Re-enable JS optimization.
  10. Save settings & clear cache.

This does require some digging, but the advantages are clear; a (much) smaller cache-size on disk and better performance for your visitors. Everyone will be so happy, people will want to hug you and there will be much rejoicing, generally.

So why doesn’t Autoptimize have automatic cache pruning? Well, the problem is a page caching layer (which could be a browser, a caching reverse proxy or a wordpress page caching plugin) contains pages that refer to the aggregated JS/CSS-files. If those optimized files were to be automatically removed while the page would remain in the page caching layer, people would get the cached page without any JS- or CSS-files being available. And as I don’t want Autoptimize to break your pages, I didn’t include a automatic cache purging mechanism. But if you have a bright idea of how this problem could be tackled, I’d be happy to reconsider, off course!

Music from Our Tube; Child of Lov

I just “discovered” Child of Lov (a.k.a. Martijn Teerlinck, a young man who was born in Belgium/ Flanders but was raised in the Netherlands) and then learned the guy had died after heart surgery 2 months ago. Damn!

The Child of Lov – "Give Me" (Official Music Video)

Watch this video on YouTube or on Easy Youtube.

His debut album was released in 2013 and got raving reviews from all over the world. If you’re into this kind of thing, listen to the full album here and you’ll understand the fuss. Such a pity.