-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
185 lines (144 loc) · 5 KB
/
index.html
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1" />
<title>(Don't) Load Font Based on Media Query</title>
<link rel="stylesheet" href="./css/demo.css" />
<link
rel="stylesheet"
data-media="tablet, desktop"
data-font="https://fonts.googleapis.com/css?family=Open+Sans"
/>
<link
rel="stylesheet"
data-media="tablet, desktop"
data-font="https://fonts.googleapis.com/css?family=Lobster+Two"
/>
<script src="./js/demo.js"></script>
</head>
<body>
<div class="wrapper">
<div class="notice">
<p>
Apparently, this is basically unnecessary. <i>Oops!</i>
</p>
<p>
Done correctly, browsers won't unnecessarily download custom fonts, if they aren't used outside the scope of their respectives <code>@media</code> queries.
</p>
<p>
If you're curious, check out this conversation on Twitter:
<a href="https://twitter.com/zachleat/status/644944306097119232">[1]</a>,
<a href="https://twitter.com/zachleat/status/644958131160662017">[2]</a>,
<a href="https://twitter.com/nathansmith/status/644958740941148166">[3]</a>.
</p>
</div>
<hr />
<h1>
(Don't) Load Font Based on Media Query
</h1>
<p>
At browser widths less than 768px, you should see a default sans-serif font.
</p>
<p>
Above 768px, you should see the "Open Sans" font, hosted by Google…
</p>
<p>
<a href="https://www.google.com/fonts/specimen/Open+Sans">https://www.google.com/fonts/specimen/Open+Sans</a>
</p>
<p>
This happens once per page load, so if you resize your browser, there's no effect. What you get when it loads is what you've got.
</p>
<hr />
<p>
Check out the CSS and JavaScript…
</p>
<p>
<a href="./css/demo.css">demo.css</a>
</p>
<p>
<a href="./js/demo.js">demo.js</a>
</p>
<hr />
<p>
This paragraph should be set in the "Lobster Two" font, at 768px and up…
</p>
<div class="callout">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>
<p>
<a href="https://www.google.com/fonts/specimen/Lobster+Two">https://www.google.com/fonts/specimen/Lobster+Two</a>
</p>
<hr />
<h2>
What? Why? This is stupid.
</h2>
<p>
I was talking with my coworkers about font loading best practices, and I jokingly said something like:
</p>
<blockquote>
<p>
"If we really care about mobile load times, then <i>best practice</i> would be no additional custom fonts."
</p>
</blockquote>
<p>
But, that got me to thinking whether or not it would even be possible to prevent/defer the loading of additional fonts, based on <code>@media</code> query.
</p>
<hr />
<p>
Initially, I tried this…
</p>
<pre><link
rel="stylesheet"
media="(min-width: 768px)"
href="https://fonts.googleapis.com/css?family=Open+Sans"
/></pre>
<hr />
<p>
However, as you may already be aware, browsers download that CSS file regardless of whether the <code>@media</code> query is met.
</p>
<p>
So, instead – and this irony is not lost on me – I am loading an extra JavaScript file that checks what "mode" the page is in (mobile, tablet, desktop) and loads external fonts if it's not mobile.
</p>
<hr />
<p>
So, at non-mobile widths (768px and up), this…
</p>
<pre><link
rel="stylesheet"
data-media="tablet, desktop"
data-font="https://fonts.googleapis.com/css?family=Open+Sans"
/></pre>
<p>
Changes to this…
</p>
<pre><link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Open+Sans"
/></pre>
<hr />
<h2>
Why not use a bandwidth <code>@media</code> query?
</h2>
<p>
That would be awesome, if it were a possibility. However, at the time of this writing, such a thing does not exist. Apparently, it would be really difficult for a browser to even know this, and if it did, for it to be completely accurate when a user is moving in/out of varying bandwidth zones.
</p>
<p>
<a href="https://css-tricks.com/bandwidth-media-queries">https://css-tricks.com/bandwidth-media-queries</a>
</p>
<p>
<a href="http://smashingmagazine.com/2013/01/bandwidth-media-queries-we-dont-need-em">http://smashingmagazine.com/2013/01/bandwidth-media-queries-we-dont-need-em</a>
</p>
<hr />
<p>
<small>
This was made by <a href="https://twitter.com/nathansmith">Nathan Smith</a>. Feel free to tell him how dumb it is, on Twitter.
</small>
</p>
</div>
<!-- /.wrapper -->
</body>
</html>