Omit empty srcset on content collection Markdown images - #17872
Conversation
The content layer's image renderer always wrote the srcset attribute, so an image without responsive candidates produced srcset="", which is invalid HTML. The older Markdown pipeline in vite-plugin-markdown only sets the attribute when there are candidates; mirror that here.
🦋 Changeset detectedLatest commit: 3b945b7 The changes in this PR will be included in the next version bump. This PR includes changesets to release 423 packages
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 |
Merging this PR will degrade performance by 14.04%
Performance Changes
Comparing |
|
The CodSpeed regression looks like noise rather than a real effect of this change. The only benchmark that moved is The code this PR touches is inside The change itself is a conditional spread on an array length that replaces an unconditional property assignment, on a path that runs once per Markdown image. The benchmark runs 3 iterations of a full build measured in wall-clock time, so a 1.5s → 1.7s swing on a shared runner is well within run-to-run variance. Happy to re-run the benchmark job if that would help confirm it. |
This PR contains the following updates: | Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) | |---|---|---|---| | [@astrojs/markdown-remark](https://astro.build) ([source](https://github.com/withastro/astro/tree/HEAD/packages/markdown/remark)) | [`7.2.4` → `7.3.0`](https://renovatebot.com/diffs/npm/@astrojs%2fmarkdown-remark/7.2.4/7.3.0) |  |  | | [astro](https://astro.build) ([source](https://github.com/withastro/astro/tree/HEAD/packages/astro)) | [`7.2.9` → `7.2.10`](https://renovatebot.com/diffs/npm/astro/7.2.9/7.2.10) |  |  | --- ### Release Notes <details> <summary>withastro/astro (@​astrojs/markdown-remark)</summary> ### [`v7.3.0`](https://github.com/withastro/astro/blob/HEAD/packages/markdown/remark/CHANGELOG.md#730) [Compare Source](https://github.com/withastro/astro/compare/@astrojs/markdown-remark@7.2.4...@astrojs/markdown-remark@7.3.0) ##### Minor Changes - [#​17262](withastro/astro#17262) [`f8e9458`](withastro/astro@f8e9458) Thanks [@​Princesseuh](https://github.com/Princesseuh)! - Adds MDX rendering to the `unified()` and `satteri()` processors. Both processors now compile `.mdx` files themselves. You still need to install `@astrojs/mdx` to add MDX support to your project. - [#​17262](withastro/astro#17262) [`f8e9458`](withastro/astro@f8e9458) Thanks [@​Princesseuh](https://github.com/Princesseuh)! - Adds a `recmaPlugins` option to `unified()` for adding recma (estree/JSX) plugins to the MDX compiler. ##### Patch Changes - Updated dependencies \[[`f8e9458`](withastro/astro@f8e9458)]: - [@​astrojs/internal-helpers](https://github.com/astrojs/internal-helpers)@0.11.0 </details> <details> <summary>withastro/astro (astro)</summary> ### [`v7.2.10`](https://github.com/withastro/astro/blob/HEAD/packages/astro/CHANGELOG.md#7210) [Compare Source](https://github.com/withastro/astro/compare/astro@7.2.9...astro@7.2.10) ##### Patch Changes - [#​17262](withastro/astro#17262) [`f8e9458`](withastro/astro@f8e9458) Thanks [@​Princesseuh](https://github.com/Princesseuh)! - Fixes `@astrojs/markdown-remark` being pinned to an exact version. - [#​17874](withastro/astro#17874) [`10c7e63`](withastro/astro@10c7e63) Thanks [@​astro-factory](https://github.com/apps/astro-factory)! - Fixes SSR manifest placeholder not being replaced when the server build is minified, which caused a runtime `Invalid URL` crash at server boot - [#​17869](withastro/astro#17869) [`2548abf`](withastro/astro@2548abf) Thanks [@​ematipico](https://github.com/ematipico)! - Fixes a case where the logger was improperly initialized at runtime in dev. - [#​17878](withastro/astro#17878) [`76eff3d`](withastro/astro@76eff3d) Thanks [@​ematipico](https://github.com/ematipico)! - Fixes browser heuristic caching for cached responses that include `Last-Modified` or `ETag` validators - [#​17833](withastro/astro#17833) [`413a6e7`](withastro/astro@413a6e7) Thanks [@​astro-factory](https://github.com/apps/astro-factory)! - Fixes prerender conflict warnings to correctly identify the route that first rendered a duplicate pathname, instead of misattributing the conflict to an unrelated route that merely matches the URL pattern - [#​17872](withastro/astro#17872) [`f7191cc`](withastro/astro@f7191cc) Thanks [@​jx-grxf](https://github.com/jx-grxf)! - Fixes Markdown images in content collections rendering an empty `srcset` attribute when no responsive candidates are generated. - [#​17755](withastro/astro#17755) [`157c500`](withastro/astro@157c500) Thanks [@​matthewp](https://github.com/matthewp)! - Fixes a bug where editing a content collection entry during `astro dev` on Windows kept serving stale content until the dev server was restarted. The data store now notifies the dev server directly after each write instead of relying only on the file watcher, which can miss the atomic rename that commits the write on some platforms. - Updated dependencies \[[`f8e9458`](withastro/astro@f8e9458), [`f8e9458`](withastro/astro@f8e9458)]: - [@​astrojs/internal-helpers](https://github.com/astrojs/internal-helpers)@0.11.0 - [@​astrojs/markdown-satteri](https://github.com/astrojs/markdown-satteri)@0.4.0 </details> --- ### Configuration 📅 **Schedule**: (UTC) - Branch creation - At any time (no schedule defined) - Automerge - At any time (no schedule defined) 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 👻 **Immortal**: This PR will be recreated if closed unmerged. Get [config help](https://github.com/renovatebot/renovate/discussions) if that's undesired. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate CLI](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC41OS4zIiwidXBkYXRlZEluVmVyIjoiNDQuNTkuMyIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->
Fixes #17871
Changes
srcsetattribute, so an image with no responsive candidates producedsrcset="", which is invalid HTML and is flagged by the W3C validator.packages/astro/src/content/runtime.tsnow only adds the attribute whenimage.srcSet.values.length > 0, which is the same guardpackages/astro/src/vite-plugin-markdown/images.tsalready applies. The<Image />component behaves this way too.value === ''branch is left alone, since it is what keeps an emptyaltfrom being dropped.Testing
Added
content collection images omit srcset when there are no candidatestopackages/astro/test/core-image.test.ts. It reuses the existingcore-image-ssgempty-altfixture and the same selector as the neighbouring empty-alttest, so the two assertions cover both directions of the serializer's empty-string handling.Verified against a standalone project on
astro@7.2.9(https://github.com/jx-grxf/astro-markdown-srcset-repro):<img ... src="/_astro/wide...webp" srcset="">, after: nosrcsetimage: { layout: 'constrained' }, after:srcset="/_astro/wide...webp 640w, ... 2000w"— still emitted when there are candidatessrc/pages/legacy.mdwas already rendering without the attribute, before and afterDocs
No docs change. This aligns the content layer output with the documented behaviour of the other image paths; nothing user-facing gains or loses an option.