svimg icon indicating copy to clipboard operation
svimg copied to clipboard

images in subfolders within `static` throw an error (Sveltekit)

Open shot-codes opened this issue 2 years ago • 8 comments

i have a sveltekit app with my images in static/images/, and would like to organize them in in sub directories. However when I move files into directories I'm met with the following error Error while preprocessing ... Input file contains unsupported image format

<!-- This works with the file `/static/images/01.png` -->
<Image src="/01.png" />

<!-- This doesn't work with the file `/static/images/some-folder/01.png` -->
<Image src="/some-folder/01.png" />
// svelte.config.js

imagePreprocessor({
  inputDir: "static/images",
  outputDir: "static/images/g",
  publicPath: "/images/g",
  webp: true,
  avif: true,
})

shot-codes avatar Oct 08 '22 00:10 shot-codes

🤦 Restarting the dev server fixed it

shot-codes avatar Oct 08 '22 00:10 shot-codes

🤦 🤦 False false alarm. I am indeed having this issue.

shot-codes avatar Oct 08 '22 00:10 shot-codes

Do you have a reproduction I could look at?

xiphux avatar Oct 12 '22 00:10 xiphux

Having the same issue

developer2346 avatar Oct 21 '22 09:10 developer2346

Sorry for the delayed response. By deleting the generated files and regenerating them everything works as expected. Will leave this open for @developer2346.

shot-codes avatar Oct 22 '22 20:10 shot-codes

I had this same issue after adding a width to a file that worked before. Deleting the generated files, checking the write permissions of the folder for the generated images and restarting the dev server fixed it. For now at least.

SirNovi avatar Jan 06 '23 09:01 SirNovi

I just stumbled upon the same issue, so I think it is a really issue. ~My hypothesis is that the generated images are not picked up by vite(?) which is causing the error, and the server restart fixes this as it discovers the previously generated files.~

I think the issue is much simpler than that: The src attribute of Image links to a path within the configured inputDir and then produces an output with the full outputDir. As the outputDir is within /static it produces the following error:

files in the public directory are served at the root path.
Instead of /static/img/headshot.[...].avif, use /img/headshot.[...].avif.
// omitted
The request url "/static/img/headshot.[...].avif" is outside of Vite serving allow list.

If it is of any help, here are my svelte.config.js and vite.config.ts (though they are very standard).

svelte.config.js
import adapter from "@sveltejs/adapter-auto";
import { vitePreprocess } from "@sveltejs/kit/vite";
import { imagePreprocessor } from "svimg";

/** @type {import('@sveltejs/kit').Config} */
const config = {
  // Consult https://kit.svelte.dev/docs/integrations#preprocessors
  // for more information about preprocessors
  preprocess: [
    imagePreprocessor({
      inputDir: "static/images",
      outputDir: "static/img",
      webp: true,
      avif: true,
    }),
    vitePreprocess(),
  ],

  kit: {
    // adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
    // If your environment is not supported or you settled on a specific environment, switch out the adapter.
    // See https://kit.svelte.dev/docs/adapters for more information about adapters.
    adapter: adapter(),
  },
};

export default config;
vite.config.ts
import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vitest/config";

export default defineConfig({
  plugins: [sveltekit()],
  test: {
    include: ["src/**/*.{test,spec}.{js,ts}"],
  },
});

JP-Ellis avatar Mar 18 '23 07:03 JP-Ellis

Sorry for the delayed response. By deleting the generated files and regenerating them everything works as expected. Will leave this open for @developer2346.

I used another computer which contains the old generated image assets, and got the same issue. it works for me after deleting all the generated files

futantan avatar Mar 27 '23 09:03 futantan