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

Conversation

lazysoundsystem
Copy link
Contributor

Refs: OPS-11125

Types of changes

  • ✔️ Improvement (non-breaking change which iterates on an existing feature)

Description

Remove lazy loading before printing.

Steps to reproduce the problem or Steps to test

https://humanitarian.atlassian.net/browse/OPS-11125

Impact

This adds a snippet of js to all pages, to avoid printing lazy-loading images. This may be too much.

PR Checklist

  • [ x ] I have followed the Conventional Commits guidelines.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have made changes to the sub theme to reflect those in the base theme

js/cd-print.js Outdated
Comment on lines 14 to 15
$('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

js/cd-print.js Outdated
// Remove lazy loading before printing.
window.addEventListener('beforeprint', (event) => {
document.querySelectorAll('[loading="lazy"]').forEach(element => {
element.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.

Suggested change
element.removeAttr('loading');
element.removeAttribute('loading');

removeAttr is jQuery, please use removeAttribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ouch. sorry.

Copy link
Contributor

@orakili orakili left a comment

Choose a reason for hiding this comment

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

The code looks ok but not sure if that fixes the problems. As mentioned in the ticket, what was tested might not have been a good example.

I would be hesitant to add another piece of javascript to all the sites using the common design without ensuring that it actually does something useful.

@lazysoundsystem
Copy link
Contributor Author

Agreed, adding 'blocked' label until I can prove this helps.

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

Successfully merging this pull request may close these issues.

2 participants