Skip to content

Lazy load post images - #539

Open
ButteredCats wants to merge 2 commits into
redlib-org:mainfrom
ButteredCats:main
Open

Lazy load post images#539
ButteredCats wants to merge 2 commits into
redlib-org:mainfrom
ButteredCats:main

Conversation

@ButteredCats

Copy link
Copy Markdown
Contributor

The <image> items don't appear to be a real thing despite displaying the image, and they didn't support loading="lazy" so this basically just sets post images to always use the normal <img> tag and get rid of all the svg stuff that wasn't doing anything.

@dave0003

dave0003 commented Apr 5, 2026

Copy link
Copy Markdown

Compiled and tested. Seems to work fine for me.

It gets rid of the svg stuff for single images. For galleries, it's still the same (doesn't have the svg stuff to begin with).

Sparronator9999 added a commit to Sparronator9999/redlib that referenced this pull request Aug 6, 2026
Sparronator9999 added a commit to Sparronator9999/redlib that referenced this pull request Aug 6, 2026
almighty-atlas added a commit to almighty-atlas/redlib that referenced this pull request Sep 7, 2026
Upstream sized post images by wrapping them in an <svg> of fixed width and
height, which reserves the box but loads eagerly: the <img loading="lazy">
inside it sits in a <desc> element and is never rendered. PR redlib-org#539 replaced
that with a real <img loading="lazy">, which restored lazy loading but left
the image with no intrinsic size at all.

The result was that a feed image occupied zero height until it arrived. On
a 25-post multireddit feed, nine images finishing their load grew the page
from 7774 to 9580 pixels - roughly 200 pixels of displacement each, while
the reader is scrolling through it.

Passing the real pixel dimensions as width and height attributes gives the
browser the aspect ratio up front and keeps loading="lazy", so neither
property has to be traded for the other. height: auto is what makes the
attributes describe a ratio rather than fix the used height.

Reddit does not always report dimensions - for posts served straight from
i.redd.it both values are zero - so the attributes are only emitted when
both are known, and those posts behave exactly as before.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.

2 participants