Skip to content

Use native loading="lazy" for article images and iframes - #9004

Closed
JamBalaya56562 wants to merge 2 commits into
FreshRSS:edgefrom
JamBalaya56562:native-lazy-loading
Closed

JamBalaya56562 wants to merge 2 commits into
FreshRSS:edgefrom
JamBalaya56562:native-lazy-loading

Conversation

@JamBalaya56562

Copy link
Copy Markdown
Contributor

Closes #2371. Continues / supersedes the stalled #5816.

What

Replace FreshRSS's custom JavaScript image lazy-loading with the native HTML loading="lazy" attribute for article-body images and iframes.

Previously, when the lazyload setting was on, lazyimg() rewrote each src to a grey.gif placeholder and stashed the real URL in data-original, and client JS (loadLazyImages) restored it as the article became active. This replaces that whole scheme with loading="lazy", letting the browser handle deferral natively.

Addressing the review feedback on #5816

The previous review raised two points; both are handled:

  1. No duplicate loading attribute — lazyimg() only adds loading="lazy" to <img>/<iframe> tags that don't already carry a loading attribute, so feed-supplied loading="…" is preserved and never doubled.
  2. The opt-out is preserved — the lazyload user setting still gates the behaviour: when it's off, no loading attribute is added and images load eagerly, exactly as before.

Changes

  • lib/lib_rss.php — lazyimg() now injects loading="lazy" into <img>/<iframe> (skipping tags that already have a loading attribute) instead of the grey.gif + data-original swap. Native loading only applies to img/iframe, so video/track are no longer rewritten (the old placeholder swap for those is dropped).
  • app/views/index/normal.phtml — the content transform is now gated on lazyload alone. The old && !display_posts condition existed only because the JS restored images on article activation (which never happens in "display all articles expanded" mode); native lazy-loading works viewport-based, so it now benefits that mode too.
  • app/views/helpers/javascript_vars.phtml — removed the now-unused does_lazyload flag.
  • p/scripts/main.js — removed the dead loadLazyImages() and its call in toggleContent(); simplified enforce_referrer_allowlist() to read the real iframe.src (no more data-original branch) and always reload matching iframes to apply referrerpolicy. In the print/PDF path, images/iframes in the cloned content are set to loading="eager" so they are all loaded for the printout.

data-original, does_lazyload and loadLazyImages are fully retired — no dead code left behind. The lazyload label (conf.reading.img_with_lazyload, "Use lazy load mode to load pictures") still describes the behaviour, so no i18n changes.

Tests

  • Added tests/lib/LazyImgTest.php covering lazyimg(): attribute injection for img/iframe, no duplication when loading already present, data-loading not mistaken for loading, video/track left untouched, mixed fragments, and no-op on plain content.
  • vendor/bin/phpunit tests/lib → 85 tests OK (12 new).
  • php -l lib/lib_rss.php → OK. phpstan on the changed files → no errors.
  • npm run eslint → passes.

