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

RFC: A plan for a core image story #500

Merged
merged 11 commits into from
May 9, 2023
Merged

RFC: A plan for a core image story #500

merged 11 commits into from
May 9, 2023

Conversation

Princesseuh
Copy link
Member

@Princesseuh Princesseuh commented Feb 24, 2023

Summary

This RFC aims to outline a plan to simplify the usage, optimization and resizing of images in Astro without the need of an integration.

Links

@Princesseuh Princesseuh marked this pull request as ready for review February 24, 2023 18:52
Copy link
Member

@FredKSchott FredKSchott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Left some comments

proposals/0030-core-image-story.md Outdated Show resolved Hide resolved
proposals/0030-core-image-story.md Outdated Show resolved Hide resolved
In tandem with the `src/assets` folder, we'd like to introduce a way for users to specify that a specific property needs to refer to a valid asset from the `src/assets` folder:

```ts
import { asset, defineCollection, z } from "astro:content";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(bikeshed comment, not blocking my approval on this RFC)

Any thoughts on matching asset naming? My brain says that import {asset} is too generic, but I see that you're trying to match the format of z.asset() which makes sense. import {assetSchema} is less generic, but now feels so different from z.asset().

What about import {a} from 'astro:content';? Then this snippet would look like:

import { defineCollection, a, z } from "astro:content";
const blogCollection = defineCollection({
  schema: z.object({
    title: z.string(),
    image: a.asset({ width: 1120, height: 1120 }),
  }),
});

Or, another idea that's probably less controversial:

import { defineCollection, schemas, z } from "astro:content";
const blogCollection = defineCollection({
  schema: z.object({
    title: z.string(),
    image: schemas.asset({ width: 1120, height: 1120 }),
  }),
});

IDK, maybe too early to be thinking about this since it's the first custom schema we're shipping. Like I said, no need to block on this, and also happy to revisit when we add our second or third custom schema.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't say I have any particular thoughts on this. I like the schemas.asset one though, but maybe schema is a bit too foreign for users not used to Zod?

proposals/0030-core-image-story.md Outdated Show resolved Hide resolved
proposals/0030-core-image-story.md Show resolved Hide resolved
proposals/0030-core-image-story.md Show resolved Hide resolved
proposals/0030-core-image-story.md Outdated Show resolved Hide resolved
proposals/0030-core-image-story.md Show resolved Hide resolved
proposals/0030-core-image-story.md Outdated Show resolved Hide resolved
@goulvenclech
Copy link

I had spoken with @Princesseuh about how cool it would be to store blog images in the content folder: one folder per article with its MDX file and its images, rather than cluttering the public folder.

Super happy to see this feature could be landed on Astro, especially with all the optimization and resizing tools... I'm testing this immediately!

Thanks for the excellent work 😊

Comment on lines +66 to +76
```html
<!-- Remote images are not optimized or resized. Nonetheless, you still benefit from the enforced attributes and the guarantee of no CLS, albeit manually. -->
<img
src="https://example.com/image.png"
decoding="async"
loading="lazy"
width="1280"
height="720"
alt="..."
/>
```

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think pixel-density is within scope of this API?

The output would look something like this:

<img
  srcset="
    /_astro/my_image.dp1hash.webp 1600w,
    /_astro/my_image.dp2hash.webp 3200w
  "
  sizes="(min-width: 3200px) 3200px, 100vw"
  src="/_astro/my_image.dp1hash.webp"
/>

I'm not quite sure what the inputs should be. Currently the @astrojs/image Picture component accepts a widths: number[]. But sizes and srcset are available on <img> without a <picture>, so I would expect Image to support them as well.

At a minimum, I think there should be some way to tell Image to generate image files for a list of widths.

Could it be calculated from srcset? From sizes? Is widths required? I'm not sure about these details.

See also Responsive images with srcset and sizes and Optimal Images in HTML.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is considered out of scope for this current proposal (see the non-goals section, it's part of it)

You can achieve this with the APIs shipped in 2.1 technically, but in the future we aim to have official support, just not in this version.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is considered out of scope for this current proposal

As long as the APIs proposed here don't preclude it being added in the future, I'm hugely supportive of shipping one thing at a time 😄👍

@wassfila
Copy link

wassfila commented Mar 11, 2023

@Princesseuh I tried the relative asset in Markdown as described in RFC here https://github.com/withastro/roadmap/blob/feat/core-image-story/proposals/0030-core-image-story.md#image-in-markdown
with Astro 2.1.2 and experimental: { assets: true }.
The scope of this question is not to troubleshoot but only to ask if it is still in the race as part of this RFC or not ? As I could not find a reference to it in the doc
https://docs.astro.build/en/guides/images/

I was also wondering if SVG could profit of colocation without necessarily enduring any optimization or is it rather just excluded for the moment ?

@Princesseuh
Copy link
Member Author

@Princesseuh I tried the relative asset in Markdown as described in RFC here https://github.com/withastro/roadmap/blob/feat/core-image-story/proposals/0030-core-image-story.md#image-in-markdown
with Astro 2.1.2 and experimental: { assets: true }.
The scope of this question is not to troubleshoot but only to ask if it is still in the race as part of this RFC or not ? As I could not find a reference to it in the doc
https://docs.astro.build/en/guides/images/

I was also wondering if SVG could profit of colocation without necessarily enduring any optimization or is it rather just excluded for the moment ?

Relative images in Markdown are still part of the plan, as you can see on this page: https://docs.astro.build/en/guides/assets/

They're currently supported in the experimental feature, although people have been reporting issues getting it working (that I'm hoping to fix next week!)

SVGs are out of scope for this specific feature at the moment, but could certainly be included in the feature in the future (specifically meaning relative assets, not optimization)

@matthewp
Copy link
Contributor

matthewp commented May 1, 2023

We are now entering the final comment period of this RFC. If there are no objections it will be merged later this week. Thanks!

@FredKSchott
Copy link
Member

An emphatic +1 on this RFC! Thank you for all of your hard work on this @Princesseuh!

@matthewp matthewp merged commit 817f4cc into main May 9, 2023
@matthewp matthewp deleted the feat/core-image-story branch May 9, 2023 20:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants