-
Hi, When I use an image inside of a Livewire component, the image is shown perfectly as expected when the component is initially loaded. But as soon as an element in the component changes the image will be replaced by the tiny placeholder (the blurry svg). For some reason this keeps happening. The only thing I can do is set use_tiny_placeholders => false in the media-library config file. Is there anyone who has experienced the same and came up with a solution? Thank you in advance! Laravel 8.27.0 |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 8 replies
-
Beta Was this translation helpful? Give feedback.
-
My workaround hack for now is to add a javascript to listen for livewire to dispatch the 'message.processed' event that in turn triggers a refresh event. (Please impove or better it as it's super-mega-fugly) To be included on any page it's required ... app.blade is a good place for site wide inclusion.
Hope it helps someone ...and hope it gets a better solution too. |
Beta Was this translation helpful? Give feedback.
-
Thanks @jayenne! This works great! |
Beta Was this translation helpful? Give feedback.
-
I have found another, and I think better solution for this. First of all you have to publish media library resources: After you publish resources, open: resources/views/vendor/media-library/responsiveImageWithPlaceholder.blade.php
I do not know why they made onload function null after loading it, but this seems like to work better than everything I tried. |
Beta Was this translation helpful? Give feedback.
-
I'm also having this problem. The solution suggested by @landg16 does work, however, in my case it leads to an infinite loop. |
Beta Was this translation helpful? Give feedback.
-
Yes, one day this solution was workong but another it didn't, so I ended up using this function on every action. For example, if i have component that has display: none and transforms to display: block after click, I have to call it to inform browser which size image it has to load.
Real problem here is that when u have display: none, images have 0 width and heigth, so you have to inform browser that image size has changed after action to load correct image src. Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
Generate UUID and set is as id of the img tag seems to solve the problem.
|
Beta Was this translation helpful? Give feedback.
-
I was having this issue not just with Livewire but also in other situations where the image was initially not visible (e.g. in an accordion, carousel or tab). It was intermittent... sometimes images would get the correct size, other times they wouldn't. This seemed to relate to browser caching but I couldn't get to bottom of it The simple solution in my case was to provide a way for me to manually set an image's width for situations where the automatic width measurement wasn't working. To do this, I published the views [as mentioned in the documentation](php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="views"
The
|
Beta Was this translation helpful? Give feedback.
-
I have a paginator connected to a grid with images. Setting the
|
Beta Was this translation helpful? Give feedback.
-
I've been experiencing this issue as well and just wanted to add my own solution, in case someone finds it useful. I created a dedicated Alpine componentHere is the code for the component: /**
* @param {HTMLElement} element
* @param {DOMRectReadOnly} rect
* @param {ResizeObserver} observer
*/
const updateSize = (element, { width, height }) => {
const size = Math.ceil((width / window.innerWidth) * 100);
element.setAttribute("sizes", `${size}vw`);
};
const resizeObserver = new ResizeObserver((entries, observer) =>
entries.forEach((entry) =>
updateSize(entry.target, entry.contentRect, observer)
)
);
Alpine.data("responsiveImage", () => ({
init() {
resizeObserver.observe(this.$el);
},
destroy() {
resizeObserver.unobserve(this.$el);
},
})); How you initialize this component will depend on your project's JavaScript setup. In most cases you should be able to wrap it in a document.addEventListener('alpine:init', () => {
// component code here
}); Blade templateYou will also need to customize the rendered HTML and add the Another piece of the puzzle is the Here is an example with added line breaks for readability: <img{!! $attributeString !!} @if($loadingAttributeValue) loading="{{ $loadingAttributeValue }}"@endif
srcset="{{ $media->getSrcset($conversion) }}" sizes="1px"
src="{{ $media->getUrl($conversion) }}" width="{{ $width }}" height="{{ $height }}"
x-data="responsiveImage()"
wire:replace.self> |
Beta Was this translation helpful? Give feedback.
-
hello, i test that alot of time and it work, you just need to add id and loading lazy {!! $image->img()->attributes(['id' => \Illuminate\Support\Str::uuid(), 'loading'=>'lazy']) !!} |
Beta Was this translation helpful? Give feedback.
I have found another, and I think better solution for this.
First of all you have to publish media library resources:
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="views"
After you publish resources, open: resources/views/vendor/media-library/responsiveImageWithPlaceholder.blade.php
You will see, that when onload function is called, it's getting null as soon as this function is getting called. all you have to do, is to remove onload=null; from onload function, and your file will look like this:
<img{!! $attributeString !!} @if($loadingAttributeValue) loading="{{ $loadingAttributeValue }}"@endif srcset="{{ $media->getSrcset($conversion) }}" …