Comment thread app/Controllers/updateController.php Resolved
Comment thread lib/lib_rss.php
Comment on lines 313 to -319
function lazyimg(string $content): string {
return preg_replace([
'/<((?:img|image|iframe|track)[^>]+?)src="([^"]+)"([^>]*)>/i',
"/<((?:img|image|iframe|track)[^>]+?)src='([^']+)'([^>]*)>/i",
'/<((?:video)[^>]+?)poster="([^"]+)"([^>]*)>/i',
"/<((?:video)[^>]+?)poster='([^']+)'([^>]*)>/i",
], [

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.

Lazy loading for image, track and video should not be removed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks — you're right that image/track/video lose their deferral here, so let me lay out the reasoning and a question.

The loading attribute is defined by the HTML Standard only for <img> and <iframe> — see the "lazy loading attributes" section: https://html.spec.whatwg.org/multipage/urls-and-fetching.html#lazy-loading-attributes (MDN overview: https://developer.mozilla.org/en-US/docs/Web/Performance/Lazy_loading). <video>, <track> and SVG <image> have no loading attribute, so native lazy-loading can't apply to them — deferring a <video> is done via preload, and its poster can't be lazy-loaded natively at all. Keeping deferral for those three would mean retaining the custom grey.gif + data-original + loadLazyImages() machinery that this PR removes, which is its whole purpose. (For <track>, the old placeholder swap was also questionable — replacing a subtitle src with grey.gif would break caption loading.)

That said, a <video> poster is a real preview image, so deferring it is genuinely meaningful, even if it's rare in feed bodies. Would you like me to keep a small dedicated handler just for <video poster> (retaining a minimal bit of the swap for that one case) while img/iframe use native loading? Or is dropping it acceptable for the sake of the simplification?

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.

I made a PR like this earlier: #7864 but abandoned it.

Let me clarify a bit:

Would you like me to keep a small dedicated handler just for <video poster> (retaining a minimal bit of the swap for that one case) while img/iframe use native loading? Or is dropping it acceptable for the sake of the simplification?

img/image can use loading="lazy", JS handler is needed for track and video.

I am not sure what should be done with iframe, loading="lazy" doesn't entirely work with it, see PR description linked above.

@JamBalaya56562 JamBalaya56562 Jul 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@Inverle
Thanks, that's really helpful context — and you're right on all three:

  • <image> — my mistake, I was thinking of the SVG element. It's the legacy HTML compatibility tag that the parser turns into <img>, and since the sanitizer leaves it as-is it does need handling; it can take loading="lazy" too.
  • <video> / <track> — agreed, the point isn't only perf but avoiding fetching src / poster before the user interacts when lazy loading is enabled, so they do need a JS handler.
  • <iframe> — noted the instantiation cost from Lazy load during scroll and other improvements #7864 (<lazy-iframe> + the 100-per-article limit); native loading doesn't cover that.

Since this PR continues #5816, which was image-focused (its lazyimg() only handled img/iframe at the time — image/track/video were added later), I'd like your call on the scope before I redo it:

  • A – hybrid: native loading="lazy" for img/image, and keep a JS handler for track/video (and adopt the <lazy-iframe> approach from Lazy load during scroll and other improvements #7864 for iframes). More complete, but reintroduces some of the JS.
  • B – narrow: only put native loading="lazy" on img/image and leave the existing JS deferral for video/track/iframe exactly as-is. Smallest change, keeps current behaviour for everything native can't handle.

Which would you prefer? Or, since you already have #7864, would it make more sense to build on that instead of this PR?

@JamBalaya56562
JamBalaya56562 requested a review from Inverle July 8, 2026 09:50
@Alkarex Alkarex added this to the 1.30.0 milestone Jul 8, 2026
@Alkarex

Alkarex commented Jul 8, 2026

Copy link
Copy Markdown
Member

I have not reviewed the code yet, but a few checks already:

  • Existing articles already in database must still be rendered correctly (probably unchanged but still worth double-checking)
  • The option to not use lazy-loading should be respected
  • Images with lazy-loading from source require extra care (I am not sure whether we should remove that attribute if our user has disabled lazy-loading or whether we should respect the source)

@JamBalaya56562

Copy link
Copy Markdown
Contributor Author

Thanks @Alkarex! Taking your first two checks here — I'll answer the third one separately since it needs a bit more.

1. Existing articles in the database still render correctly ✅

lazyimg() has always been a render-time transform: it's applied to $this->entry->content(true) at display time (app/views/index/normal.phtml:131), never stored. So the database only ever holds the original sanitised HTML — nothing was ever persisted with the old grey.gif / data-original swap. Before and after this PR the article body goes through the same render path, so existing articles render identically; the only difference is that the new code adds loading="lazy" at display time instead of rewriting src to a placeholder. There's no stored state to migrate, so it's backward-compatible. (If anything it's more robust: the old scheme showed a grey placeholder until JS restored the real src, so images broke when JS didn't run — native loading has no such dependency.)

2. The opt-out is respected ✅

The lazyload user setting still fully gates the behaviour:

  • app/views/index/normal.phtml:131 — lazyload ? lazyimg(...) : $this->entry->content(true). When it's off, lazyimg() isn't called at all, so no loading attribute is added and images load eagerly.
  • app/views/helpers/index/normal/entry_header.phtml:65 — the topline thumbnail honours it too: $lazyload ? ' loading="lazy"' : ''.
  • Reader view does no server-side lazy-loading (unchanged by this PR).

One deliberate change: the normal-view condition dropped the old && !display_posts. That clause only existed because the JS restored images on article activation, which never happens in "show articles unfolded" mode — so lazy-loading was effectively disabled there. Native loading is viewport-based, so it now works in that mode too. The setting itself is still honoured; it just no longer has that one dead spot.

@JamBalaya56562

Copy link
Copy Markdown
Contributor Author

On the third point (feed-supplied loading attributes):

Good question — and it does apply in practice: loading is not in SimplePie's strip_attributes, so a feed's <img loading="…"> survives sanitisation, is stored, and reaches rendering. So we do have to decide.

My proposal is that the user's setting should be authoritative over the feed-supplied attribute, in both directions:

  • Lazy-loading ON → normalise every article-body <img>/<iframe> to loading="lazy", overwriting a feed-supplied loading="eager". (The current code skips any tag that already has a loading attribute, so a feed's loading="eager" currently slips through and defeats the setting — that's the same class of problem as this one, just the mirror case.)
  • Lazy-loading OFF → strip the loading attribute so the image loads eagerly, rather than leaving a feed's loading="lazy" in place.

Rationale: the setting is the user's explicit opt-in/opt-out, and — as @Inverle framed it in the inline thread — lazy-loading here is also about not issuing requests before user interaction. Letting the setting win keeps that guarantee meaningful and makes the behaviour predictable, instead of having it silently overridden by whatever a feed happens to emit. It also fully satisfies your second point (the opt-out being respected), including the edge cases.

I'll fold this normalisation into the implementation. Since the element-scope question (native loading for img/image vs. keeping a JS handler for video/track/iframe) is still open with @Inverle, I'll settle that first and then make the loading handling consistent with whatever elements we land on.

JamBalaya56562 and others added 2 commits July 25, 2026 09:58
Replace the custom JS lazy-loading (grey.gif placeholder + data-original
restored by loadLazyImages) with the native HTML loading="lazy" attribute.
lazyimg() now adds loading="lazy" to img/iframe tags that do not already
have a loading attribute (no duplicates); the lazyload setting still gates
it, preserving the opt-out. Removes the now-dead data-original/does_lazyload
JS and applies lazy-loading in display_posts mode too.

Closes #2371. Continues #5816.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
It has been dead since its introduction and the reader view does no
server-side lazy-loading.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@Alkarex

Alkarex commented Jul 25, 2026

Copy link
Copy Markdown
Member

If you can, I prefer that you just push and merge to the same PR without rewriting git history (so no forced pushes, no rebase) as it breaks things such as partial reviews, changes since last review, some comments, information about multiple authors, git signing, etc. and also makes git checkouts more cumbersome due to branches having diverged.

@JamBalaya56562

Copy link
Copy Markdown
Contributor Author

Sorry about the forced push, and thank you for explaining the downsides — I hadn't considered the impact on partial reviews, signing and multi-author history.

For what it's worth, the rebase was only to resolve the merge conflict with the advanced edge; the PR's own changes were not modified. But I understand that rewriting the history causes problems regardless of intent.

Going forward I'll leave the existing commits untouched: when a conflict comes up I'll merge edge into the branch as an ordinary commit and push that on top, rather than rebasing and force-pushing, so the history stays intact and reviewable.

@Inverle Inverle modified the milestones: 1.30.0, 1.31.0 Sep 8, 2026
@JamBalaya56562 JamBalaya56562 closed this by deleting the head repository Oct 15, 2026
Alkarex added a commit to sad270/FreshRSS that referenced this pull request Nov 19, 2026
Alkarex added a commit that referenced this pull request Nov 19, 2026
…te (#5816)

* perf(image): replace js images lazy loading by html `loading` attribute

* Improve lazy images regex
Fix #2371
Replaces #5816
Replaces #9004

* Reduce comments

---------

Co-authored-by: Alexandre Alapetite <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider using native loading="lazy"

3 participants