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

feat(module:image): support lazy loading for image component #8157

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

ParsaArvanehPA
Copy link
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[✔] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Application (the showcase website) / infrastructure changes
[ ] Other... Please describe:

What is the current behavior?

Currently when you add loading='lazy' to an image, it doesn't work as intended. It loads the image like as the page is loaded.

Issue Number: N/A

What is the new behavior?

Now if you add nzLoading='lazy', the image is only loaded when image is in viewport.

Does this PR introduce a breaking change?

[ ] Yes
[✔] No

Other information

Copy link

zorro-bot bot commented Nov 12, 2023

This preview will be available after the AzureCI is passed.

Copy link

codecov bot commented Nov 12, 2023

Codecov Report

Attention: 7 lines in your changes are missing coverage. Please review.

Comparison is base (7c79ab3) 91.70% compared to head (aaecff1) 91.66%.

Files Patch % Lines
components/image/image.directive.ts 65.00% 6 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8157      +/-   ##
==========================================
- Coverage   91.70%   91.66%   -0.04%     
==========================================
  Files         519      519              
  Lines       17885    17899      +14     
  Branches     2747     2751       +4     
==========================================
+ Hits        16402    16408       +6     
- Misses       1181     1188       +7     
- Partials      302      303       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@simplejason simplejason changed the title Feature/support lazy loading for image component feature(module:image): support lazy loading for image component Nov 13, 2023
@HyperLife1119
Copy link
Collaborator

HyperLife1119 commented Nov 13, 2023

Should we consider using <img loading="lazy" /> directly?
https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/loading

NgOptimizedImage also uses the same technique
https://angular.io/api/common/NgOptimizedImage#instance-properties

@ParsaArvanehPA
Copy link
Contributor Author

ParsaArvanehPA commented Nov 13, 2023

Should we consider using <img loading="lazy" /> directly? https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/loading

NgOptimizedImage also uses the same technique https://angular.io/api/common/NgOptimizedImage#instance-properties

Unfortunately, this method is ineffective. The reason is that in image.directive.ts, the image element is created using document.createElement('img'). Once the element is created, its src attribute is immediately assigned, triggering the browser to download it. I attempted to set its loading attribute before setting its src, but it did not enable lazy loading.

I implement a manual approach to verify when the newly created img element appears in the viewport. I utilize IntersectionObserver, which ensures a high performance.
When the img element enters the viewport, its src attribute is assigned, causing it to be downloaded by the browser.

@simplejason
Copy link
Member

Thanks for your contribution :) @ParsaArvanehPA This year we applied for some free licenses from Jetbrains(valid until Nov 14, 2024, it can be activated in All Products Pack of Jetbrains), please contact me if you need it, I've sent you a notification to your email!

@ParsaArvanehPA
Copy link
Contributor Author

Thanks for your contribution :) @ParsaArvanehPA This year we applied for some free licenses from Jetbrains(valid until Nov 14, 2024, it can be activated in All Products Pack of Jetbrains), please contact me if you need it, I've sent you a notification to your email!

Hi Jason,

Thank you so much for your wonderful message and your generous offer. I am very happy and honored to be part of the ng-zorro-antd open source community. I enjoy working on this project and I am glad that my code has been useful and helpful.

I am very grateful for the license from JetBrains that you have applied for me. This is a very valuable and thoughtful gift, and I appreciate your kindness and generosity. I have registered an account on JetBrains with my email address, and I've sent you my account info via email.

I look forward to using JetBrains products and continuing to support our ng-zorro-antd community. Thank you again for your support!

@HyperLife1119 HyperLife1119 changed the title feature(module:image): support lazy loading for image component feat(module:image): support lazy loading for image component Nov 17, 2023
Copy link
Member

@simplejason simplejason left a comment

Choose a reason for hiding this comment

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

Just left some small nits :)

@@ -45,6 +47,7 @@ export class NzImageDirective implements OnInit, OnChanges, OnDestroy {

@Input() nzSrc = '';
@Input() nzSrcset = '';
@Input() nzLoading: nzLoading = 'eager';
Copy link
Member

Choose a reason for hiding this comment

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

type nzLoading can be removed.

-- @Input() nzLoading: nzLoading = 'eager';
++ @Input() nzLoading?: 'eager' | 'lazy';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.


private backLoadCompleteStateHandler(): void {
this.status = 'normal';
this.getElement().nativeElement.loading = this.nzLoading;
Copy link
Member

Choose a reason for hiding this comment

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

This attribute loading is not supported by chrome browsers lower than version 77, so it's better to not add it if nzLoadingType is not set. (https://caniuse.com/?search=eager)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I apologize for the delay.
This could be removed entirely, as setting the image element's lazy loading does not produce the desired outcome.

…component

# Conflicts:
#	components/image/doc/index.en-US.md
#	components/image/doc/index.zh-CN.md
#	components/image/image.directive.ts
@ParsaArvanehPA
Copy link
Contributor Author

Greetings @simplejason,
I am wondering if you have had a chance to look at the changes I made to my pull request based on your comments. I have been waiting for your feedback and approval for some time now. Is there anything else I need to do to move this process forward? Please let me know as soon as possible. Thank you for your time and assistance.

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

Successfully merging this pull request may close these issues.

None yet

3 participants