Skip to content
This repository was archived by the owner on Sep 10, 2022. It is now read-only.

Commit 1c5ffa0

Browse files
author
Matt Gaunt
committed
Adding ability to async in extra CSS
1 parent 919268b commit 1c5ffa0

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

server/layouts/app-shell.handlebars

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,42 @@ limitations under the License.
110110
</svg>
111111
</div>
112112

113+
{{#if remoteStyles}}
114+
<script>
115+
var remoteStyles = [
116+
{{#each remoteStyles}}
117+
'{{this}}',
118+
{{~/each}}
119+
];
120+
121+
// <3 to Paul Irish
122+
// http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
123+
var requestAnimFrame = (function(){
124+
return window.requestAnimationFrame ||
125+
window.webkitRequestAnimationFrame ||
126+
window.mozRequestAnimationFrame ||
127+
function( callback ){
128+
window.setTimeout(callback, 1000 / 60);
129+
};
130+
})();
131+
132+
// Asynchronously load CSS with a Request Animation Frame
133+
requestAnimFrame(function() {
134+
var elementToInsertLinkBefore =
135+
document.getElementsByTagName('script')[0];
136+
for (var i = 0; i < remoteStyles.length; i++) {
137+
var linkElement = document.createElement('link');
138+
linkElement.rel = 'stylesheet';
139+
linkElement.media = 'all';
140+
linkElement.href = remoteStyles[i];
141+
142+
elementToInsertLinkBefore.parentNode.insertBefore(linkElement,
143+
elementToInsertLinkBefore);
144+
}
145+
});
146+
</script>
147+
{{~/if}}
148+
113149
{{#each remoteScripts}}
114150
<script src="{{this}}" async></script>
115151
{{~/each}}

server/layouts/default.handlebars

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,42 @@ limitations under the License.
6161
</div>
6262
</section>
6363

64+
{{#if remoteStyles}}
65+
<script>
66+
var remoteStyles = [
67+
{{#each remoteStyles}}
68+
'{{this}}',
69+
{{~/each}}
70+
];
71+
72+
// <3 to Paul Irish
73+
// http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
74+
var requestAnimFrame = (function(){
75+
return window.requestAnimationFrame ||
76+
window.webkitRequestAnimationFrame ||
77+
window.mozRequestAnimationFrame ||
78+
function( callback ){
79+
window.setTimeout(callback, 1000 / 60);
80+
};
81+
})();
82+
83+
// Asynchronously load CSS with a Request Animation Frame
84+
requestAnimFrame(function() {
85+
var elementToInsertLinkBefore =
86+
document.getElementsByTagName('script')[0];
87+
for (var i = 0; i < remoteStyles.length; i++) {
88+
var linkElement = document.createElement('link');
89+
linkElement.rel = 'stylesheet';
90+
linkElement.media = 'all';
91+
linkElement.href = remoteStyles[i];
92+
93+
elementToInsertLinkBefore.parentNode.insertBefore(linkElement,
94+
elementToInsertLinkBefore);
95+
}
96+
});
97+
</script>
98+
{{~/if}}
99+
64100
{{#each remoteScripts}}
65101
<script src="{{this}}" async></script>
66102
{{~/each}}

server/models/path-config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,29 @@ var pathConfigs = {
55
'/': {
66
view: 'index',
77
inlineStyles: getFileContents(['/styles/core.css']),
8+
remoteStyles: ['https://fonts.googleapis.com/css?family=Roboto:' +
9+
'400,300,700,500,400italic'],
810
remoteScripts: ['/scripts/static-page.js']
911
},
1012
'/url-1': {
1113
view: 'url-1',
1214
inlineStyles: getFileContents(['/styles/core.css']),
15+
remoteStyles: ['https://fonts.googleapis.com/css?family=Roboto:' +
16+
'400,300,700,500,400italic'],
1317
remoteScripts: ['/scripts/static-page.js']
1418
},
1519
'/url-2': {
1620
view: 'url-2',
1721
inlineStyles: getFileContents(['/styles/core.css']),
22+
remoteStyles: ['https://fonts.googleapis.com/css?family=Roboto:' +
23+
'400,300,700,500,400italic'],
1824
remoteScripts: ['/scripts/static-page.js']
1925
},
2026
'/app-shell': {
2127
view: '',
2228
inlineStyles: getFileContents(['/styles/core.css']),
29+
remoteStyles: ['https://fonts.googleapis.com/css?family=Roboto:' +
30+
'400,300,700,500,400italic'],
2331
remoteScripts: ['/scripts/core.js']
2432
}
2533
};

src/styles/core/_core.scss

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ html, body {
2424
margin: 0;
2525
height: 100%;
2626
width: 100%;
27-
font-family: 'Roboto';
27+
font-family: 'Helvetica', 'Verdana', sans-serif;
2828
font-weight: 400;
29+
// Not implemented yet but is a nice solution for async loading fonts
30+
font-display: optional;
2931
color: #444;
3032
-webkit-font-smoothing: antialiased;
3133
-moz-osx-font-smoothing: grayscale;
@@ -59,6 +61,10 @@ body:after {
5961
transition: opacity 0.333s cubic-bezier(0,0,0.21,1) 0.4s;
6062
}
6163

64+
h1, h2, h3, h4, h5, h6 {
65+
font-family: 'Roboto', 'Helvetica', 'Verdana', sans-serif;
66+
}
67+
6268
.app-deeplink:after {
6369
opacity: 1;
6470
pointer-events: auto;

0 commit comments

Comments
 (0)