Closed matthewp closed 1 year ago
This is great, thank you for all of your work. In my mind nailing the image story is critical as it is one of the most difficult parts of building for the web that is so often overlooked.
In the meantime, I hope that issue 6010 can get resolved. I was really looking forward to using a combination of SSG and SSR post 2.0 on a few sites with prerender but cannot until this issue is resolved. I'd love to offer a hand to help.
Are there any downsides to adding dimensions to the output image filename?
/_astro/my_image.640_960.hash.webp"
?
Transforming images on SSR is always bad, just adding sharp to function dependencies increases cold start noticeably 😬
I'm considering compiling a static version first especially for compiling the images, I would use a wrapper with some adaptations for <Image>
and <Picture>
, move the compiled images and then build and deploy with SSR.
I can read the dimensions picture by picture, but it would be much simpler if it was already in the filename, and I think this could be useful in other ways as well. Wdyt?
My bad, it's necessary just a bash line to list images with respective sizes...
And yes, at least for Firebase Functions prevent publishing
@astrojs/image
has a really considerable great impact. If someone is interested I'm runningastro build
twice on production build, first one with static output (reduced paths withgetStaticPaths
) to transform the images, second one with Node adapter. Copy the compiled images todist/server/
and create a "manifest" file, on second build I'm not using@astrojs/image
source, replacing it with Vite alias to a customPicture.runtime.astro
.
This proposal is now in Stage 3! https://github.com/withastro/roadmap/pull/500
Body
Summary
This proposal aims to outline a plan for a core story for images in Astro, doing so by:
src/content
)Background & Motivation
src/assets
folderUsing images in Astro is currently a bit confusing. Should my images go in
public
orsrc
? How do I refer to them?For this, we'd like to introduce a
src/assets
folder. This folder would be used for your source assets, as in, your not optimized, raw assets. Usage of this folder would be recommended, but not forced.To make it easier to use this folder from anywhere in the project, an alias will be provided so it is possible to write
~/assets
. Due to necessitatingtsconfig.json
changes, this will only affect new projects, and is completely optional.Content Collection integration
It is fairly common for one of the property of a Markdown piece of content to need to be a reference to an asset (think, cover image for an article, picture for an author etc).
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 thesrc/assets
folder. This would allow us to provide validation of the asset, thinkcover needs to be a png of size 240x240
, including validating that the file actually exists.Facts
src/assets
is supported and they would be optimized, like before.src/assets
is also supported (ex:![...](./image.png)
) in addition to thesrc/assets
folder (ex:![...](~/assets/image.png)
)public
folder can still be referred to, however, they won't be optimized / transformed. We would like for this folder to be kept for its original purpose, for assets that will be copied as-is.src/assets
folder. Remote images are currently out of scope and still need to be referred to using az.string
.src
and they'll be optimized.![]
syntax and refer to images insrc/assets
or relative to the file and they'll be optimized.src/assets
.Image component
(see https://github.com/withastro/roadmap/discussions/447 for an earlier version of this proposal)
The current
Image
component can be confusing to use at time. Why do I need to set anaspectRatio
? (what even is the aspect ratio of my image?) Why is my image getting cropped? What format should I use? What doesquality
means?width, height and aspectRatio
We'd like to make it so less properties are needed in general. In most cases, the
width
andheight
would be automatically inferred from the source file and not passing them would just result in the same dimensions, but a smaller weight.For cases where they're needed, we'd like to remove the
aspectRatio
property, instead preferring that users manually set bothwidth
andheight
. Combined with [[#Better behaviour for resizing images]], we believe that this will lead to a more consistant and easier to resonate about experience.format
format
would be set towebp
by default, removing the need for it to be passed completely. We believe WebP to be a sensible default for most images, providing with a smaller file size while not sacrificing on compatibility and support for transparency.quality
For
quality
, we'd like to make it easier for people to use without needing to know the in-and-out of the loader's algorithm for quality. We propose to fix this by introducing different presets that users can choose from, for example:quality: 'low'
would automatically set an appropriate, approximative quality that is considered 'low' (specific implementation is left to the loaders to decide.)Better behaviour for resizing images
Despite the tremendous power it offered, users were often confused by how
@astrojs/image
would crop their images to fit the desired aspect ratio. This was especially confusing for users coming from other popular frameworks that don't offer this feature.As such, this feature would be removed of the base toolkit. Resizing would now only operate in ways that follow the original image aspect ratio (based on
width
). To control how an image is fitted inside its container, theobject-fit
andobject-position
CSS properties can be used.For users used to other frameworks, this is a similar behaviour to the one offered by NextJS's
next/image
and Eleventy'seleventy-img
.Facts
src
. Remote images (ex:http://example.com/image.png
,/image.png
or${import.meta.env.BASE_URL}/image.png
) would however requirewidth
andheight
to be set manually.src
can be a dynamic import, but be aware that it must respect Vite's limitations on dynamic importsformat
would be set towebp
by default, with the possibility to override as needed. If you desire to have the same format as the original, for remote images it needs to be manually provided whereas for local images, it is possible to doformat={myImage.format}
.quality
now accepts two types of values, either a preset (low | mid | high | max
) or a specific number (0 - 100
).alt
is a required property in all cases. It can be explicitly set to""
, for cases where an alt-text is not required.loading="lazy"
anddecoding="async"
, with possibility to override as needed.src
,width
,height
,quality
, and all the properties natively available on theimg
tag (alt
,loading
,decoding
,style
etc)Shape of ESM imports
Currently, importing images in Astro returns a simple
string
with the path of the image. The@astrojs/image
integration enhance this by instead returning the following shape:{src: string, width: number, height: number, format: string}
, allowing users to easily constructimg
tags with no CLS:This shape should also give most of the information an user could need to build their own image integrations based on ESM imports.
Since this would be directly in core, there would be no more configuration changes needed to get the proper types and as such, editor integration should be painless (completions, type checking etc) and address user confusion around this.
In order to avoid any breakage, this shape would be under an experimental flag until the next major release of Astro. Similarly to content collections, we would automatically update your
env.d.ts
with the type changes needed for this.Goals
alt
,loading
,decoding
)Non-goals of this proposal
srcset
.svg
supportWe realize that many of those features not being goals are considered to be downgrades compared to
@astrojs/image
. This is on purpose as we'd like to approach this project with a "small MVP, add features over time" mentality.Creating a perfect
Image
component for core is a way more complex project than it might seems, the balance between "This has too much options, it's too confusing to use" and "This doesn't have enough options, it's unusable for anything even a little bit complex" is incredibly hard to hit.We believe that, much like we did with other features in Astro, by building a good core and providing ways build around it, we'll ultimately achieve the best result possible.
Example
Basic image from
src/assets
Source
Result
Resized image from
src/assets
Source
Result
Source
Result
Basic remote image
Source
Result (3rd example)
Markdown
Source
Source image is a .png of 640x960
Result
Result is a .webp of 640x960
Source
Result