astro-helmet 1.4.0-beta.1 → 1.4.0-beta.2
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.
- package/README.md +19 -2
- package/index.d.ts +1 -0
- package/index.ts +1 -0
- package/package.json +1 -1
- package/src/main.ts +66 -2
package/README.md
CHANGED
|
@@ -96,6 +96,11 @@ const headItems: HeadItems = {
|
|
|
96
96
|
preconnect('https://cdn.example.com'),
|
|
97
97
|
preloadFont('/fonts/Inter.woff2', { type: 'font/woff2' }),
|
|
98
98
|
preloadImage('/images/hero.jpg', { fetchpriority: 'high' }),
|
|
99
|
+
preloadImage({
|
|
100
|
+
imagesrcset: '/images/hero-640.jpg 640w, /images/hero-1280.jpg 1280w',
|
|
101
|
+
imagesizes: '100vw',
|
|
102
|
+
fetchpriority: 'high'
|
|
103
|
+
}),
|
|
99
104
|
modulepreload('/scripts/entry.js'),
|
|
100
105
|
stylesheet('/styles/site.css')
|
|
101
106
|
],
|
|
@@ -103,6 +108,18 @@ const headItems: HeadItems = {
|
|
|
103
108
|
}
|
|
104
109
|
```
|
|
105
110
|
|
|
111
|
+
`preloadImage()` also supports responsive image preloads without a fallback `href`:
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
preloadImage({
|
|
115
|
+
imagesrcset: '/images/hero-640.jpg 640w, /images/hero-1280.jpg 1280w',
|
|
116
|
+
imagesizes: '100vw',
|
|
117
|
+
fetchpriority: 'high'
|
|
118
|
+
})
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
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.
|
|
122
|
+
|
|
106
123
|
### SEO Helper
|
|
107
124
|
|
|
108
125
|
Use `createSeoHead()` when you want a small typed layer for common SEO tags while still keeping the raw `HeadItems` escape hatch.
|
|
@@ -259,7 +276,7 @@ Meta items later in the array replace earlier items. `name` and `http-equiv` mat
|
|
|
259
276
|
|
|
260
277
|
- `rel="canonical"` links, keeping the last one.
|
|
261
278
|
- `rel="preconnect"` and `rel="dns-prefetch"` links with the same `href`, keeping the last one.
|
|
262
|
-
- `rel="preload"` and `rel="modulepreload"` links with the same `href`, `as`, `type`, `crossorigin`, `media`, `imagesrcset`, and `imagesizes`, keeping the last one.
|
|
279
|
+
- `rel="preload"` and `rel="modulepreload"` links with the same `href`, `as`, `type`, `crossorigin`, `media`, `imagesrcset`, and `imagesizes`, keeping the last one. Responsive image preloads without `href` are deduplicated by `as`, `type`, `crossorigin`, `media`, `imagesrcset`, and `imagesizes`.
|
|
263
280
|
|
|
264
281
|
Any `meta`, `link`, `style`, `script`, or `noscript` item may provide `key` to opt into explicit last-write-wins deduplication:
|
|
265
282
|
|
|
@@ -327,7 +344,7 @@ const issues = validateHeadItems(headItems, {
|
|
|
327
344
|
})
|
|
328
345
|
```
|
|
329
346
|
|
|
330
|
-
Validation returns structured issues with `code`, `severity`, `message`, `tagName`, and `path`. It checks for common mistakes such as missing titles/descriptions, duplicate or relative canonicals, invalid preloads, font preloads without `crossorigin`, incomplete Open Graph basics, relative Open Graph/Twitter image URLs, raw `innerHTML`, inline event attributes, JSON-LD serialization errors, and optional missing SRI on external scripts/styles.
|
|
347
|
+
Validation returns structured issues with `code`, `severity`, `message`, `tagName`, and `path`. It checks for common mistakes such as missing titles/descriptions, duplicate or relative canonicals, invalid preloads, preloads without a usable source, responsive image preloads that use width descriptors without `imagesizes`, font preloads without `crossorigin`, incomplete Open Graph basics, relative Open Graph/Twitter image URLs, raw `innerHTML`, inline event attributes, JSON-LD serialization errors, and optional missing SRI on external scripts/styles.
|
|
331
348
|
|
|
332
349
|
Set `requireOpenGraphImage: true` if every shareable page in your project should provide `og:image`. By default, image-less website pages are allowed so dev validation does not warn on simple pages that still have useful `og:title`, `og:type`, and `og:url` tags.
|
|
333
350
|
|
package/index.d.ts
CHANGED
package/index.ts
CHANGED
package/package.json
CHANGED
package/src/main.ts
CHANGED
|
@@ -107,8 +107,10 @@ export type HeadValidationCode =
|
|
|
107
107
|
| 'relative-canonical'
|
|
108
108
|
| 'canonical-with-noindex'
|
|
109
109
|
| 'invalid-url'
|
|
110
|
+
| 'missing-preload-source'
|
|
110
111
|
| 'invalid-preload-as'
|
|
111
112
|
| 'missing-preload-as'
|
|
113
|
+
| 'responsive-image-missing-sizes'
|
|
112
114
|
| 'font-preload-missing-crossorigin'
|
|
113
115
|
| 'modulepreload-with-as'
|
|
114
116
|
| 'preconnect-missing-href'
|
|
@@ -159,6 +161,11 @@ export type PreloadImageOptions = ResourceHintOptions & {
|
|
|
159
161
|
fetchpriority?: FetchPriority
|
|
160
162
|
}
|
|
161
163
|
|
|
164
|
+
export type ResponsivePreloadImageOptions = PreloadImageOptions & {
|
|
165
|
+
href?: string | URL
|
|
166
|
+
imagesrcset: string
|
|
167
|
+
}
|
|
168
|
+
|
|
162
169
|
export type StylesheetOptions = Omit<LinkItem, 'rel' | 'href'>
|
|
163
170
|
|
|
164
171
|
export type ExternalScriptOptions = Omit<ScriptItem, 'src'>
|
|
@@ -507,9 +514,26 @@ export function preloadFont(
|
|
|
507
514
|
|
|
508
515
|
export function preloadImage(
|
|
509
516
|
href: string | URL,
|
|
517
|
+
options?: PreloadImageOptions
|
|
518
|
+
): LinkItem
|
|
519
|
+
export function preloadImage(options: ResponsivePreloadImageOptions): LinkItem
|
|
520
|
+
export function preloadImage(
|
|
521
|
+
hrefOrOptions: string | URL | ResponsivePreloadImageOptions,
|
|
510
522
|
options: PreloadImageOptions = {}
|
|
511
523
|
): LinkItem {
|
|
512
|
-
|
|
524
|
+
if (typeof hrefOrOptions === 'string' || hrefOrOptions instanceof URL) {
|
|
525
|
+
return {
|
|
526
|
+
...options,
|
|
527
|
+
rel: 'preload',
|
|
528
|
+
href: toUrlString(hrefOrOptions),
|
|
529
|
+
as: 'image'
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
const { href, ...rest } = hrefOrOptions
|
|
534
|
+
const link: LinkItem = { ...rest, rel: 'preload', as: 'image' }
|
|
535
|
+
if (href !== undefined) link.href = toUrlString(href)
|
|
536
|
+
return link
|
|
513
537
|
}
|
|
514
538
|
|
|
515
539
|
export function modulepreload(
|
|
@@ -844,6 +868,21 @@ function validatePreload(
|
|
|
844
868
|
path: string
|
|
845
869
|
): void {
|
|
846
870
|
const as = normaliseAttrValue(link.as).toLowerCase()
|
|
871
|
+
const href = normaliseAttrValue(link.href)
|
|
872
|
+
const imagesrcset = normaliseAttrValue(link.imagesrcset)
|
|
873
|
+
const hasResponsiveImageSource = as === 'image' && !!imagesrcset
|
|
874
|
+
|
|
875
|
+
if (!href && !hasResponsiveImageSource) {
|
|
876
|
+
issues.push({
|
|
877
|
+
code: 'missing-preload-source',
|
|
878
|
+
severity: 'warning',
|
|
879
|
+
tagName: 'link',
|
|
880
|
+
path,
|
|
881
|
+
message:
|
|
882
|
+
'Preload links need an href, except responsive image preloads with imagesrcset.'
|
|
883
|
+
})
|
|
884
|
+
}
|
|
885
|
+
|
|
847
886
|
if (!as) {
|
|
848
887
|
issues.push({
|
|
849
888
|
code: 'missing-preload-as',
|
|
@@ -865,6 +904,21 @@ function validatePreload(
|
|
|
865
904
|
})
|
|
866
905
|
}
|
|
867
906
|
|
|
907
|
+
if (
|
|
908
|
+
as === 'image' &&
|
|
909
|
+
hasWidthDescriptorImageSrcset(imagesrcset) &&
|
|
910
|
+
!hasNonEmptyAttr(link, 'imagesizes')
|
|
911
|
+
) {
|
|
912
|
+
issues.push({
|
|
913
|
+
code: 'responsive-image-missing-sizes',
|
|
914
|
+
severity: 'warning',
|
|
915
|
+
tagName: 'link',
|
|
916
|
+
path,
|
|
917
|
+
message:
|
|
918
|
+
'Responsive image preloads with width descriptors need imagesizes.'
|
|
919
|
+
})
|
|
920
|
+
}
|
|
921
|
+
|
|
868
922
|
if (as === 'font' && !hasAttr(link, 'crossorigin')) {
|
|
869
923
|
issues.push({
|
|
870
924
|
code: 'font-preload-missing-crossorigin',
|
|
@@ -1051,6 +1105,13 @@ function isSriRelevantLink(link: LinkItem): boolean {
|
|
|
1051
1105
|
return as === 'script' || as === 'style'
|
|
1052
1106
|
}
|
|
1053
1107
|
|
|
1108
|
+
function hasWidthDescriptorImageSrcset(imagesrcset: string): boolean {
|
|
1109
|
+
return imagesrcset.split(',').some((candidate) => {
|
|
1110
|
+
const descriptor = candidate.trim().split(/\s+/).at(-1) || ''
|
|
1111
|
+
return /^\d+w$/.test(descriptor)
|
|
1112
|
+
})
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1054
1115
|
function hasRobotsDirective(metaItems: MetaItem[], directive: string): boolean {
|
|
1055
1116
|
return metaItems.some(
|
|
1056
1117
|
(meta) =>
|
|
@@ -1404,7 +1465,9 @@ function getLinkDeduplicationKey(link: BaseItem): string | undefined {
|
|
|
1404
1465
|
if ((rels.includes('preconnect') || rels.includes('dns-prefetch')) && href)
|
|
1405
1466
|
return `${rels.join(' ')}:${href}`
|
|
1406
1467
|
|
|
1407
|
-
if (
|
|
1468
|
+
if (rels.includes('preload') || rels.includes('modulepreload')) {
|
|
1469
|
+
if (!href && !(rels.includes('preload') && as === 'image' && imagesrcset))
|
|
1470
|
+
return undefined
|
|
1408
1471
|
return [
|
|
1409
1472
|
rels.join(' '),
|
|
1410
1473
|
href,
|
|
@@ -1415,6 +1478,7 @@ function getLinkDeduplicationKey(link: BaseItem): string | undefined {
|
|
|
1415
1478
|
imagesrcset,
|
|
1416
1479
|
imagesizes
|
|
1417
1480
|
].join(':')
|
|
1481
|
+
}
|
|
1418
1482
|
}
|
|
1419
1483
|
|
|
1420
1484
|
function renderHeadTag(item: BaseItem | ContentItem): string {
|