No more prefixing

As I was tinkering with the code for this site, as one does on a Sunday afternoon, I came to realise that I hadn’t done some proper, old-school browser testing in Internet Explorer for a while. I had been doing a lot of refactoring in my gulpfile.js and exchanged some packages for others, most notably the package for minifying my CSS. I had been using gulp-cssnano for quite a while without giving it any thought (I can’t even remember which package I had use prior to that one).

Opening the site up in Internet Explorer 9 on Windows 7 revealed that a few things like font declarations didn’t work at all and after some debugging it turned out that cssnano was indeed the culprit. I didn’t delve too deeply into the cause of the issue, but instead ended up switching to gulp-clean-css, since it did the same thing without breaking IE compatibility. Good enough!

While debugging in IE, something else did catch my eye, though. Man, there sure is a lot of prefixed properties in here! Most of them was for flexbox, which I’m only using on a few places like the footer and the main layout to get a sticky footer on pages with less content. Dropping these would mean that IE 10, to name one, would not get the same slick layout (well, slick-ish, you know…) as modern browsers. And you know what? That’s just fine. The upside was that I got to trim away a few packages in my node_modules folder, not to mention that I got to delete a few extra lines in my gulpfile.js. That felt really good!

I did some checking and since I use display: inline-block; on elements that normally would be flex-items in supporting browsers to at least get them to appear next to each other, things looked pretty ok. Nothing looked too broken and all the content was fully accessible.

So what am I getting at with all this rambling, then? Well, most vendor prefixes are on their way out the door and to my knowledge, new prefixes are not coming in anymore. If we just apply some progressive enhancement thinking to our CSS as well, we can cut down on complexity in our tool stacks (and you know that’s normally not a thing these days!), plus we’re also cutting down on the amount of code our users are downloading.

Almost all major browsers are evergreen today and feature support is rapidly progressing across the board. Vendor prefixes was an unfortunate hiccup along the way, that got picked up by developers and misused. It’s time we left that stuff behind us.

On browser support for evergreen websites

“Pixel perfect” meant “this website looks like this graphic”. Designers reacted in horror that users might increase their text size. Browser compatibility ranged from “Best Viewed in Internet Explorer” badges to the development of two separate sites, one for each browser in order to ensure the same design was shown to each.

I remember those days back in the 90s and early 2000s. While it was fun to tackle such challenges, I still shudder thinking back knowing what I know now.

We don’t have 99% browser support for border-radius, or for pretty much anything introduced in the last few years. If you think you need 99% support to use any CSS you probably had best stop using CSS altogether.

So true. Applying progressive enhancement in all your work makes your job a lot easier in the end. The challenge is selling the idea to your client. In my experience, transparency is key. Be open with how you communicate your ideas and what benefits it will offer. Telling them that a majority of their users will receive the high-end experience, but at the same time a wider audience can be reached, helps a lot.

Vim and Apple’s Touch Bar

Since we basically lost the escape key with the new MacBook Pro models that came out at the end of 2016, Vim users needed a solid backup plan if they were to get one of the new laptops. Harry Roberts has put together some really nice things to consider with regards to the escape key:

