xiphux / svimg

Svelte image component with image preprocessing and lazy loading
ISC License
243 stars 15 forks source link

Breaking Bug due to `outputDir` path. Fix is to remove `static/` prefix from generated URLs. #41

Open jasongitmail opened 1 year ago

jasongitmail commented 1 year ago

I experience a breaking bug with svimg v3.2.0.

I have tested with both vitePreprocess (SvelteKit's default) and svelte-preprocess and received identical behavior.

To Reproduce

// svelte.config.js
... 
imagePreprocessor({
    inputDir: 'src/lib/images',
    outputDir: 'static/images/g',
    avif: true,
    webp: true,
}),
...

The solution is to not include static as part of the image URLs within the HTML.

System

Node v20.2.0

"svimg": "^3.2.0",
"@sveltejs/kit": "^1.20.2",
"vite": "^4.3.0",
"svelte-preprocess": "^5.0.4",
jasongitmail commented 1 year ago

Temporary workaround for development:

manuganji commented 1 year ago

I have just spent 4 hours on this but my issue turned out to be different. Posting in case someone else is also developing on MacOS and deploying to a linux environment.

The src prop was "IMG_something.JPG" but it the actual file name is "IMG_something.jpg". MacOS is not case sensitive so this was working on dev and build locally but in production.

xiphux commented 1 year ago

This is covered a bit in the documentation of the outputDir and srcGenerator preprocessor options. The default behavior to generate the image URI is built around the outputDir being a subdirectory of the inputDir (generally, with your images already existing within your static directory). If this is not the case for you, you can use the srcGenerator callback to manipulate the generated URIs to add/remove/modify path components as needed, because svimg isn't aware of the URI the outputDir ends up being served from.

jasongitmail commented 1 year ago

Thanks for the response. Up to you if you want to document it differently. FWIW, it would seem more intuitive to make both relative to the project root. I've more recently realized I want output images to be served from _app/immutable and have far-futures caching, so I've switched to a vite-imagetool solution now. But props for making this! It was the only that worked out of the box.