Generate assets images

Hi everyone! 👋 I’m using nuxt generate with ssr: true to create a static site and I have images stored in the assets/images folder at the root of my project. I’d like to define dynamic paths to these images in a composable. How can I configure this properly to make sure the images work correctly in production build?
3 Replies
kapa.ai
kapa.ai4w ago
Hi @Guillaume. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To handle images stored in the assets/images folder for a static site generated with Nuxt, there are a few important considerations to keep in mind: 1. Images in the assets directory are processed by the build tool (Vite or webpack) and are not directly served as-is Getting Started: Assets (https://nuxt.com/docs/getting-started/assets). 2. For static sites, it's recommended to use the public directory for assets that should be served directly Getting Started: Assets (https://nuxt.com/docs/getting-started/assets). 3. If you want to use images from the assets directory, you need to ensure they are properly processed during the build. Here's how you can approach this: 1. Move your images to the public directory: This is the simplest solution. Move your images from assets/images to public/images. Then, you can reference them in your composable using paths like /images/your-image.jpg. 2. If you want to keep the images in the assets directory: You'll need to import the images in your component or composable. This way, the build tool will process them correctly. For example:
import { defineNuxtConfig } from 'nuxt/config'
import myImage from '~/assets/images/my-image.jpg'

export default defineNuxtConfig({
// your config here
})

import { defineNuxtConfig } from 'nuxt/config'
import myImage from '~/assets/images/my-image.jpg'

export default defineNuxtConfig({
// your config here
})

Then you can use myImage in your template or pass it to components. 3. Use Nuxt Image module: The Nuxt Image module provides optimized image handling. You can install it using:
npx nuxi@latest module add image

npx nuxi@latest module add image

Then, configure it in your nuxt.config.ts Nuxt Image Configuration (https://image.nuxt.com/get-started/configuration):
export default defineNuxtConfig({
image: {
dir: 'assets/images'
}
})

export default defineNuxtConfig({
image: {
dir: 'assets/images'
}
})

This allows you to use the <NuxtImg> component or useImage composable to reference images from your assets/images directory. Remember, when using nuxt generate, make sure your images are accessible during the build process. If you're using dynamic imports or runtime logic to determine image paths, you might need to use the nitro.prerender.routes option to ensure all necessary images are generated Static Images (https://image.nuxt.com/advanced/static-images). For the most reliable setup, especially for static sites, I recommend moving your images to the public directory. This ensures they're served directly without any processing complications. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@248111356652290048> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Guillaume
GuillaumeOP4w ago
When I build my site, I don't have my images in the .output/public/_ipx/ folder. I only have some of them, the others are in the .output/public/assets/ file and if I inspect my images, it looks for them in the _ipx folder.
Want results from more Discord servers?
Add your server