Skip to content

Commit

Permalink
Closes #6269: Lazyload CSS background image compatibility issue with …
Browse files Browse the repository at this point in the history
…Chrome 119+
  • Loading branch information
Miraeld committed Dec 1, 2023
1 parent f588be9 commit 1fb6c44
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 6 deletions.
8 changes: 7 additions & 1 deletion assets/js/lazyload-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ function rocket_css_lazyload_launch() {
const pairs = usable_pairs.filter(s => entry.target.matches(s.selector));
pairs.map(pair => {
if (pair) {
styleElement.innerHTML += pair.style;
const img = new Image();
img.src = pair.url;

img.onload = () => {
styleElement.innerHTML += `<style>${pair.style}</style>`;
}

pair.elements.forEach(el => {
// Stop observing the target element
observer.unobserve(el);
Expand Down
3 changes: 2 additions & 1 deletion assets/js/lazyload-css.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion assets/js/lazyload-css.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion inc/Engine/Media/Lazyload/CSS/Front/MappingFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ public function format( array $data ): array {
$url = $datum['url'];

$placeholder = "--wpr-bg-$hash";
$variable_placeholder = ':root{' . $placeholder . ': ' . $url . ';}';
$variable_placeholder = $datum['selector'] . '{' . $placeholder . ': ' . $url . ';}';
$formatted_urls[] = [
'selector' => $selector,
'style' => $variable_placeholder,
'hash' => $hash,
'url' => $url,
];
}

Expand Down
2 changes: 1 addition & 1 deletion inc/Engine/Media/Lazyload/CSS/Front/TagGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function ( $mapping_item ) use ( $loaded ) {
foreach ( $loaded as $item ) {
$loaded_content .= $item['style'];
}
$loaded_tag = "<style id=\"wpr-lazyload-bg\"></style><style id=\"wpr-lazyload-bg-exclusion\">$loaded_content</style>";
$loaded_tag = "<div id=\"wpr-lazyload-bg\"></div><style id=\"wpr-lazyload-bg-exclusion\">$loaded_content</style>";

$nostyle_content = '';
foreach ( $mapping as $item ) {
Expand Down
8 changes: 7 additions & 1 deletion src/js/global/lazyload-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ function rocket_css_lazyload_launch() {
const pairs = usable_pairs.filter(s => entry.target.matches(s.selector));
pairs.map(pair => {
if (pair) {
styleElement.innerHTML += pair.style;
const img = new Image();
img.src = pair.url;

img.onload = () => {
styleElement.innerHTML += `<style>${pair.style}</style>`;
}

pair.elements.forEach(el => {
// Stop observing the target element
observer.unobserve(el);
Expand Down

0 comments on commit 1fb6c44

Please sign in to comment.