Repository navigation
Use native loading="lazy" for article images and iframes - #9004
JamBalaya56562 wants to merge 2 commits into
Conversation
| 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", | ||
| ], [ |
There was a problem hiding this comment.
Lazy loading for image, track and video should not be removed
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
I made a PR like this earlier: #7864 but abandoned it.
Let me clarify a bit:
- It is true that
<video>and<track>do not have an equivalentloadingattribute, but we need to lazy load them, to avoid making requests to theirsrc/posterwithout user interaction if lazy loading is enabled. - It is not SVG
<image>, but a compatibility element in browsers, that is turned to<img>during parsing, see: https://html.spec.whatwg.org/multipage/parsing.html#:~:text=A%20start%20tag%20whose%20tag%20name%20is%20%22image (it is not replaced during sanitization, so that's why it's included - HTML5 parser is not used yet there) - As for the
grey.gifplaceholder, it doesn't really matter what it's replaced with, since the original is put back later.
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) whileimg/iframeuse 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.
There was a problem hiding this comment.
@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 takeloading="lazy"too.<video>/<track>— agreed, the point isn't only perf but avoiding fetchingsrc/posterbefore 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); nativeloadingdoesn'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"forimg/image, and keep a JS handler fortrack/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"onimg/imageand leave the existing JS deferral forvideo/track/iframeexactly 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?
f19fc5b to
93f1a8d
Compare
|
I have not reviewed the code yet, but a few checks already:
|
|
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 ✅
2. The opt-out is respected ✅ The
One deliberate change: the normal-view condition dropped the old |
|
On the third point (feed-supplied Good question — and it does apply in practice: My proposal is that the user's setting should be authoritative over the feed-supplied attribute, in both directions:
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 |
4647b86 to
6ba879b
Compare
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]>
6ba879b to
16cd9a8
Compare
|
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. |
|
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 Going forward I'll leave the existing commits untouched: when a conflict comes up I'll merge |
Fix FreshRSS#2371 Replaces FreshRSS#5816 Replaces FreshRSS#9004
…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]>
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
lazyloadsetting was on,lazyimg()rewrote eachsrcto agrey.gifplaceholder and stashed the real URL indata-original, and client JS (loadLazyImages) restored it as the article became active. This replaces that whole scheme withloading="lazy", letting the browser handle deferral natively.Addressing the review feedback on #5816
The previous review raised two points; both are handled:
loadingattribute —lazyimg()only addsloading="lazy"to<img>/<iframe>tags that don't already carry aloadingattribute, so feed-suppliedloading="…"is preserved and never doubled.lazyloaduser setting still gates the behaviour: when it's off, noloadingattribute is added and images load eagerly, exactly as before.Changes
lib/lib_rss.php—lazyimg()now injectsloading="lazy"into<img>/<iframe>(skipping tags that already have aloadingattribute) instead of thegrey.gif+data-originalswap. Nativeloadingonly applies toimg/iframe, sovideo/trackare no longer rewritten (the old placeholder swap for those is dropped).app/views/index/normal.phtml— the content transform is now gated onlazyloadalone. The old&& !display_postscondition 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-unuseddoes_lazyloadflag.p/scripts/main.js— removed the deadloadLazyImages()and its call intoggleContent(); simplifiedenforce_referrer_allowlist()to read the realiframe.src(no moredata-originalbranch) and always reload matching iframes to applyreferrerpolicy. In the print/PDF path, images/iframes in the cloned content are set toloading="eager"so they are all loaded for the printout.data-original,does_lazyloadandloadLazyImagesare fully retired — no dead code left behind. Thelazyloadlabel (conf.reading.img_with_lazyload, "Use lazy load mode to load pictures") still describes the behaviour, so no i18n changes.Tests
tests/lib/LazyImgTest.phpcoveringlazyimg(): attribute injection for img/iframe, no duplication whenloadingalready present,data-loadingnot mistaken forloading, 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.phpstanon the changed files → no errors.npm run eslint→ passes.