image icon indicating copy to clipboard operation
image copied to clipboard

Module doesn't work with nuxt layer images

Open dimaTarhan opened this issue 2 years ago • 5 comments

Hi there. Thanks for cool module. I use "nuxt-layers" with images in public folder. Also, I use default config for "nuxt-image" module for my test. Everything is good with main project images, they loaded correct.

image

As for images from layer, there are problems. I think, this images don't include in build process in time...

image

Could you give me some recommendations about that?

dimaTarhan avatar Aug 16 '23 12:08 dimaTarhan

I have the same issue. Images in the/public folder of a nuxt layer will cause 404 error for any images you want to load with nuxt-img.

Layer setup

  • Let's say layer-2 extends layer-1:
// ./layer-2/nuxt.config.ts
export default defineNuxtConfig({
  extends: ["../layer-1"],
})
  • layer-1 has a /public/image.png.

Problems statement

Using nuxt-img :

<!-- response will be 404 -->
<nuxt-img src="/image.png"/>

Using plain html:

<!-- response is 200 -->
<img src="/image.png"/>

Basically @nuxt/image should account for images from other layers.

oemer-aran avatar Nov 15 '23 09:11 oemer-aran

I fixed it by adding this to the base layer / layer-1:

// ./layer-1/nuxt.config.ts
import { createResolver } from "@nuxt/kit"
const { resolve } = createResolver(import.meta.url)

export default defineNuxtConfig({
  image: {
    dir: resolve("./public"),
  },
})
// ./layer-2/nuxt.config.ts
export default defineNuxtConfig({
  extends: ["../layer-1"],
})

But this approach will make the /public folder in the current layer not work then (layer-2/public).

oemer-aran avatar Nov 16 '23 14:11 oemer-aran

@danielroe apologies for pinging you; is there something we can do to fix this? So moving /public to layers does not work for the nuxt-image component, but works for the regular <img> tag. If you can give some pointers I can try a PR.

darthf1 avatar Dec 20 '23 09:12 darthf1

@darthf1 Here would be my debugging steps:

  1. identify it is in dev or build mode or both
  2. check what IPX is doing - is it performing a network request to get the image, or getting it from the FS directly?
  3. see what path it's trying to resolve if it's from the FS, or try the network request yourself, manually
  4. fix the bug

danielroe avatar Dec 20 '23 09:12 danielroe

Attaching reproduction: https://stackblitz.com/edit/nuxt-image-ph6pdk

Edit: After reading the code and confirming, the issue only affects dev server. In production layers work as expected.

Aareksio avatar Jan 04 '24 14:01 Aareksio