astro-helmet 1.4.0-beta.3 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +19 -0
  2. package/package.json +1 -1
  3. package/src/main.ts +32 -0
package/README.md CHANGED
@@ -122,6 +122,25 @@ preloadImage({
122
122
 
123
123
  Use this when you want browsers that support responsive preloads to choose from `imagesrcset` without causing an additional fallback preload in browsers that do not. Width-descriptor `imagesrcset` values such as `640w` should include `imagesizes`; density descriptors such as `1x, 2x` do not need it.
124
124
 
125
+ The preload's `imagesrcset` and `imagesizes` must match the rendered `<img>` attributes so the browser can reuse the preload. When an `<img>` uses `src` as the density fallback, include that URL as an explicit `1x` candidate in both source sets:
126
+
127
+ ```ts
128
+ preloadImage({
129
+ imagesrcset: '/images/hero.jpg 1x, /images/hero@2x.jpg 2x',
130
+ fetchpriority: 'high'
131
+ })
132
+ ```
133
+
134
+ ```html
135
+ <img
136
+ src="/images/hero.jpg"
137
+ srcset="/images/hero.jpg 1x, /images/hero@2x.jpg 2x"
138
+ alt=""
139
+ />
140
+ ```
141
+
142
+ If the `<img>` instead has `srcset="/images/hero@1.5x.jpg 1.5x, /images/hero@2x.jpg 2x"`, a DPR-1 browser selects the `1.5x` candidate rather than its separate `src`. Preloading `src` in that case will not match the image request.
143
+
125
144
  ### SEO Helper
126
145
 
127
146
  Use `createSeoHead()` when you want a small typed layer for common SEO tags while still keeping the raw `HeadItems` escape hatch.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-helmet",
3
- "version": "1.4.0-beta.3",
3
+ "version": "1.4.0",
4
4
  "type": "module",
5
5
  "description": "A document head manager for astro.",
6
6
  "author": "Ryan Voitiskis <ryanvoitiskis@pm.me> (https://ryanvoitiskis.com/)",
package/src/main.ts CHANGED
@@ -138,6 +138,7 @@ export type HeadValidationCode =
138
138
  | 'invalid-preload-as'
139
139
  | 'missing-preload-as'
140
140
  | 'responsive-image-missing-sizes'
141
+ | 'responsive-image-missing-1x'
141
142
  | 'font-preload-missing-crossorigin'
142
143
  | 'invalid-modulepreload-as'
143
144
  | 'preconnect-missing-href'
@@ -971,6 +972,22 @@ function validatePreload(
971
972
  })
972
973
  }
973
974
 
975
+ if (
976
+ as === 'image' &&
977
+ !href &&
978
+ hasDensityDescriptorImageSrcset(imagesrcset) &&
979
+ !hasOneXImageSrcsetCandidate(imagesrcset)
980
+ ) {
981
+ issues.push({
982
+ code: 'responsive-image-missing-1x',
983
+ severity: 'warning',
984
+ tagName: 'link',
985
+ path,
986
+ message:
987
+ 'Density-descriptor responsive image preloads should include the rendered image src as an explicit 1x candidate when src provides its fallback.'
988
+ })
989
+ }
990
+
974
991
  if (as === 'font' && !hasAttr(link, 'crossorigin')) {
975
992
  issues.push({
976
993
  code: 'font-preload-missing-crossorigin',
@@ -1177,6 +1194,21 @@ function hasWidthDescriptorImageSrcset(imagesrcset: string): boolean {
1177
1194
  })
1178
1195
  }
1179
1196
 
1197
+ function hasDensityDescriptorImageSrcset(imagesrcset: string): boolean {
1198
+ return imagesrcset.split(',').some((candidate) => {
1199
+ const descriptor = candidate.trim().split(/\s+/).at(-1) || ''
1200
+ return /^\d*\.?\d+x$/.test(descriptor)
1201
+ })
1202
+ }
1203
+
1204
+ function hasOneXImageSrcsetCandidate(imagesrcset: string): boolean {
1205
+ return imagesrcset.split(',').some((candidate) => {
1206
+ const parts = candidate.trim().split(/\s+/)
1207
+ const descriptor = parts.at(-1) || ''
1208
+ return parts.length === 1 || /^(?:1|1\.0+)x$/.test(descriptor)
1209
+ })
1210
+ }
1211
+
1180
1212
  function hasRobotsDirective(metaItems: MetaItem[], directive: string): boolean {
1181
1213
  return metaItems.some(
1182
1214
  (meta) =>