Code Monkey home page Code Monkey logo

Comments (5)

kaishin avatar kaishin commented on May 25, 2024 2

As someone who's new to Astro and who came across this today, I'd vote for 3 as well as it would give me the choice to decide how I want to handle it.

from docs.

VoxelMC avatar VoxelMC commented on May 25, 2024 1

I can add this shortly!

from docs.

sarah11918 avatar sarah11918 commented on May 25, 2024 1

I think I like including the built-in logic for the error with the if statement. I say go with that! @VoxelMC

from docs.

VoxelMC avatar VoxelMC commented on May 25, 2024 1

As someone who's new to Astro and who came across this today, I'd vote for 3 as well as it would give me the choice to decide how I want to handle it.

Perfect - I will make that PR now for review! Please feel free to have a look

from docs.

VoxelMC avatar VoxelMC commented on May 25, 2024

@Princesseuh Sorry to bother you, but I just want to ensure this is correct!
About your comment here: #5898 (comment)

Reference to PR: #5898

So, as the import.meta.glob function returns an object with path: Function pairs, if you access a path that isn't there, the function can't be called. Here are the three strategies I would use to tackle this, and let me know which makes more sense for Astro.

1. Nullish Coalesce the function, then call it later:

This makes more sense if you can use a default image.

---
const { imagePath, ... } = Astro.props;
const images = import.meta.glob<{ default: ImageMetadata }>('src/assets/*.{jpeg,jpg,png,gif}');
const loadImage = images[imagePath] ?? images['src/assets/default-image.png'];
---
<Image src={ loadImage() } alt={...} />

2. Conditionally Render the image:

If it's better not to have the image at all.

---
const { imagePath, ... } = Astro.props;
const images = import.meta.glob<{ default: ImageMetadata }>('src/assets/*.{jpeg,jpg,png,gif}');
const loadImage = images[imagePath];
---
{ loadImage && <Image src={ loadImage() } alt={...} /> }

3. Just throw an error:

---
const { imagePath, ... } = Astro.props;
const images = import.meta.glob<{ default: ImageMetadata }>('src/assets/*.{jpeg,jpg,png,gif}');
if (!images[imagePath]) throw new Error("Image not found in glob pattern: src/assets/*.{jpeg,jpg,png,gif}");
---
<Image src={ images[imagePath]() } alt={...} />

Alternatively, I could just leave it to the reader and put this in a :::warning block:

---
const { imagePath, ... } = Astro.props;
const images = import.meta.glob<{ default: ImageMetadata }>('src/assets/*.{jpeg,jpg,png,gif}');
// if imagePath is not a valid file in the glob, images[imagePath]() will log an error.
---
<Image src={ images[imagePath]() } alt={...} />

@sarah11918 I would also greatly appreciate your opinion on this :)

from docs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.