#Image path

8 messages · Page 1 of 1 (latest)

hoary sail
#

I have the following issue.
In my website, I have these images: https://myndi.dev/about/#My-projects

They're optimized with Astro Image on build, and on pop-up you should get an unoptimized (the original) version.

The problem?

They don't show up.

This is how I call it:

<Image src={`${src}`} width={200} height={300} alt={`${alt}`} position="top" class="img-about" format="webp" quality={100}/>

In local host, I'm able to see the image using this JS function:

imgWrappers.forEach(wrapper => {
  wrapper.addEventListener('click', () => {
    const img = wrapper.querySelector('img');
    const src = img.getAttribute('src').split('&href=')[1];
    const decodedSrc = decodeURIComponent(src);
    console.log(decodedSrc);
    expandedImg.setAttribute('src', decodedSrc);
    dialog.showModal();
  });
});

I store them dynamically in a dialog box to have some sort of preview.

What is the issue?

The decoded version gives me the following path for the image /images/image.png, but on built time this paths gets changed. So in production the src path is undefined.

Does anyone have a suggestion or a better way to achieve this?
Or perhaps, how could I reference the path after it has been built?

Reasoning

The only reason I'm doing this, is because I want to access the full length of the image when showing the preview, since the Image component cuts it.

My Astro Page

About me page

plush folioBOT
#

Hi @hoary sail. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Astro provides several ways to handle images, and it seems like the issue you're facing is related to the way you're referencing your images in production.

Astro's build process optimizes and hashes images, changing their paths. This is likely why your src path is undefined in production.

Here are a few suggestions based on the information provided in the extracts:

  1. Use Absolute URL Path: If your asset lives inside of public/, you can safely reference them by absolute URL path directly in your component templates. This is the simplest way to reference an asset. For example: <img src="/penguin.png"> (source (https://docs.astro.build/en/guides/upgrade-to/v1#how-to-resolve-images--other-assets)).

  2. ESM Import: If your asset lives inside of the src/ directory, and you want automatic optimization features like filename hashing, you can use ESM import. This works inside of any JavaScript or Astro component, and returns a resolved URL to the final image. Once you have the resolved URL, you can use it anywhere inside of the component template. For example:

---
// Example: Astro will include this image file in your final build
import imgUrl from './penguin.png';
---
<img src={imgUrl} />

(source (https://docs.astro.build/en/guides/upgrade-to/v1#how-to-resolve-images--other-assets))

  1. Astro.resolve(): Starting from Astro 0.19, you can use the Astro.resolve() helper function to create an absolute URL reference from any relative path. For example:
<!-- Astro component input: -->
<img src={Astro.resolve('../images/logo.png')} />
<!-- HTML output: -->
<img src="/_astro/src/images/logo.png" />

(source (https://astro.build/blog/astro-019))

#
  1. Markdown, MDX, and Markdoc files: Relative images can now be referenced in Markdown, MDX, and Markdoc files. These will automatically be optimized and hashed by Astro’s build process. For example:
<!-- Local image stored at src/assets/stars.png -->
!**A starry night sky.** (../../assets/stars.png)

(source (https://docs.astro.build/en/guides/assets#update-your-markdown-mdx-and-markdoc-files))

  1. Astro Image Services API: If you're using an external image service, you can use the Astro Image Services API to connect Astro with the third-party image service of your choice. This API contains a small collection of hooks that can control image handling inside of Astro. For example:
// Example: A basic image service that generates custom URLs
export const service = {
 getURL({ src, width, height }) {
 return `<https://mywebsitethatgeneratesimages.com/${src}?width=${width}&height=${height}`>
 },
}

(source (https://astro.build/blog/images))

Please review these options and choose the one that best fits your use case.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@hoary sail kapa.ai is still learning and improving, please let us know how it did by reacting below

hoary sail
#

@plush folio I'm not entirely sure this solves my issue :(

plush folioBOT
#

Let me check the knowledge base for you. One moment! :books:

#

I'm sorry to hear that the suggestions provided didn't solve your issue. Unfortunately, the information provided in the extracts doesn't cover the specific issue you're facing.

Astro's build process does optimize and change the paths of images, which seems to be causing the issue with your src path becoming undefined in production. However, without more specific information or examples related to your issue, it's difficult to provide a more targeted solution.

I would recommend checking out Astro's Troubleshooting Guide (https://docs.astro.build/en/guides/troubleshooting) for more help. If you're still stuck, you could consider creating an issue (https://docs.astro.build/en/guides/troubleshooting#create-an-issue) on Astro's GitHub repository or joining the Astro Discord (https://astro.build/chat) to chat with the community and get more direct help.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: