Skip to content

Commit 17d820d

Browse files
authored
Merge pull request #502 from jamstack/zl/lazyiframe
Lazy load the vimeo iframe for improved performance.
2 parents c0f2776 + c668717 commit 17d820d

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/js/lazyiframe.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
if( "forEach" in NodeList.prototype ) {
2+
function iframeSwitch(el) {
3+
if (!("fetch" in window) || "connection" in navigator && navigator.connection.saveData === true) {
4+
return;
5+
}
6+
7+
let iframe = document.createElement("iframe");
8+
iframe.setAttribute("loading", "lazy");
9+
for(let attr of el.attributes) {
10+
iframe.setAttribute(attr.name, attr.nodeValue);
11+
}
12+
el.replaceWith(iframe);
13+
}
14+
15+
if(typeof IntersectionObserver !== "undefined") {
16+
var observer = new IntersectionObserver(function(changes) {
17+
changes.forEach(function(change) {
18+
if(change.isIntersecting) {
19+
iframeSwitch(change.target);
20+
observer.unobserve(change.target);
21+
}
22+
});
23+
});
24+
}
25+
26+
document.querySelectorAll("div[data-lazyiframe]").forEach(function(element) {
27+
if(typeof IntersectionObserver !== "undefined") {
28+
observer.observe(element);
29+
} else {
30+
iframeSwitch(element);
31+
}
32+
});
33+
}

src/site/_includes/layouts/base.njk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,6 @@ ogimage: "/img/og/default-og-image.png"
9595
<script type="module" src="/js/details-force-state.js"></script>
9696
<script type="module" src="/js/filter-container.js"></script>
9797
<script type="module" src="/js/sort-container.js"></script>
98+
<script type="module" src="/js/lazyiframe.js"></script>
9899
</body>
99100
</html>

src/site/index.njk

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ layout: layouts/base.njk
7373
<a href="https://www.netlify.com/authors/matt-biilmann/">Matt Biilmann</a> took the concept of Jamstack mainstream with his presentation at Smashing Conf 2016. Watch the quintessential introduction to the Jamstack.
7474
</p>
7575
<div class="videowrapper mb-8">
76-
<iframe loading="lazy" src="https://player.vimeo.com/video/163522126?title=0&byline=0&portrait=0" width="640" height="360" frameborder="0" allow="autoplay; fullscreen" allowfullscreen title="Mathias Biilmann Smashing Conf 2016 talk video: The New Front-End Stack"></iframe>
76+
<div data-lazyiframe src="https://player.vimeo.com/video/163522126?title=0&byline=0&portrait=0" width="640" height="360" frameborder="0" allow="autoplay; fullscreen" allowfullscreen title="Mathias Biilmann Smashing Conf 2016 talk video: The New Front-End Stack">
77+
<a href="https://vimeo.com/163522126">Watch <em>Mathias Biilmann Smashing Conf 2016 talk video: The New Front-End Stack</em> on Vimeo</a>
78+
</div>
7779
</div>
7880
<a href="/resources/" class="cta">See more videos and resources</a>
7981
</div>

0 commit comments

Comments
 (0)