Skip to content

Instantly share code, notes, and snippets.

@atma
Created December 11, 2015 14:27
Show Gist options
  • Save atma/0fa1934094519710fa2f to your computer and use it in GitHub Desktop.
Save atma/0fa1934094519710fa2f to your computer and use it in GitHub Desktop.
// Woff font loader with localStorage caching
function loadFont(a, b, c, d) { function e() { if (!window.FontFace) return !1; var a = new FontFace("t", 'url("data:application/font-woff2,") format("woff2")', {}), b = a.load(); try { b.then(null, function () { }) } catch (c) { } return "loading" === a.status } var f = navigator.userAgent, g = !window.addEventListener || f.match(/(Android (2|3|4.0|4.1|4.2|4.3))|(Opera (Mini|Mobi))/) && !f.match(/Chrome/); if (!g) { var h = {}; try { h = localStorage || {} } catch (i) { } var j = "x-font-" + a, k = j + "url", l = j + "css", m = h[k], n = h[l], o = document.createElement("style"); if (o.rel = "stylesheet", document.head.appendChild(o), !n || m !== b && m !== c) { var p = c && e() ? c : b, q = new XMLHttpRequest; q.open("GET", p), q.onload = function () { q.status >= 200 && q.status < 400 && (h[k] = p, h[l] = q.responseText, d || (o.textContent = q.responseText)) }, q.send() } else o.textContent = n } };
//loadFont('Lato400', '/assets/fonts/lato/lato.woff.css', '/assets/fonts/lato/lato.woff2.css');
@godfreyhobbs
Copy link

This has bug for safari private:

fix with:
try { h = localStorage;h[42]=42; delete h[42]; || {} }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment