N
Nuxt4d ago
Joey

Setting up NuxtImg with an external cdn using ipx

Hello, I want to use Nuxt Image to serve images from an external cdn locally (from within the public folder), but I can't get the configuration right. Could anyone provide me with an example configuration for this?
13 Replies
kapa.ai
kapa.ai4d ago
Hi @Joey. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To set up Nuxt Image to serve images from an external CDN locally using IPX, you need to configure the domains option in your Nuxt configuration. Here's an example of how you can do this:
export default defineNuxtConfig({
image: {
domains: [
'your-cdn-domain.com'
],
// Other image module options...
}
})
export default defineNuxtConfig({
image: {
domains: [
'your-cdn-domain.com'
],
// Other image module options...
}
})
Replace 'your-cdn-domain.com' with the actual domain of your external CDN. This configuration allows Nuxt Image to process images from the specified external domain. When you use the <NuxtImg> or <NuxtPicture> components with a src pointing to this domain, Nuxt Image will handle the optimization. For local images in the public folder, you don't need any special configuration. Nuxt Image will automatically handle these. Here's an example of how you might use it in your Vue template:
<template>
<!-- For an image from your CDN -->
<NuxtImg src="<https://your-cdn-domain.com/image.jpg"> />

<!-- For a local image in the public folder -->
<NuxtImg src="/local-image.jpg" />
</template>
<template>
<!-- For an image from your CDN -->
<NuxtImg src="<https://your-cdn-domain.com/image.jpg"> />

