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

Allow usage of attributes in hooks #485

Merged
merged 2 commits into from
Nov 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ setLoadedImage(imagePath: string, attributes: Attributes): void; // Logic to set
setErrorImage(error: Error, attributes: Attributes): void; // Logic to set the error image
setup(attributes: Attributes): void; // Set up function
finally(attributes: Attributes): void; // Teardown function
isBot(attributes: Attributes): boolean; // Is the user a bot?
isBot(attributes?: Attributes): boolean; // Is the user a bot?
isDisabled(): boolean; // Should the lazyload mechanism be disbled?
skipLazyLoading(attributes: Attributes): boolean; // Should we load the image ASAP?
```
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ import { isPlatformServer } from '@angular/common';
import { IntersectionObserverHooks, Attributes } from 'ng-lazyload-image';

class LazyLoadImageHooks extends IntersectionObserverHooks {
isBot(attributes: Attributes) {
isBot(attributes?: Attributes) {
// Check if the user is a bot or not.
this.navigator; // Is the same as `window.navigator` if window is defined otherwise undefined.
isPlatformServer(this.platformId); // True if the code is running on the server
Expand Down
2 changes: 1 addition & 1 deletion src/intersection-observer-hooks/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class IntersectionObserverHooks extends SharedHooks<{ isIntersecting: boo
private readonly uniqKey = {};

getObservable(attributes: Attributes<{ isIntersecting: boolean }>): Observable<{ isIntersecting: boolean }> {
if (this.skipLazyLoading()) {
if (this.skipLazyLoading(attributes)) {
return of({ isIntersecting: true });
}
if (attributes.customObservable) {
Expand Down
2 changes: 1 addition & 1 deletion src/scroll-hooks/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class ScrollHooks extends SharedHooks<Event | string> {
private readonly scrollListeners = new WeakMap<any, Observable<any>>();

getObservable(attributes: Attributes<Event | string>): Observable<Event | string> {
if (this.skipLazyLoading()) {
if (this.skipLazyLoading(attributes)) {
return of('load');
} else if (attributes.customObservable) {
return attributes.customObservable.pipe(startWith(''));
Expand Down
8 changes: 4 additions & 4 deletions src/shared-hooks/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export abstract class SharedHooks<E> extends Hooks<E> {
}

loadImage(attributes: Attributes): ObservableInput<string> {
if (this.skipLazyLoading()) {
if (this.skipLazyLoading(attributes)) {
// Set the image right away for bots for better SEO
return [attributes.imagePath];
}
Expand Down Expand Up @@ -72,11 +72,11 @@ export abstract class SharedHooks<E> extends Hooks<E> {
return isPlatformServer(this.platformId) && !this.isBot();
}

skipLazyLoading(): boolean {
return this.isBot();
skipLazyLoading(attributes: Attributes): boolean {
return this.isBot(attributes);
}

isBot(): boolean {
isBot(attributes?: Attributes): boolean {
if (this.navigator?.userAgent) {
return /googlebot|bingbot|yandex|baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest\/0\.|pinterestbot|slackbot|vkShare|W3C_Validator|whatsapp|duckduckbot/i.test(
this.navigator.userAgent
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export abstract class Hooks<E = unknown> {
abstract setErrorImage(error: Error, attributes: Attributes): void;
abstract setup(attributes: Attributes): void;
abstract finally(attributes: Attributes): void;
abstract isBot(attributes: Attributes): boolean;
abstract isBot(attributes?: Attributes): boolean;
abstract isDisabled(): boolean;
abstract skipLazyLoading(attributes: Attributes): boolean;
onDestroy(attributes: Attributes): void {}
Expand Down