Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lazy load attribute for images #3961

Open
HarshKapadia2 opened this issue Mar 5, 2021 · 18 comments · May be fixed by #4348
Open

Add lazy load attribute for images #3961

HarshKapadia2 opened this issue Mar 5, 2021 · 18 comments · May be fixed by #4348
Milestone

Comments

@HarshKapadia2
Copy link

HarshKapadia2 commented Mar 5, 2021

The loading="lazy" attribute for lazy loading images should be supported by Asciidoctor for HTML sites. (MDN)

Eg:

image::file_name.ext[loading="lazy", ...]

Lazy loading images increases the performance of the site.

The browser will handle the lazy loading of images. The attribute is supported in the major browsers as well. The support will only increase, so I think it is a good feature that can be added.

@mojavelinux
Copy link
Member

This seems like a reasonable request and therefore will support it. However, since lazy is a switch, it should be declared as an option.

image::file_name.ext[opts=lazy]

or

[%lazy]
image::file_name.ext[]

The converter would still output the loading attribute, of course.

I will accept a PR to include this in Asciidoctor 2.1.x.

@mojavelinux mojavelinux added this to the v2.x milestone Mar 7, 2021
@HarshKapadia2
Copy link
Author

HarshKapadia2 commented Mar 7, 2021

Thank you for accepting the request!

The loading attribute can have the value eager and lazy both (MDN), so the syntax that I had suggested in the original comment (also shown below) makes more sense to me, @mojavelinux. It seems clearer and more understandable to me as well.

The syntax that I had suggested:

image::file_name.ext[loading="lazy", ...]

@mojavelinux
Copy link
Member

The loading attribute can have the value eager and lazy

That's true, but eager is the default. So there's no point specifying it. That's what allows us to look at "lazy" as a switch.

@slonopotamus
Copy link
Contributor

That's what allows us to look at "lazy" as a switch.

That might introduce problems in the future if browsers decide to add a third possible value.

@mojavelinux
Copy link
Member

At that point, we could introduce an attribute. But since no such value is anywhere in sight, I think we would be overdesigning by trying to accommodate it.

@KartikSoneji
Copy link

KartikSoneji commented Aug 8, 2022

The loading attribute can have the value eager and lazy

That's true, but eager is the default. So there's no point specifying it. That's what allows us to look at "lazy" as a switch.

If this is an attribute, it can also be declared globally as :image-loading: lazy, and then overridden on a case-by-case basis with image::file_name.ext[loading=eager] (or vice-versa).
I don't think there is an easy way to do that with options?

@mojavelinux
Copy link
Member

I don't think there is an easy way to do that with options?

Yes there is. It would be image-lazy-option if we were to go that route.

@KartikSoneji
Copy link

I don't think there is an easy way to do that with options?

Yes there is. It would be image-lazy-option if we were to go that route.

Hmm and how would it be overridden (added/removed) for one instance?

@mojavelinux
Copy link
Member

You'd need to unset the attribute using an attribute entry, then set it again below the element. There's currently no way to indicate that an option be turned off on an element. That's something we hope to add to the language in the future.

@KartikSoneji
Copy link

In that case, I think an attribute will result in cleaner markup?
Additionally, since the HTML spec specifies loading=lazy and loading=eager, I feel it makes more sense to mirror that.
(For what it's worth, there used to be a third non-standard value supported by chromium, loading=auto but that has now been deprecated)

To allow switch-like behaviour, image::file_name.ext[opts=lazy] can be an alias for image::file_name.ext[loading=lazy]

@KartikSoneji
Copy link

@mojavelinux any update on the issue?
I'll be happy to create a PR for whichever form you think is best.

@mojavelinux
Copy link
Member

I'd be open to a PR. This issue is not a priority for me, but I will help push it through if you do the implementation and docs.

@mojavelinux mojavelinux changed the title Add lazy load attribute for images. Add lazy load attribute for images Aug 21, 2022
@arturo32
Copy link

While this new feature is being developed, is there a way to add these attributes to images somewhere else before the generation of the HTML file?

@KartikSoneji
Copy link

@arturo32 for now, we are using a very hacky regex

 # Lazy load images
 sed -i -e 's/<img/<img loading="lazy"/g' "${public_dir}/html/summary/combined-summary.html";

https://github.com/OurTechCommunity/catchup/blob/72783d38e7c7d9f3b5902e4f98c904287183f842/util/build.sh#L101-L102

@arturo32
Copy link

@arturo32 for now, we are using a very hacky regex

 # Lazy load images
 sed -i -e 's/<img/<img loading="lazy"/g' "${public_dir}/html/summary/combined-summary.html";

https://github.com/OurTechCommunity/catchup/blob/72783d38e7c7d9f3b5902e4f98c904287183f842/util/build.sh#L101-L102

Thank you very much! I think that will do!

@KartikSoneji
Copy link

Happy to help, but I can't vouch for using that for anything mission critical.
The safest way would be to patch your asciidoctor install based on my PR (#4348)

@mojavelinux
Copy link
Member

It would be much cleaner to use a postprocessor extension. See https://docs.asciidoctor.org/asciidoctor/latest/extensions/postprocessor/. You could also extend the converter and override convert_image or convert_inline_image. See https://docs.asciidoctor.org/asciidoctor/latest/convert/custom/

@vogella
Copy link

vogella commented May 7, 2024

IMHO it would be nice to have a general option to make all images lazy.

We currently use the following slim extension:

= block_with_title({class: 'imageblock', style: (style_value text_align: (attr :align), float: (attr :float))}, :bottom)
  .content
    - if (attr :link)
      a.image href=(attr :link)
        img src=image_uri(attr :target) alt=(attr :alt) width=(attr :width) height=(attr :height) loading="lazy"
    - else
      img src=image_uri(attr :target) alt=(attr :alt) width=(attr :width) height=(attr :height) loading="lazy"

AFAICS this works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants