You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all, thanks for this great package. I was able to generate a fully reponsive picture with only a few lines of code, which is amazing! To the problem
I am trying to use the basePixels directive with React, but it does not work as expected. When rendered on the server, I get the correct 1x and 2x images, but when it renders on the client, it switches to 300w and 600w versions, and I get a hydration mismatch.
AtreehydratedbutsomeattributesoftheserverrenderedHTMLdidn't match the client properties. This won'tbepatchedup.ThiscanhappenifaSSR-edClientComponentused:
-Aserver/clientbranch`if (typeof window !== 'undefined')`.-Variableinputsuchas`Date.now()`or`Math.random()`whichchangeseachtimeit's called.
-Dateformattinginauser's locale which doesn'tmatchtheserver.-ExternalchangingdatawithoutsendingasnapshotofitalongwiththeHTML.-InvalidHTMLtagnesting.ItcanalsohappeniftheclienthasabrowserextensioninstalledwhichmesseswiththeHTMLbeforeReactloaded.https://react.dev/link/hydration-mismatch<RenderedRoutematch={{params:{}, ...}}routeContext={{...}}><Layout><htmllang="en"><head><body><App><Outlet><RenderedRoutematch={{params:{}, ...}}routeContext={{outlet:null, ...}}><ViteImageTool><main><h1><divclassName="my-6 w-[30..."><picture><source+srcSet="/@imagetools/6b96b93013bb7a72f4ff6f967dbd76974b92bfbc 300w, /@imagetools/16bf4d663..."-srcSet="/@imagetools/6b96b93013bb7a72f4ff6f967dbd76974b92bfbc 1x, /@imagetools/16bf4d6638f..."type="image/avif"><source+srcSet="/@imagetools/a9ce3b8e03e646d59be2173e1e8a5ac7b4757714 300w, /@imagetools/8c6aafbb3..."-srcSet="/@imagetools/a9ce3b8e03e646d59be2173e1e8a5ac7b4757714 1x, /@imagetools/8c6aafbb331..."type="image/webp"><source+srcSet="/@imagetools/b14e2bce7ff2b46c1553fd7dfeb18b7d3659eeae 300w, /@imagetools/f9ebb33f5..."-srcSet="/@imagetools/b14e2bce7ff2b46c1553fd7dfeb18b7d3659eeae 1x, /@imagetools/f9ebb33f5f5..."type="image/jpeg">
To be able to debug this problem, I forked and installed a local copy of vite-imagetools. I put a console.log(metadatas) into output-format.tsx > pictureFormat function and I see, that second log is missing the pixelDensityDescriptor.
Not sure if this is a bug or I am doing something wrong ... I think it might be a caching problem or maybe some weird React/Remix race condition. I can create the reproduction repo, when I get back from vacation in a few days ;)
The text was updated successfully, but these errors were encountered:
HriBB
changed the title
Directive basePixels does not work as expected with React and SSR
Directive basePixels hidration mismatch with React and SSR
Oct 2, 2024
I am looking at the source code and I see the problem. Image metadata is different when generating from scratch or reading from the cache:
basePixels is implemented inside the resize transform, which only happens when applyTransforms is executed, which of course does not happen when reading from the cache. This should probably be calculated in the output function.
So basePixels approach does not work for me. Now I am trying to use the extendOutputFormats, but the problem is that input parameters are not available inside the output function, only metadatas ...
// vite.config.tsconstcustomOutput: OutputFormat=(config)=>async(metadatas)=>{// how can we get input params here?console.log('customOutput',{ config, metadatas })return{ metadatas,todo: 'square'}}exportdefaultdefineConfig({plugins: [imagetools({extendOutputFormats: (builtins)=>({
...builtins,square: customOutput,}),}),],});// in some pageimportsquarefrom'~/image/spacenet.jpg?w=300;600&format=avif;webp;jpeg&as=square'
We could simply pass parameters as a second argument to format function here or here. Then we can pass custom parameters and do whatever we want with them in the output function.
First of all, thanks for this great package. I was able to generate a fully reponsive picture with only a few lines of code, which is amazing! To the problem
I am trying to use the
basePixels
directive with React, but it does not work as expected. When rendered on the server, I get the correct 1x and 2x images, but when it renders on the client, it switches to 300w and 600w versions, and I get a hydration mismatch.Packages
This is my component:
This is the output in the linux console (SSR)
And the output in the browser console
React complains with
To be able to debug this problem, I forked and installed a local copy of
vite-imagetools
. I put aconsole.log(metadatas)
intooutput-format.tsx
> pictureFormat function and I see, that second log is missing thepixelDensityDescriptor
.First metadata contains
pixelDensityDescriptor
:Second data does not (also some other props are missing such as
chromaSubsampling
and others):Not sure if this is a bug or I am doing something wrong ... I think it might be a caching problem or maybe some weird React/Remix race condition. I can create the reproduction repo, when I get back from vacation in a few days ;)
The text was updated successfully, but these errors were encountered: