Optimize image loading
Hi everyone
I'm trying to create an image component to be optimized the loading, etc.
I see this repository on a video: https://github.com/w3cj/solid-vs-react/blob/main/solid-image-search/src/components/ImageLoader.tsx
But I'm trying to use it and facing an error:
IntersectionObserver is not defined
I need to do someting on SolidStart?GitHub
solid-vs-react/solid-image-search/src/components/ImageLoader.tsx at...
This repo has a few apps built with react and solid for comparison. - w3cj/solid-vs-react
4 Replies
In SolidStart, components run twice: once on the server, and once on the client.
IntersectionObserver
doesn't exist on the server, so, when the component runs on the server-side it throws an error.
To fix this, you could wrap your IntersectionObserver
in onMount
, which runs when the component is mounted to the DOM
Seems not working the
loadImage
:/
Do you suggest any way else to optimize images?
I try to add some console.logs
on onMount
, like before the loadImage()
but don't appear on the console and the image doens't loadThe same rule applies in NextJS: you are constructing an
IntersectionObserver
during the component's setup/render, which isn't available in SSR.
You can basically copy something like this:
https://github.com/lxsmnsyc/solid-tiny-router/blob/bf795238c341cb10b6bc333c739c784e4bbfdbbc/packages/solid-tiny-router/src/components/Link.tsx#L66-L85
Like this?
If yes, it's something wrong...
Only way to try to show a lower image while is loading is this:
But I prefer the part where the load only starts whe the user intersect the image itself
but isn't working