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

chore: avoid lazy loading for print #493

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common_design.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ cd-other:
libraries/cd/cd-other/cd-skip-link.css: {}
theme:
libraries/cd/cd-other/cd-print.css: {}
js:
js/cd-print.js: {}

cd-core:
dependencies:
Expand Down
21 changes: 21 additions & 0 deletions js/cd-print.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @file
* Print helper.
*/

((Drupal) => {
'use strict';

Drupal.behaviors.cdPrint = {
attach: function (context, settings) {

// Remove lazy loading before printing.
window.addEventListener('beforeprint', (event) => {
$('img').each(() => {
$(this).removeAttr('loading');
Copy link
Contributor

Choose a reason for hiding this comment

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

$ assumes that jquery is globally available. Iframe also can be lazy loaded.

So better be more generic and use vanilla JS:

Suggested change
$('img').each(() => {
$(this).removeAttr('loading');
document.querySelectorAll('[loading="lazy"]').forEach(element => {
element.removeAttribute('loading');
});

Notes

This may not be enough to force load the images and iframes, depending on the browsers. This needs testing and if that doesn't work, re-setting the src attribute for example may do the trick.

This may also require adjusting the waiting time on the snap tool.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I've made the changes and a feature branch for RWR with this as a patch so I can test it there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The patch is now on the feature instance, where it seems to be working as intended: https://stage.response-reliefweb-int.ahconu.org/fr/central-african-republic/bulletin-humanitaire/reponse-durgence

});
});

}
};
})(Drupal);
Loading