Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image component doesn't recognize local images in the new Content Layer API #12025

Closed
1 task
frenicohansen opened this issue Sep 18, 2024 · 7 comments
Closed
1 task
Assignees
Labels
5.0 beta Related to the Astro 5.0 beta - P4: important Violate documented behavior or significantly impacts performance (priority) feat: content layer Related to the Content Layer feature (scope)

Comments

@frenicohansen
Copy link

Astro Info

Astro                    v5.0.0-beta.1
Node                     v20.9.0
System                   Linux (x64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             @astrojs/mdx
                         @astrojs/sitemap

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

After migrating to the new Content Layer API, the Image component is no longer able to recognize local images when the source is provided by a content collection. This is causing issues as the component now requires manual setting of image dimensions, which should not be necessary by default.

https://docs.astro.build/en/guides/images/#images-in-content-collections

What's the expected result?

Image component shouldn't ask for dimensions of local images

Link to Minimal Reproducible Example

https://codesandbox.io/p/devbox/z22xn5

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Sep 18, 2024
@frenicohansen frenicohansen changed the title Image component doesn't recognize local images in the new Content Layer AP Image component doesn't recognize local images in the new Content Layer API Sep 18, 2024
@i-bsd
Copy link

i-bsd commented Sep 18, 2024

Thanks for posting this issue. I wasted hours on this yesterday thinking I did something wrong.

@jurajkapsz
Copy link
Contributor

jurajkapsz commented Sep 19, 2024

I can confirm this.

The schema image() ImageFunction no longer returns an image object with src, width etc. properties but only a resolved string representing src/ relative path to the image file.

const myCollection = defineCollection({
  loader: glob({
    pattern: "**/*.mdx",
    base: "./src/data/collection",
  }),
  schema: ({ image }) =>
    z.object({
      title: z.string(),
      image: image(), // <-- this
    }),
});
Astro                    v4.15.7
Node                     v20.11.1
System                   Linux (x64)
Package Manager          pnpm
Output                   hybrid
Adapter                  @astrojs/node
Integrations             @astrojs/mdx
                         @astrojs/sitemap

@Princesseuh
Copy link
Member

This seems to be caused by the path being absolute, modifying the reproduction to use a relative path makes it work

@Princesseuh Princesseuh added - P4: important Violate documented behavior or significantly impacts performance (priority) feat: content layer Related to the Content Layer feature (scope) and removed needs triage Issue needs to be triaged labels Sep 19, 2024
@i-bsd
Copy link

i-bsd commented Sep 19, 2024

For me, it's dynamically importing images that doesn't work at all now after upgrading (using this recipe). Never had an issue before.

@Princesseuh
Copy link
Member

For me, it's dynamically importing images that doesn't work at all now after upgrading (using this recipe). Never had an issue before.

That seems unrelated, this issue is specifically about the image() schema.

@Princesseuh Princesseuh added the 5.0 beta Related to the Astro 5.0 beta label Sep 19, 2024
@jurajkapsz
Copy link
Contributor

This seems to be caused by the path being absolute, modifying the reproduction to use a relative path makes it work

Yes, I use absolute paths for linked images eg /src/assets/images/perex/image.jpg.
I actually can't imagine to use relative paths. I'd like to say that switching to absolute paths resolved my project's maintainability.

@Princesseuh
Copy link
Member

Fixed by #12030

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5.0 beta Related to the Astro 5.0 beta - P4: important Violate documented behavior or significantly impacts performance (priority) feat: content layer Related to the Content Layer feature (scope)
Projects
None yet
Development

No branches or pull requests

5 participants