Skip to content

Instantly share code, notes, and snippets.

View stevepiron's full-sized avatar

Steve Piron stevepiron

  • Brussels, Belgium
View GitHub Profile
@stevepiron
stevepiron / sw.js
Created October 29, 2019 21:19
Simple WebP images with Imgix
/**
* Serving WebP images drastically reduces the weight of a web page,
* however their implementation is rather complex to manage as it involves
* picture elements with multiple sources to make sure browsers that do not
* yet support them are not left out without any image at all.
*
* Imgix generates images on the fly as they are requested. This snippet
* leverages this amazing capability to simplify the delivery of WebP images
* in browsers that support them.
*
@stevepiron
stevepiron / preparing-cross-browser-lazy-loading.html
Last active August 5, 2019 16:03
Preparing for cross-browser (native) lazy loading
<noscript>
<img src="image.jpg">
</noscript>
<img data-src="image.jpg" loading="lazy">
<script>
if ('loading' in HTMLImageElement.prototype) {
const lazy = document.querySelectorAll('[data-src]');
lazy.forEach(el => {
// Turn `data-src` into `src`
@stevepiron
stevepiron / native-lazy-loading-test.js
Last active August 5, 2019 15:49
Testing for native lazy loading support
if ('loading' in HTMLImageElement.prototype) {
// Native lazy loading is supported
} else {
// Offer another lazy loading solution
}
@stevepiron
stevepiron / native-lazy-loading.html
Created August 5, 2019 15:33
Native lazy loading
<img src="image.jpg" loading="lazy">
@stevepiron
stevepiron / service-worker-registration.js
Created April 24, 2018 14:30
Basic service worker registration
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js');
}
@stevepiron
stevepiron / two-tier-variables.scss
Last active September 1, 2017 22:46
Two-tier Sass variables
/* ========================================================================= */
/* Colors */
/* ========================================================================= */
$neutral-1: #767676;
$neutral-2: #E6E6E6;
$neutral-3: #F5F5F5;
$neutral-text: $neutral-1;
$neutral-border: $neutral-2;
@stevepiron
stevepiron / gist:9b33477c068c3363f95107a0334584f0
Created May 10, 2017 12:46
Craft CMS semantic (accessible) language switcher
<nav class="o-lang-switcher">
<h2 class="u-screenreader-only">{{ 'Available translations of the current page'|translate }}</h2>
<ul class="o-lang-switcher__list">
{% for locale in craft.i18n.getSiteLocales() %}
{% if entry is defined %}
{% set localisedPage = craft.entries.id(entry.id).locale(locale.id).first() %}
{% elseif category is defined %}
{% set localisedPage = craft.categories.id(category.id).locale(locale.id).first() %}
{% endif %}
{% set linkUrl = localisedPage is defined ? localisedPage.getUrl() : craft.config.siteUrl[locale.id] %}