Comments (5)
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.
I can add this shortly!
from docs.
I think I like including the built-in logic for the error with the if
statement. I say go with that! @VoxelMC
from docs.
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.
@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)
- Change HTTP method for hx directive in partials section HOT 3
- Suggestion on CSS bundling behavior HOT 2
- Internationalization Update(other docs) HOT 3
- 404 link fails for one level deep paths HOT 1
- ⚠️ Issue - The 'Build Forms With API Routes' docs is not working HOT 1
- Left Sidebar Resets to Top After Page Refresh HOT 3
- CAPTCHA recipe needs checking HOT 1
- Verify Code in Build Forms recipe HOT 3
- Low Contrast Favicon in Safari HOT 2
- Documentation advises against using remark/rehype, but markdown example includes such plugins HOT 3
- Error when submitting website theme!
- left scroll bar issue. HOT 1
- ⚠️ Issue Reading Time HOT 5
- Issue with Astro Gatsby Migration Guide HOT 3
- Incorrect code in "Build a blog" tutorial HOT 2
- Slots can also be Fragments besides HTML elements, but it's not explained HOT 1
- Add Information on Mixing SolidJS and React HOT 8
- Suggestion to Include Modern Performance Metrics Alongside TTI HOT 2
- `compilerOptions.paths` in `tsconfig` isn't respected for TypeScript Astro configs (`astro.config.ts`) HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from docs.