Open HriBB opened 1 month ago
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.ts
const customOutput: OutputFormat = (config) => async (metadatas) => {
// how can we get input params here?
console.log('customOutput', { config, metadatas })
return { metadatas, todo: 'square' }
}
export default defineConfig({
plugins: [
imagetools({
extendOutputFormats: (builtins) => ({
...builtins,
square: customOutput,
}),
}),
],
});
// in some page
import square from '~/image/spacenet.jpg?w=300;600&format=avif;webp;jpeg&as=square'
I was able to get some config with:
import square from '~/image/spacenet.jpg?w=300;600&format=avif;webp;jpeg&as=square:300'
config
now contains ['300']
:
const customOutput: OutputFormat = (config) => async (metadatas) => {
console.log(config)
return { metadatas, todo: '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.
@JonasKruckenberg @benmccann I can make a PR, if you think that this is worth fixing/improving ;)
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 ;)