feat: experimental responsive images#12377
Conversation
🦋 Changeset detectedLatest commit: cc1db75 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
* feat: add experimental responsive images config option * Apply suggestions from code review Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Update config types * Move config into `images` * Move jsdocs --------- Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* feat: add experimental responsive images config option * Apply suggestions from code review Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Update config types * Move config into `images` * Move jsdocs * wip: responsive image component * Improve srcset logic * Improve fixture * Lock * Update styling * Fix style prop handling * Update test (there's an extra style for images now) * Safely access the src props * Remove unused export * Handle priority images * Consolidate styles * Update tests * Comment * Refactor srcset * Avoid dupes of original image * Calculate missing dimensions * Bugfixes * Add tests * Fix test * Correct order * Lint * Fix fspath * Update test * Fix test * Conditional component per flag * Fix class concatenation * Remove logger * Rename helper * Add comments * Format * Fix markdoc tests * Add test for style tag --------- Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* wip: add crop support to image services * Add tests * Strip crop attributes * Don't upscale * Format * Get build working properly * Changes from review
* feat: add responsive support to picture component * Add picture tests * Fix test * Implement own define vars * Share logic * Prevent extra astro-* class * Use dataset scoping * Revert unit test * Revert "Fix test" This reverts commit f9ec6e2. * Changes from review
* docs: add responsive images flag docs * docs: adds jsdoc for image components * chore: updates from review * Fix jsdoc * Changes from review
| width: resolvedOptions.width, | ||
| layout, | ||
| originalWidth, | ||
| breakpoints: isLocalService(service) ? LIMITED_RESOLUTIONS : DEFAULT_RESOLUTIONS, |
There was a problem hiding this comment.
Should people / image service be able to customise this? I'm notably talking about Vercel here, where the breakpoints needs to somewhat matches the allowed widths (hahaha)
There was a problem hiding this comment.
I did consider adding a breakpoints option to the config, though people can still set widths manually. Maybe it would be a good idea.
There was a problem hiding this comment.
OK, config now supports image.experimentalBreakpoints
|
|
||
| let imageURL = await service.getURL(validatedOptions, imageConfig); | ||
|
|
||
| const matchesOriginal = (transform: ImageTransform) => |
There was a problem hiding this comment.
In theory, couldn't the quality be different here? Or any other parameters that affects the image, for image services who support more.
There was a problem hiding this comment.
I think in this scenario it can't, as this is just checking if it's the srcset entry which is the same as the src. I don't think we expose a way to set this separately.
There was a problem hiding this comment.
Can't an image service set a different quality for a srcset transform?
There was a problem hiding this comment.
I guess in theory. I'm comfortable with not supporting that though. It's a terrible idea.
| 750, // iPhone 6-8 | ||
| 828, // iPhone XR/11 | ||
| 1080, // iPhone 6-8 Plus |
| * - For fixed layout we return 1x and 2x the requested width, unless the original image is smaller than that. | ||
| * - For responsive layout we return all breakpoints smaller than 2x the requested width, unless the original image is smaller than that. | ||
| */ | ||
| export const getWidths = ({ |
There was a problem hiding this comment.
Appreciate the comments in this one, great work.
| getSrcSet(options) { | ||
| const srcSet: UnresolvedSrcSetValue[] = []; | ||
| const { targetWidth } = getTargetDimensions(options); | ||
| getSrcSet(options): Array<UnresolvedSrcSetValue> { |
There was a problem hiding this comment.
I don't personally care all that much, but UnresolvedSrcSetValue[] is more common in our codebase
| export { inferRemoteSize } from "astro/assets/utils/inferRemoteSize.js"; | ||
|
|
||
| export const imageConfig = ${JSON.stringify(settings.config.image)}; | ||
| export const imageConfig = ${JSON.stringify({ ...settings.config.image, experimentalResponsiveImages: settings.config.experimental.responsiveImages })}; |
There was a problem hiding this comment.
might want to add the new breakpoint option here as well?
There was a problem hiding this comment.
That's part of settings.config.image.
| }), | ||
| ) | ||
| .default([]), | ||
| experimentalLayout: z.enum(['responsive', 'fixed', 'full-width', 'none']).optional(), |
There was a problem hiding this comment.
A single source of truth for this would be great, you can add it in const, derive the type from it and also use it here
There was a problem hiding this comment.
cries in single source of truth for the config types ahem it would, yes
Princesseuh
left a comment
There was a problem hiding this comment.
Left a bunch of comments, but nothing blocking. Awesome work!
Changes
Implements responsive images RFC withastro/roadmap#1051
Testing
Docs