<!-- For a local image in the public folder -->
<NuxtImg src="/local-image.jpg" />
</template>
Remember that when using the default IPX provider, URL aliases are shortened on the server-side, which is especially useful for optimizing external URLs. Configuration (https://image.nuxt.com/get-started/configuration#domains) provides more details on setting up domains for remote images. If you need more specific configuration options or encounter any issues, please provide more details about your setup and requirements. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4d ago
<@699655702314221588> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Joey
JoeyOP4d ago
@kapa.ai
kapa.ai
kapa.ai4d ago
Let me check the knowledge base for you again. One moment! :books: Sorry @Joey, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Joey
JoeyOP4d ago
@kapa.ai I'm getting this error:
{
"url": "/_ipx/s_2560x576/https://your-cdn-domain.com/fz/home/sliders/nl/slider-algemeen/background_slider-achtergrond.webp",
"statusCode": 404,
"statusMessage": "Page not found: /_ipx/s_2560x576/https://your-cdn-domain.com/fz/home/sliders/nl/slider-algemeen/background_slider-achtergrond.webp",
"message": "Page not found: /_ipx/s_2560x576/https://your-cdn-domain.com/fz/home/sliders/nl/slider-algemeen/background_slider-achtergrond.webp",
"stack": "",
"data": {
"path": "/_ipx/s_2560x576/https://your-cdn-domain.com/fz/home/sliders/nl/slider-algemeen/background_slider-achtergrond.webp"
}
}
{
"url": "/_ipx/s_2560x576/https://your-cdn-domain.com/fz/home/sliders/nl/slider-algemeen/background_slider-achtergrond.webp",
"statusCode": 404,
"statusMessage": "Page not found: /_ipx/s_2560x576/https://your-cdn-domain.com/fz/home/sliders/nl/slider-algemeen/background_slider-achtergrond.webp",
"message": "Page not found: /_ipx/s_2560x576/https://your-cdn-domain.com/fz/home/sliders/nl/slider-algemeen/background_slider-achtergrond.webp",
"stack": "",
"data": {
"path": "/_ipx/s_2560x576/https://your-cdn-domain.com/fz/home/sliders/nl/slider-algemeen/background_slider-achtergrond.webp"
}
}
kapa.ai
kapa.ai4d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Based on the error message you're seeing, it appears that Nuxt Image is trying to process an external image URL but is encountering a 404 (Not Found) error. This could be due to a few reasons, but let's address the most likely cause and solution. The error suggests that the image URL is being processed by the IPX provider (as indicated by the /_ipx/ path), but it's not able to fetch or process the image from the external CDN. Here are a few steps to troubleshoot and potentially resolve this issue: 1. Double-check your domains configuration: Make sure you've correctly configured the domains option in your nuxt.config.ts file. It should include the exact domain of your CDN:
export default defineNuxtConfig({
image: {
domains: ['your-cdn-domain.com']
}
})

export default defineNuxtConfig({
image: {
domains: ['your-cdn-domain.com']
}
})

Configuration (https://image.nuxt.com/get-started/configuration#domains) provides more details on setting up domains for remote images. 2. Verify the image URL: Ensure that the image URL is correct and accessible. Try opening the URL directly in your browser to confirm it's valid. 3. Consider using the alias option: If you're frequently using images from the same CDN, you can set up an alias to simplify the URLs and potentially avoid issues. Here's how you can do it:
export default defineNuxtConfig({
image: {
domains: ['your-cdn-domain.com'],
alias: {
cdn: '<https://your-cdn-domain.com'>
}
}
})

export default defineNuxtConfig({
image: {
domains: ['your-cdn-domain.com'],
alias: {
cdn: '<https://your-cdn-domain.com'>
}
}
})

Then, in your component, you can use the alias like this:
<NuxtImg src="/cdn/fz/home/sliders/nl/slider-algemeen/background_slider-achtergrond.webp" />

<NuxtImg src="/cdn/fz/home/sliders/nl/slider-algemeen/background_slider-achtergrond.webp" />

Configuration (https://image.nuxt.com/get-started/configuration#alias) provides more information about using aliases. 4. Check for CORS issues: If your CDN has strict CORS (Cross-Origin Resource Sharing) policies, it might prevent Nuxt Image from fetching the image. You may need to configure your CDN to allow requests from your application's domain. 5. Ensure proper deployment: If you're using a custom dir option for the IPX provider, make sure you're deploying that directory along with your application. As noted in the Configuration (https://image.nuxt.com/get-started/configuration#dir) documentation, "For ipx provider, make sure to deploy customized dir as well." If you've verified all these points and are still encountering issues, you might want to consider using a different provider that's specifically designed for external CDNs, such as the Cloudinary or Imgix providers, if they're compatible with your CDN setup. Remember, some providers may have limitations when it comes to processing external URLs at runtime. If you continue to face issues, you might need to explore alternative approaches or consult the specific documentation for your chosen CDN and Nuxt Image provider. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4d ago
<@699655702314221588> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Joey
JoeyOP3d ago
@kapa.ai How do I let ipx create extra images in the public folder that are not in the template during the npm run generate process?
kapa.ai
kapa.ai3d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- To generate additional images using IPX during the npm run generate process, even if they are not directly referenced in your templates, you can use the nitro.prerender.routes option in your Nuxt configuration. This approach is particularly useful for static site generation. Here's how you can set it up: 1. First, make sure you're using the ipxStatic provider, which is automatically used when running nuxt generate. 2. In your nuxt.config.ts file, add the following configuration:
export default defineNuxtConfig({
nitro: {
prerender: {
routes: [
'/_ipx/w_120/your-image.jpg',
'/_ipx/w_140/your-image.jpg',
// Add more routes for additional images and sizes
]
}
}
})
export default defineNuxtConfig({
nitro: {
prerender: {
routes: [
'/_ipx/w_120/your-image.jpg',
'/_ipx/w_140/your-image.jpg',
// Add more routes for additional images and sizes
]
}
}
})
This configuration tells Nuxt to prerender these specific image routes during the generate process, even if they're not referenced in your templates. Advanced (https://image.nuxt.com/advanced/static-images) provides more information about optimizing images for static websites. It's important to note that if you have disabled server-side rendering (ssr: false in the nuxt.config), you'll need to explicitly set up the prerendering routes as shown above. This is because Nuxt Image won't be able to optimize your images during the static generation process when SSR is disabled. Remember to adjust the image paths and sizes in the routes array to match your specific requirements. Each entry should follow the format '/_ipx/[modifiers]/[image-path]', where [modifiers] are the IPX modifiers you want to apply (like width, height, etc.), and [image-path] is the path to your image relative to the public directory. By using this approach, you can ensure that IPX generates these additional image variants in your public folder during the npm run generate process, even if they're not explicitly used in your templates. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3d ago
<@699655702314221588> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Joey
JoeyOP3d ago
@kapa.ai Can I also do this from within pages/components?
kapa.ai
kapa.ai3d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Yes, you can generate additional image routes for prerendering from within your pages or components in Nuxt 3. This can be done using the prerenderRoutes function, which allows you to dynamically add routes for prerendering during runtime. Here's how you can do it: 1. In your page or component, you can use the prerenderRoutes function within a <script setup> block:
<script setup>
prerenderRoutes([
"/_ipx/w_120/your-image.jpg",
"/_ipx/w_140/your-image.jpg"
]);
</script>

<template>
<div>
<!-- Your component content -->
</div>
</template>
<script setup>
prerenderRoutes([
"/_ipx/w_120/your-image.jpg",
"/_ipx/w_140/your-image.jpg"
]);
</script>

<template>
<div>
<!-- Your component content -->
</div>
</template>
This approach allows you to dynamically add routes for prerendering based on your component's logic or data. Docs > Getting-started > Prerendering > Runtime prerender configuration (https://nuxt.com/docs/getting-started/prerendering#runtime-prerender-configuration) provides more information about using prerenderRoutes within a Nuxt context. It's important to note that this method will only work when the page containing this code is actually rendered during the generate process. If you need to ensure these routes are always added, regardless of whether the page is visited, you might want to consider using a plugin or the nuxt.config.ts approach mentioned in the previous answer. Remember that when using this method, the routes you add should follow the IPX format: '/_ipx/[modifiers]/[image-path]', where [modifiers] are the IPX modifiers you want to apply (like width, height, etc.), and [image-path] is the path to your image. This approach gives you more flexibility to dynamically generate image routes based on your application's runtime state or component-specific requirements. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3d ago
<@699655702314221588> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?