Skip to content

Commit

Permalink
Load lazy to fix safari loading, also grab different width when devic…
Browse files Browse the repository at this point in the history
…ePixelContentBoxSize is not available
  • Loading branch information
BobWez98 committed Dec 10, 2024
1 parent 911703c commit 5d65d8e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
9 changes: 2 additions & 7 deletions resources/views/image.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,13 @@ class="{{ $class }}"
<img
{!! $attributes ?? '' !!}
class="{{ $class }}"
@if($presets['placeholder'] ?? false)
style="background-image: url('{{ $presets['placeholder'] }}'); background-size: contain; background-position: center; background-repeat: no-repeat;"
@endif
src="{{ $presets['placeholder'] ?? $image->url() }}"
alt="{{ $alt ?? $image->alt() }}"
width="{{ $width }}"
height="{{ $height }}"
loading="lazy"
onload="
this.onload=()=>{
this.style.backgroundImage = null;
this.onload=null
};
this.onload = null;
window.responsiveResizeObserver.observe(this);
"
>
Expand Down
5 changes: 2 additions & 3 deletions resources/views/partials/head.blade.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<script>
window.responsiveResizeObserver = new ResizeObserver(async (entries) => {
entries.forEach(entry => {
let imgWidth = entry?.devicePixelContentBoxSize?.[0]?.inlineSize || 0;
let imgWidth = entry?.devicePixelContentBoxSize?.[0]?.inlineSize || (entry.borderBoxSize[0]?.inlineSize * window.devicePixelRatio);
if (imgWidth === 0) {
return;
}
requestAnimationFrame(() => {
entry.target.loading = 'lazy';
entry.target.parentNode.querySelectorAll('source').forEach((source) => {
source.sizes = imgWidth + 'px'
})
Expand Down

0 comments on commit 5d65d8e

Please sign in to comment.