This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
/
inline.js
50 lines (43 loc) · 1.41 KB
/
inline.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/***
* Copyright (c) 2016 - 2018 Alex Grant (@localnerve), LocalNerve LLC
* Copyrights licensed under the BSD License. See the accompanying LICENSE file for terms.
*
* The inline javascript, small, run first.
*
* This is the source of the built asset, not the asset itself.
* This is an entry module, so there are no global concerns here.
* You might save a couple bytes by reverting this to es5 (should that arise).
*/
/*eslint-disable no-console */
/* global document, Promise */
import FontFaceObserver from 'fontfaceobserver';
import { loadCSS } from 'fg-loadcss';
const docEl = document.documentElement;
// --------------------------------------------------
// js happens
//
docEl.className = docEl.className.replace('no-js', '');
// --------------------------------------------------
// Fontface observer to quickly load fonts.
// Relies on Promise.
//
if (Promise) {
const font = new FontFaceObserver('Source Sans Pro', {});
font.load()
.then(() => {
docEl.className += ' fonts-loaded';
})
.catch((error) => {
console.error('font failed to load: ', error);
});
} else {
// Just let font-face work normally.
docEl.className += ' fonts-loaded';
}
// --------------------------------------------------
// Load non-critical stylesheets
//
const cssHrefs = document.querySelectorAll('meta[content$=".css"]');
for (let i = 0; i < cssHrefs.length; i++) {
loadCSS(cssHrefs[i].content);
}