Skip to content

Commit 8dd42bd

Browse files
authored
Merge pull request #10 from indykoning/patch-2
Prevent render blocking resizes
2 parents ad8974a + 1c1c808 commit 8dd42bd

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

resources/views/image.blade.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,18 @@ class="{{ $class }}"
2626
<img
2727
{!! $attributes ?? '' !!}
2828
class="{{ $class }}"
29+
@if($presets['placeholder'] ?? false)
30+
style="background-image: url('{{ $presets['placeholder'] }}'); background-size: contain; background-position: center; background-repeat: no-repeat;"
31+
@endif
2932
src="{{ $presets['placeholder'] ?? $image->url() }}"
3033
alt="{{ $alt ?? $image->alt() }}"
3134
width="{{ $width }}"
3235
height="{{ $height }}"
3336
onload="
34-
this.onload=null;
37+
this.onload=()=>{
38+
this.style.backgroundImage = null;
39+
this.onload=null
40+
};
3541
window.responsiveResizeObserver.observe(this);
3642
"
3743
>
Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
<script>
2-
window.responsiveResizeObserver = new ResizeObserver((entries) => {
3-
entries.forEach(entry => {
4-
const bounds = entry.target.getBoundingClientRect();
5-
const imgWidth = bounds.width;
6-
const imgHeight = bounds.height;
7-
const pixelRatio = window.devicePixelRatio * imgWidth;
8-
9-
requestAnimationFrame(() => entry.target.parentNode.querySelectorAll('source').forEach((source) => {
10-
source.sizes = pixelRatio + 'px';
11-
}));
2+
window.responsiveResizeObserver = new ResizeObserver(async (entries) => {
3+
entries.forEach(entry => {
4+
let imgWidth = entry.devicePixelContentBoxSize[0].inlineSize;
5+
6+
if (imgWidth === 0) {
7+
return;
8+
}
9+
10+
requestAnimationFrame(() => {
11+
entry.target.loading = 'lazy';
12+
entry.target.parentNode.querySelectorAll('source').forEach((source) => {
13+
source.sizes = imgWidth + 'px'
14+
})
1215
});
13-
});
16+
})
17+
});
1418
</script>

0 commit comments

Comments
 (0)