#How to dynamically load images using glob?
8 messages · Page 1 of 1 (latest)
Hi @hollow lagoon. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To dynamically load images using glob in Astro, you should use Vite’s import.meta.glob() function instead of Astro.glob(), as Astro.glob() is deprecated. Here's how you can achieve this:
-
Use
import.meta.glob()to import images dynamically. This function returns an object where each key is a file path, and the value is a function that imports the file. -
You can then access the images using the keys and call the import function to load the image.
Here's an example of how you can set this up:
---
// Import the Image component from Astro
import { Image } from 'astro:assets';
// Use import.meta.glob to dynamically import images
const images = import.meta.glob('/src/assets/*.{jpeg,jpg,png,gif}');
// Access a specific image using its path
const imagePath = './assets/avatar-1.jpg'; // Example path
if (!images[imagePath]) throw new Error(`"${imagePath}" does not exist in glob: "src/assets/*.{jpeg,jpg,png,gif}"`);
---
<div class="card">
<Image src={images[imagePath]()} alt="Description of the image" />
</div>
In this example, import.meta.glob() is used to import all images from the specified directory. You can then access a specific image by its path and use it in your component.
For more detailed instructions, you can refer to the Astro documentation on dynamically importing images.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@hollow lagoon Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@foggy summit
Why is this not working?
const basePath = "/src/assets/gradients/";
const images = import.meta.glob("/src/assets/gradients/*.png");
const fileName = number + ".png";
const imagePath = basePath + fileName;
const src = images[imagePath]();
<Image
src={src}
alt=""
class="col-span-full h-full w-fit rounded-3xl lg:col-span-4"
/>
Let me check the knowledge base for you again. One moment! :books:
The issue with your code might be related to how the import.meta.glob() function handles file paths. The paths used in import.meta.glob() must match exactly with the keys returned by the function, including the relative path from the root of the project.
Here are a few things to check and adjust:
-
Ensure Correct Path: Make sure that the
imagePathyou are constructing matches exactly with the keys in theimagesobject. The path should be relative to the root of the project and should match the pattern used inimport.meta.glob(). -
Check for Errors: Add error handling to ensure that the
imagePathexists in theimagesobject before trying to access it. This can help you catch any issues with path mismatches.
Here's an example of how you might adjust your code:
const basePath = "/src/assets/gradients/";
const images = import.meta.glob("/src/assets/gradients/*.png");
const fileName = number + ".png";
const imagePath = basePath + fileName;
// Check if the imagePath exists in the images object
if (!images[imagePath]) {
throw new Error(`"${imagePath}" does not exist in glob: "/src/assets/gradients/*.png"`);
}
const src = images[imagePath]();
-
Verify File Existence: Double-check that the file actually exists in the specified directory and that the file name is correct.
-
Console Logging: Use
console.log(Object.keys(images))to see the exact keys returned byimport.meta.glob(). This can help you verify that your constructedimagePathmatches one of these keys.