For almost as long as I’ve been using Vim–which is a long time now–I’ve been using jj and jk to leave Insert mode. These mappings are on the Home Row, so always easy to reach, and the letter pairs very rarely (if ever) occur in the English language. If there were words that contained jj and jk next to each other then I would be flung straight into Normal mode any time I tried to write them. (The reason I haven’t mapped kk to Escape is because it does occur within words, e.g. bookkeeper.

What did I do? Well, as a macOS Sierra user, I just followed the advice of a former colleague and remapped my Caps Lock key.

Update: As of macOS Sierra 10.12.1, the Caps Lock -> Escape remapping can be done natively in the Keyboard System Preferences pane! To remap without any 3rd party software, do the following:

  • Open System Preferences and click on ‘Keyboard’
  • Click on ‘Modifier Keys…’
  • For ‘Caps Lock (⇪) Key’, choose ‘⎋ Escape’
  • Click ‘OK’

(See screenshot for reference).

Ending the dyslexia legibility experiment

Back in February, I decided to do a little experiment right here on my journal web site, whereby I added a simple JavaScript function to toggle styling that should increase the legibility for people with dyslexia. Partly, it was for me to do some JavaScript hacking, but also to try and get some feedback about the validity of the write up “A Typeface For Dyslexics? Don’t Buy Into The Hype”.

Nine months have passed since I published that post, and I feel it’s time to remove the toggle from my web site. While I did get some amount of response from people suffering from dyslexia, I felt it wasn’t quite enough. I’ll update the original post in due time and move the experiment into its own lab page.

I’d like to thank those to gave feedback and helped me do this little experiment (you know who you are 😊).

Cache busting with Jekyll and Gulp

Since I love tinkering with my journal, updates to both my stylesheets and JavaScript files are quite frequent. Up till now, I’ve just let Gulp generate my files and then I included this in my Jekyll templates in a hard-coded fashion. But it then hit me that maybe I should do something to make sure that recurring visitors always get the latest version of my JavaScript and CSS, if they’ve been changed.

What is cache busting?

In short, cache busting is a way to make sure that a client downloads your very latest version of a file. The simplest way to do this is to either give the file a random name, like ab459ef32da.css or, in my opinion, the nicer variant of adding a query string along the lines of styles.css?version=ab459ef32da. Then, each time you do a change to this file, you make sure that the random string changes, and you’ve successfully busted that cache.

Update (2016-02-26): Apparently, according to GTmetrix, some proxies do not cache static resources with a query string in the URL. They recommend that you encode the unique string into the file names themselves.

Making Gulp and Jekyll work together

As mentioned, I use Gulp to minify and concatenate both my CSS and JavaScript. The output files are hardcoded in my Gulpfile.js so all Jekyll needed was the paths to both files in the templates and be done with it.

<link rel="stylesheet" href="/gui/css/styles.css">
<script src="/gui/js/main.js" defer></script>

In order to get some cache busting going, I needed the following:

  • Give my generated CSS and JavaScript files a unique string everytime they are updated with Gulp
  • Make this string available to Jekyll somehow to append it when linking to the files in the Jekyll template
  • Automate it so I don’t have to think about it anymore

Since I only want the string to change when I’ve done something to either the JavaScript or CSS, the best approach would be to use the MD5 checksum from each generated file to indicate when something has changed. So I needed some sort of Gulp plugin to grab the MD5 and then do something with it. But first, we need to sort out how to get this data into Gulp.

Enter the data folder

Jekyll has this nifty feature that lets you define custom data.

In addition to the built-in variables available from Jekyll, you can specify your own custom data that can be accessed via the Liquid templating system.

Jekyll supports loading data from YAML, JSON, and CSV files located in the _data directory.

–– Jekyll documentation page on data files

Perfect! This will allow us to pass information into our Jekyll templates. Reading on in the Jekyll documentation lets us know that if we create _data/cache_bust.yml, the contents of this file will be available in Jekyll via {{ site.data.cache_bust }}. If the cache_bust.yml just contains a string, the aforementioned Jekyll tag will output just that. That’s all we need for this job.

Getting Gulp to write an MD5 hash to the data folder

There’s a plethora of cache busting plugins for Gulp over at npmjs.com. But all I need is something that grabs the MD5 checksum of my generated files and write that string to a file in the _data folder. The closest match for me turned out to be gulp-hashsum.

So this is my Gulp task for building the CSS.

// Process stylesheets
gulp.task('css', function () {
  return gulp.src(paths.css)
    .pipe(plumber({
      errorHandler: onError
    }))
    .pipe(sourcemaps.init())
    .pipe(autoprefixer({
      browsers: ['last 2 versions'],
      cascade: false
    }))
    .pipe(concat(paths.cssOutput))
    .pipe(cssnano())
    .pipe(gulpif(!isProduction, sourcemaps.write('.')))
    .pipe(gulp.dest(paths.cssDest));
});

In short, I’m using Gulp to do autoprefixing, then concatenate all my files, minify them, and lastly, if I’m not on production, add sourcemaps. So first we install gulp-hashum.

$ npm install gulp-hashsum --save-dev

Require it in our Gulpfile.js

var hashsum = require("gulp-hashsum");

Then take a quick look at the code example in the README.

gulp.src(["app/**/*.js"]).
    pipe(hashsum({dest: "app"}));

Hmm, ok. We don’t actually want to specify what files to get the checksum from. The simplest would just be to pipe hashsum() in my task right before we write the file to disk.

// Process stylesheets
gulp.task('css', function () {
  return gulp.src(paths.css)
    .pipe(plumber({
      errorHandler: onError
    }))
    .pipe(sourcemaps.init())
    .pipe(autoprefixer({
      browsers: ['last 2 versions'],
      cascade: false
    }))
    .pipe(concat(paths.cssOutput))
    .pipe(cssnano())
    .pipe(hashsum({filename: './_data/cache_bust_css.yml', hash: 'md5'}))
    .pipe(gulpif(!isProduction, sourcemaps.write('.')))
    .pipe(gulp.dest(paths.cssDest));
});

There we go! Right after cssnano() (on line 14) we grab the MD5 hash and write it to _data/cache_bust_css.yml. Let’s give it a go and see what our output is.

6ebeded38c4fc6c1b111172052b6ca17  ../src/css/styles.css

Oh. That’s not quite what we were after, but the fact is that a checksum file is supposed to look like this. No matter. I think we can do some magic in Jekyll to get what we want. As I mentioned earlier, all we need is a unique string to properly identify when a file has changed, so we can probably just use one of Jekyll’s output filters to truncate down to the first 10 characters. Luckily enough, there’s something called truncate that does just that. Damn, I love Jekyll (or Liquid)!

<link rel="stylesheet" href="{{ site.baseurl }}/gui/css/styles.css?version={{ site.data.cache_bust_css | truncate: 10, '' }}">

Let’s break down what I just did. Besides adding the ?version= query string to the href attribute, I also added {{ site.data.cache_bust_css }} which basically means output the content from the file _data/cache_bust_css.yml right into the template. I then added the truncation filter truncate: 10, '' to just show the first 10 characters from the file. The trailing comma and empty single quotes is just to make sure that truncate doesn’t add an ellipsis after the string, since that’s the default behavior.

All this will result in a nice, unique string appended to our files, and we didn’t have to muck about with special files names and how to pass that info into Jekyll.

<link rel="stylesheet" href="/gui/css/styles.css?version=362887da69">

Looking good! Just rinse and repeat for the JavaScript task in Gulpfile.js and we’re done!