Skip to content

Lazy load during scroll and other improvements - #7864

Closed
Inverle wants to merge 2 commits into
FreshRSS:edgefrom
Inverle:lazy-loading-perf
Closed

Inverle wants to merge 2 commits into
FreshRSS:edgefrom
Inverle:lazy-loading-perf

Conversation

@Inverle

@Inverle Inverle commented Aug 27, 2025

Copy link
Copy Markdown
Member

Overview

  • Lazy loading is now done during scroll instead of loading everything at once when an article gets opened
    • This is implemented in JS using IntersectionObserver to ensure support with SeaMonkey and other old browsers
      • Note: <iframe> is renamed to <lazy-iframe>
      • Because inserting 1k empty iframes into the HTML had bigger performance impact than inserting 50k <img>
    • To use the same logic for all elements since <video poster> needs to lazy loaded with JS anyway. (with other elements to come)
    • 100 visible elements can be loaded at once, if there are more for some reason then the user needs to rescroll
      • Shouldn't happen unless done intentionally by a bad feed
  • Added lazy loading to reader view and HTML user query page
    • Also if the Show articles unfolded by default option is enabled
    • does_lazyload option is only respected on reader view page
  • Amount of iframes that can be inserted inside one article is limited to 100 to prevent DoS
    • This is done with a new SimplePie function: function limit_tags(array<tag:string,limit:int> $limits)
  • Some unrelated changes in javascript_vars.phtml were done (replacing instances of !!$boolean with $boolean)

CSS fix

When Show articles unfolded by default option is enabled

Before (see GIF)

fdsfsdf

After (static background color) image

Extensions that need to be fixed

(after merging)

TODO

  • add loading="lazy" to elements when reading via API (if this makes sense)

@Inverle

Inverle commented Aug 27, 2025

Copy link
Copy Markdown
Member Author

By the way, should it be possible to close articles when using Show articles unfolded by default? It isn't at the moment and I don't know if that's intended.

Comment thread p/scripts/global_view.js
if (intersectionObserver) {
intersectionObserver.disconnect();
}
// eslint-disable-next-line no-global-assign

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this imply it'd be preferable to pick a different name? The ESLint complaint strikes me as valid.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The observer needs to be reinstantiated each time when posts are automatically unfolded and lazy loading is enabled after clicking on a feed in global view.

Otherwise if articles are folded, this will get taken care of in toggleContent() and in that place ESLint won't complain because the variable is in the same file.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh, just add :writable in /* global intersectionObserver:writable */ instead. I thought this was complaining about the built-in.

@Alkarex Alkarex added the UI 🎨 User Interfaces label Aug 27, 2025
@Alkarex

Alkarex commented Aug 27, 2025

Copy link
Copy Markdown
Member

