Skip to content

A platform for community discussion. Free, open, simple.

PERF: Use native iframe lazy loading for full app embeds (#39320)

Rafael dos Santos Silva45b48524dc20
diff --git a/frontend/discourse/admin/controllers/admin-embedding/index.js b/frontend/discourse/admin/controllers/admin-embedding/index.js
index 823f4922133..e872d5c5687 100644
--- a/frontend/discourse/admin/controllers/admin-embedding/index.js
+++ b/frontend/discourse/admin/controllers/admin-embedding/index.js
@@ -27,8 +27,7 @@ export default class AdminEmbeddingIndexController extends Controller {
? `
fullApp: true,
embedHeight: '800px',
- // lazyLoad: false, // disable lazy loading of the iframe
- // lazyLoadMargin: '1000', // pixels before viewport to start loading
+ // lazyLoad: false, // disable native lazy loading of the iframe
// dynamicHeight: true,
// embedMinHeight: '400',
// embedMaxHeight: '900',`
diff --git a/public/javascripts/embed.js b/public/javascripts/embed.js
index a237696e2cb..f3169ae3850 100644
--- a/public/javascripts/embed.js
+++ b/public/javascripts/embed.js
@@ -57,38 +57,18 @@
}
}
+ iframe.src = src;
iframe.id = "discourse-embed-frame";
iframe.width = "100%";
iframe.frameBorder = "0";
iframe.scrolling = DE.fullApp ? "yes" : "no";
if (DE.fullApp) {
iframe.style.height = DE.embedHeight || "600px";
+ iframe.loading = DE.lazyLoad === false ? "eager" : "lazy";
}
iframe.referrerPolicy =
DE.discourseReferrerPolicy || "no-referrer-when-downgrade";
-
- var lazyLoad = DE.fullApp && DE.lazyLoad !== false;
-
- if (lazyLoad && "IntersectionObserver" in window) {
- var margin = parseInt(DE.lazyLoadMargin, 10);
- if (isNaN(margin)) {
- margin = 1000;
- }
- comments.appendChild(iframe);
- var observer = new IntersectionObserver(
- function (entries) {
- if (entries[0].isIntersecting) {
- iframe.src = src;
- observer.disconnect();
- }
- },
- { rootMargin: margin + "px" }
- );
- observer.observe(iframe);
- } else {
- iframe.src = src;
- comments.appendChild(iframe);
- }
+ comments.appendChild(iframe);
// Thanks http://amendsoft-javascript.blogspot.ca/2010/04/find-x-and-y-coordinate-of-html-control.html
function findPosY(obj) {