@Alkarex Alkarex added this to the 1.28.0 milestone Aug 27, 2025
Comment thread p/scripts/main.js
}
el.removeAttribute('data-original');
}, {
rootMargin: '800px 0px 800px 0px',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that 800 real px? Because if so I'd suggest at least something like:

const pixelDensity = window.devicePixelRatio || 1;
const margin = Math.floor(pixelDensity * 800);
const rootMargin = `${margin}px 0px ${margin}px 0px`;

And perhaps I'd make it something like this because 800 only half a screen or less regardless of the devicePixelRatio but that aside.

const pixelDensity = window.devicePixelRatio || 1;
const margin = Math.floor(pixelDensity * window.innerHeight);
const rootMargin = `${margin}px 0px ${margin}px 0px`;

Though really I'd prefer to do at least 2 * window.innerHeight.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In short, something like this:

const pixelDensity = window.devicePixelRatio || 1;
const margin = Math.min(800 * pixelDensity, Math.floor(2 * window.innerHeight * pixelDensity));
const rootMargin = `${margin}px 0px ${margin}px 0px`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should also be reinitialized on window resize akin to the debouncedOnScroll stuff.

@Frenzie

Frenzie commented Aug 27, 2025 •

Copy link
Copy Markdown
Member

Related:

Though then there's no control over what it does exactly of course. Put another way, it's very easy for the lazy loading experience to be atrocious if it's too lazy. This can be emulated with 3G in the network tab. Hence why it should be at least one viewport ahead and not mere pixels.

@Alkarex

Alkarex commented Aug 27, 2025

Copy link
Copy Markdown
Member

Isn't a good part of this PR solved by simply using native loading="lazy" ?

@Frenzie

Frenzie commented Aug 27, 2025

Copy link
Copy Markdown
Member

Yes, I believe it abides by my requirements. And I didn't even realize it also worked on iframes.

@Inverle

Inverle commented Aug 27, 2025 •

Copy link
Copy Markdown
Member Author

So should I switch to loading="lazy" for img and iframe, and still use JS for <video poster>?

This would make a difference on the HTML query page if JS were disabled (because images wouldn't be displayed, but lazy loading wouldn't work anyway after doing this: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/img#attr-loading:~:text=Note%3A%20Loading,requested%20and%20when.)

Note that lazy loading wouldn't work anymore in SeaMonkey

The other concern I have is that even if I enable lazy loading for iframes like this:

diff --git a/lib/lib_rss.php b/lib/lib_rss.php
index c0fe1770..d89a2be8 100644
--- a/lib/lib_rss.php
+++ b/lib/lib_rss.php
@@ -714,8 +714,8 @@ function lazyimg(string $content): string {
                        '/<((?:video)[^>]+?)poster="([^"]+)"([^>]*)>/i',
                        "/<((?:video)[^>]+?)poster='([^']+)'([^>]*)>/i",
                ], [
-                       '<$1src="' . Minz_Url::display('/themes/icons/grey.gif') . '" data-original="$2"$3>',
-                       "<$1src='" . Minz_Url::display('/themes/icons/grey.gif') . "' data-original='$2'$3>",
+                       '<$1loading="lazy" src="$2"$3>',
+                       "<$1loading='lazy' src='$2'$3>",
                        '<$1poster="' . Minz_Url::display('/themes/icons/grey.gif') . '" data-original="$2"$3>',
                        "<$1poster='" . Minz_Url::display('/themes/icons/grey.gif') . "' data-original='$2'$3>",

then using this feed:

View
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title>Example Feed2</title>
  <link href="http://localhost/"/>
  <updated>2003-12-13T18:30:02Z</updated>
  <author>
    <name>John Doe</name>
  </author>
  <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>

  <entry>
    <title>Atom-Powered Robots Run Amok2</title>
    <link href="http://example.org/2003/12/13/atom03"/>
    <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
    <updated>2003-12-13T18:30:02Z</updated>
    <summary>Some text.</summary>
    <content type="html">
    <![CDATA[
      <?= str_repeat('<iframe src="https://http.cat/"></iframe>', 10000) ?>
    ]]>
    </content>
  </entry>

</feed>
DOM tab in devtools without opening any article image image

slowdowns still happen, but don't if iframe is renamed to lazy-iframe. In both Firefox and Chrome
(requests don't get sent though before iframes are in viewport)

I suppose it's just a minor issue given that iframes are limited to 100 now, but multiple entries with 100 iframes each can still cause issues depending on how much posts the user allows to be displayed by default

for example
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title>Example Feed2</title>
  <link href="http://localhost/"/>
  <updated>2003-12-13T18:30:02Z</updated>
  <author>
    <name>John Doe</name>
  </author>
  <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>

  <?php for ($i = 0; $i < 1000; $i++): ?>
  <entry>
    <title>Atom-Powered Robots Run Amok2<?= $i ?></title>
    <link href="http://example.org/2003/12/13/atom03"/>
    <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a<?= $i ?></id>
    <updated>2003-12-13T18:30:02Z</updated>
    <summary>Some text.</summary>
    <content type="html">
    <![CDATA[
      <?= str_repeat('<iframe src="about:blank"></iframe>', 100) ?>
    ]]>
    </content>
  </entry>
  <?php endfor ?>

</feed>

^ this feed causes a minor slowdown upon page load in Firefox and Chrome, but crashes SeaMonkey - haven't tested other browsers

edit: Chrome crashed too once while running Lighthouse with this feed loaded but doesn't happen consistently - gets lower scores

@Frenzie

Frenzie commented Aug 27, 2025

Copy link
Copy Markdown
Member

I suppose it's just a minor issue given that iframes are limited to 100 now, but multiple entries with 100 iframes each can still cause issues depending on how much posts the user allows to be displayed by default

Maybe make it a setting of some sort and lower the default to 50 if it can cause issues?

@Inverle

Inverle commented Sep 1, 2025

Copy link
Copy Markdown
Member Author

Maybe make it a setting of some sort and lower the default to 50 if it can cause issues?

Unfortunately 50 is still too much

Using <lazy-iframe> could also improve the performance in case of FreshRSS/Extensions#236

@Alkarex Alkarex modified the milestones: 1.28.0, 1.29.0 Dec 16, 2025
civilblur added a commit to civilblur/youlag that referenced this pull request Jan 8, 2026
…hrss' native lazyload that adds 'grey.gif', resulting in loads of http requests (FreshRSS/FreshRSS/pull/7864)
@Inverle Inverle added the BLOCKED ❌ merge conflict or failing tests that block the PR label Apr 1, 2026
@Alkarex Alkarex modified the milestones: 1.29.0, 1.30.0 May 3, 2026
@Alkarex Alkarex modified the milestones: 1.30.0, 1.31.0 Aug 29, 2026
@Inverle

Inverle commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

5ad167e

@Inverle Inverle closed this Nov 19, 2026
@Alkarex

Alkarex commented Nov 19, 2026

Copy link
Copy Markdown
Member

I had a bit forgotten about that one...

@Inverle

Inverle commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

Main issue is that <video poster> and <track src> won't be lazy loaded at all anymore.

And by looking at the commit it doesn't look like the option for disabling lazy loading will work anymore.

@Alkarex Alkarex mentioned this pull request Nov 20, 2026
@Alkarex

Alkarex commented Nov 20, 2026

Copy link
Copy Markdown
Member

#9601

Alkarex added a commit that referenced this pull request Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BLOCKED ❌ merge conflict or failing tests that block the PR UI 🎨 User Interfaces

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants