astro-helmet 1.2.1 → 1.4.0-beta.1
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/LICENSE +15 -0
- package/README.md +208 -58
- package/index.d.ts +52 -0
- package/index.ts +43 -1
- package/package.json +21 -16
- package/src/Helmet.astro +56 -17
- package/src/main.ts +1260 -54
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Ryan Voitiskis
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
CHANGED
|
@@ -9,8 +9,12 @@
|
|
|
9
9
|
- Control the order of head items.
|
|
10
10
|
- Include default charset and viewport meta tags.
|
|
11
11
|
- Order head items with default or specified priority.
|
|
12
|
-
- Meta
|
|
12
|
+
- Meta and resource-hint link deduplication.
|
|
13
13
|
- JSON-LD structured data helper with automatic `@context`, serialization, and escaping.
|
|
14
|
+
- Optional SEO helper for canonical, robots, Open Graph, Twitter, alternates, and JSON-LD.
|
|
15
|
+
- Resource hint helpers for preconnect, preload, modulepreload, fonts, images, stylesheets, and external scripts.
|
|
16
|
+
- Dev-time validation for common head, SEO, preload, CSP, SRI, and unsafe-content mistakes.
|
|
17
|
+
- Optional Astro CSP hash/resource registration.
|
|
14
18
|
- Flexible API for adding head items and tag attributes.
|
|
15
19
|
|
|
16
20
|
## Installation
|
|
@@ -21,6 +25,8 @@ Install `astro-helmet` using npm:
|
|
|
21
25
|
npm i astro-helmet
|
|
22
26
|
```
|
|
23
27
|
|
|
28
|
+
`astro-helmet` supports Astro 4, 5, 6, and 7.
|
|
29
|
+
|
|
24
30
|
## Usage
|
|
25
31
|
|
|
26
32
|
In your Layout component, import `astro-helmet` and pass an object of `headItems` to the `Helmet` component.
|
|
@@ -37,9 +43,9 @@ const headItems = {
|
|
|
37
43
|
{ property: 'og:type', content: 'website' }
|
|
38
44
|
],
|
|
39
45
|
link: [{ rel: 'stylesheet', href: 'styles.css' }],
|
|
40
|
-
style: [{
|
|
41
|
-
script: [{
|
|
42
|
-
noscript: [{
|
|
46
|
+
style: [{ textContent: 'body { color: red; }' }],
|
|
47
|
+
script: [{ textContent: 'console.log("Hello, world!")' }],
|
|
48
|
+
noscript: [{ textContent: 'Please enable JavaScript' }]
|
|
43
49
|
}
|
|
44
50
|
---
|
|
45
51
|
|
|
@@ -52,9 +58,11 @@ const headItems = {
|
|
|
52
58
|
|
|
53
59
|
Any attribute can be added to a head item. Simply provide the attribute as a key-value pair in the object.
|
|
54
60
|
|
|
55
|
-
`'innerHTML', 'priority', 'tagName'` are reserved keys and cannot be used as attributes.
|
|
61
|
+
`'innerHTML', 'textContent', 'priority', 'tagName', 'key'` are reserved keys and cannot be used as attributes.
|
|
62
|
+
|
|
63
|
+
To add content to a tag, prefer `textContent`. For `<script>` and `<style>`, `textContent` preserves the code text while escaping closing-tag sequences such as `</script>`. For tags like `<noscript>`, `textContent` is HTML-escaped.
|
|
56
64
|
|
|
57
|
-
|
|
65
|
+
Use `innerHTML` only when you intentionally need raw, trusted markup. Do not pass CMS or user-provided strings into `innerHTML` or inline event-handler attributes.
|
|
58
66
|
|
|
59
67
|
To control the order of head items, use the `priority` key.
|
|
60
68
|
|
|
@@ -67,6 +75,74 @@ const headItems: HeadItems = {
|
|
|
67
75
|
|
|
68
76
|
See `applyPriority()` in the [Options](#options) section for more information on controlling the order of head items.
|
|
69
77
|
|
|
78
|
+
### Resource Helpers
|
|
79
|
+
|
|
80
|
+
Resource helpers return ordinary `link` or `script` items. They are optional, but they make common performance-sensitive tags harder to mistype.
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import {
|
|
84
|
+
externalScript,
|
|
85
|
+
modulepreload,
|
|
86
|
+
preconnect,
|
|
87
|
+
preloadFont,
|
|
88
|
+
preloadImage,
|
|
89
|
+
stylesheet,
|
|
90
|
+
type HeadItems
|
|
91
|
+
} from 'astro-helmet'
|
|
92
|
+
|
|
93
|
+
const headItems: HeadItems = {
|
|
94
|
+
title: 'My Site Title',
|
|
95
|
+
link: [
|
|
96
|
+
preconnect('https://cdn.example.com'),
|
|
97
|
+
preloadFont('/fonts/Inter.woff2', { type: 'font/woff2' }),
|
|
98
|
+
preloadImage('/images/hero.jpg', { fetchpriority: 'high' }),
|
|
99
|
+
modulepreload('/scripts/entry.js'),
|
|
100
|
+
stylesheet('/styles/site.css')
|
|
101
|
+
],
|
|
102
|
+
script: [externalScript('/scripts/app.js', { defer: true })]
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### SEO Helper
|
|
107
|
+
|
|
108
|
+
Use `createSeoHead()` when you want a small typed layer for common SEO tags while still keeping the raw `HeadItems` escape hatch.
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
import { createSeoHead } from 'astro-helmet'
|
|
112
|
+
|
|
113
|
+
const headItems = createSeoHead({
|
|
114
|
+
title: 'My Article',
|
|
115
|
+
titleTemplate: '%s | My Site',
|
|
116
|
+
description: 'A short page description.',
|
|
117
|
+
site: Astro.site,
|
|
118
|
+
path: Astro.url.pathname,
|
|
119
|
+
openGraph: {
|
|
120
|
+
type: 'article',
|
|
121
|
+
siteName: 'My Site',
|
|
122
|
+
image: {
|
|
123
|
+
url: '/images/article.jpg',
|
|
124
|
+
alt: 'Article image',
|
|
125
|
+
width: 1200,
|
|
126
|
+
height: 630
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
twitter: {
|
|
130
|
+
site: '@mysite',
|
|
131
|
+
image: '/images/article-twitter.jpg'
|
|
132
|
+
},
|
|
133
|
+
jsonLd: {
|
|
134
|
+
'@type': 'Article',
|
|
135
|
+
headline: 'My Article'
|
|
136
|
+
}
|
|
137
|
+
})
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
`createSeoHead()` returns normal `HeadItems`, so layout and page data can still be merged:
|
|
141
|
+
|
|
142
|
+
```astro
|
|
143
|
+
<Helmet headItems={[layoutHeadItems, pageHeadItems]} />
|
|
144
|
+
```
|
|
145
|
+
|
|
70
146
|
You can also pass an array of `headItems` to the `Helmet` component:
|
|
71
147
|
|
|
72
148
|
```astro
|
|
@@ -140,7 +216,12 @@ const headItems: HeadItems = {
|
|
|
140
216
|
{
|
|
141
217
|
'@type': 'BreadcrumbList',
|
|
142
218
|
itemListElement: [
|
|
143
|
-
{
|
|
219
|
+
{
|
|
220
|
+
'@type': 'ListItem',
|
|
221
|
+
position: 1,
|
|
222
|
+
name: 'Home',
|
|
223
|
+
item: 'https://example.com/'
|
|
224
|
+
},
|
|
144
225
|
{ '@type': 'ListItem', position: 2, name: 'My Article' }
|
|
145
226
|
]
|
|
146
227
|
}
|
|
@@ -172,10 +253,33 @@ JSON-LD script tags are rendered with priority `105`, placing them after regular
|
|
|
172
253
|
When provided with an array of `headItems`, `astro-helmet` will merge the items together.
|
|
173
254
|
|
|
174
255
|
`headItems.meta` are deduplicated by `name`, `property`, `http-equiv` and `charset`.
|
|
175
|
-
Meta items later in the array replace earlier items. Meta tags without any of these keys (e.g. `itemprop`-only tags) are never deduplicated.
|
|
256
|
+
Meta items later in the array replace earlier items. `name` and `http-equiv` matching is case-insensitive. `media` is part of the deduplication key, so media-specific tags such as multiple `theme-color` metas are preserved. Meta tags without any of these keys (e.g. `itemprop`-only tags) are never deduplicated.
|
|
257
|
+
|
|
258
|
+
`headItems.link` deduplicates:
|
|
259
|
+
|
|
260
|
+
- `rel="canonical"` links, keeping the last one.
|
|
261
|
+
- `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.
|
|
263
|
+
|
|
264
|
+
Any `meta`, `link`, `style`, `script`, or `noscript` item may provide `key` to opt into explicit last-write-wins deduplication:
|
|
265
|
+
|
|
266
|
+
```ts
|
|
267
|
+
const headItems = [
|
|
268
|
+
{
|
|
269
|
+
title: 'Layout',
|
|
270
|
+
script: [{ key: 'analytics', src: '/analytics-v1.js', defer: true }]
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
title: 'Page',
|
|
274
|
+
script: [{ key: 'analytics', src: '/analytics-v2.js', defer: true }]
|
|
275
|
+
}
|
|
276
|
+
]
|
|
277
|
+
```
|
|
176
278
|
|
|
177
279
|
`title` and `base` items are also deduplicated, with the last item in the array taking precedence.
|
|
178
280
|
|
|
281
|
+
At least one merged `HeadItems` object must provide a non-empty `title`; otherwise `renderHead()` will throw.
|
|
282
|
+
|
|
179
283
|
### Props
|
|
180
284
|
|
|
181
285
|
The `Helmet` component takes two props:
|
|
@@ -183,15 +287,50 @@ The `Helmet` component takes two props:
|
|
|
183
287
|
```ts
|
|
184
288
|
interface Props {
|
|
185
289
|
headItems: HeadItems | HeadItems[]
|
|
186
|
-
options?:
|
|
187
|
-
omitHeadTags?: boolean
|
|
188
|
-
applyPriority?: (tag: Tag) => Required<Tag>
|
|
189
|
-
}
|
|
290
|
+
options?: HelmetOptions
|
|
190
291
|
}
|
|
191
292
|
```
|
|
192
293
|
|
|
193
294
|
### Options
|
|
194
295
|
|
|
296
|
+
#### `validate`
|
|
297
|
+
|
|
298
|
+
By default, `<Helmet />` runs `validateHeadItems()` and logs warnings in Astro dev mode. Production builds stay quiet unless you explicitly pass `options.validate`.
|
|
299
|
+
|
|
300
|
+
```astro
|
|
301
|
+
<Helmet
|
|
302
|
+
{headItems}
|
|
303
|
+
options={{
|
|
304
|
+
validate: {
|
|
305
|
+
requireSri: true,
|
|
306
|
+
baseUrl: Astro.site
|
|
307
|
+
}
|
|
308
|
+
}}
|
|
309
|
+
/>
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
Set `validate: false` to disable component warnings:
|
|
313
|
+
|
|
314
|
+
```astro
|
|
315
|
+
<Helmet {headItems} options={{ validate: false }} />
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
You can also call the validator yourself:
|
|
319
|
+
|
|
320
|
+
```ts
|
|
321
|
+
import { validateHeadItems } from 'astro-helmet'
|
|
322
|
+
|
|
323
|
+
const issues = validateHeadItems(headItems, {
|
|
324
|
+
baseUrl: 'https://example.com',
|
|
325
|
+
requireOpenGraphImage: true,
|
|
326
|
+
requireSri: true
|
|
327
|
+
})
|
|
328
|
+
```
|
|
329
|
+
|
|
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.
|
|
331
|
+
|
|
332
|
+
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
|
+
|
|
195
334
|
#### `omitHeadTags`
|
|
196
335
|
|
|
197
336
|
By default, `astro-helmet` will render the <head> opening and closing tags around the head items. If you want it to render only the head items, set `omitHeadTags` to `true`.
|
|
@@ -213,10 +352,14 @@ const options = { omitHeadTags: true }
|
|
|
213
352
|
</html>
|
|
214
353
|
```
|
|
215
354
|
|
|
355
|
+
When using `omitHeadTags`, keep `<Helmet />` near the top of your manual `<head>`. The generated charset should remain inside the first 1024 bytes of the document and `<base>` should appear before URL-bearing tags.
|
|
356
|
+
|
|
216
357
|
#### `applyPriority()`
|
|
217
358
|
|
|
218
359
|
The `applyPriority` option allows you to customize the priority of head items. It takes a function that accepts a `Tag` object and returns a `Tag` object with the priority applied.
|
|
219
360
|
|
|
361
|
+
The default ordering follows Harry Roberts' [Get Your Head Straight](https://speakerdeck.com/csswizardry/get-your-head-straight) head-order model: document metadata first, early connection hints, async work, blocking work, future-navigation hints, then SEO/social metadata. It is a safe default ordering, not an auto-optimizer. Use explicit priorities when you have measured that a page needs a different critical path.
|
|
362
|
+
|
|
220
363
|
By default, items are ordered as follows:
|
|
221
364
|
|
|
222
365
|
| priority | item |
|
|
@@ -227,68 +370,75 @@ By default, items are ordered as follows:
|
|
|
227
370
|
| \-1 | `<meta http-equiv="">` |
|
|
228
371
|
| 0 | `<title>` |
|
|
229
372
|
| 10 | `<link rel="preconnect" />` |
|
|
373
|
+
| 11 | `<link rel="dns-prefetch" />` |
|
|
230
374
|
| 20 | `<script src="" async></script>` |
|
|
231
|
-
| 30 | `<style>`
|
|
375
|
+
| 30 | `<style>` where innerHTML.includes('@import') |
|
|
232
376
|
| 40 | `<script>` |
|
|
233
377
|
| 50 | `<link rel="stylesheet" />` |
|
|
234
378
|
| 60 | `<link rel="preload" />` |
|
|
379
|
+
| 60 | `<link rel="modulepreload" />` |
|
|
235
380
|
| 70 | `<script src="" defer></script>` |
|
|
236
381
|
| 80 | `<link rel="prefetch" />` |
|
|
382
|
+
| 80 | `<link rel="prerender" />` |
|
|
237
383
|
| 90 | remaining `<link>` |
|
|
238
384
|
| 100 | remaining `<meta>` |
|
|
239
385
|
| 105 | `<script type="application/ld+json">` |
|
|
240
386
|
| 110 | anything else |
|
|
241
387
|
|
|
242
|
-
|
|
388
|
+
Resource hints are powerful but easy to overuse:
|
|
389
|
+
|
|
390
|
+
- Use `preconnect` sparingly for important third-party origins needed early. Add `crossorigin` only when the eventual request needs a CORS connection.
|
|
391
|
+
- Use `preload` only for critical resources needed by the current navigation. Provide a correct `as`, and include matching `type` and `crossorigin` when needed.
|
|
392
|
+
- Use `modulepreload` for module graphs, not classic scripts.
|
|
393
|
+
- Prefer `<script src async>` over inline async-loader snippets when possible, so the browser's preload scanner can see the URL.
|
|
394
|
+
- Avoid CSS `@import`; direct stylesheet links are easier for browsers to discover in parallel.
|
|
395
|
+
|
|
396
|
+
#### `csp`
|
|
397
|
+
|
|
398
|
+
Set `options.csp` to `true` to register inline script/style hashes and external script/style sources with Astro's CSP runtime API.
|
|
399
|
+
|
|
400
|
+
```astro
|
|
401
|
+
<Helmet {headItems} options={{ csp: true }} />
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
Leave this disabled unless your Astro project has `security.csp` configured. Astro warns when `Astro.csp` is accessed without CSP enabled, so `astro-helmet` keeps CSP registration opt-in.
|
|
405
|
+
|
|
406
|
+
Astro CSP is available in Astro 6 and newer. In Astro 4 and 5 this option is a no-op. Astro CSP is also a build/preview feature rather than a dev-server feature, and Astro documents limitations with client-side routing.
|
|
407
|
+
|
|
408
|
+
`astro-helmet` registers:
|
|
409
|
+
|
|
410
|
+
- Inline `<style>`, inline `<script>`, and JSON-LD hashes.
|
|
411
|
+
- External `<script src>`.
|
|
412
|
+
- Stylesheet links.
|
|
413
|
+
- `rel="preload"` links with `as="script"` or `as="style"`.
|
|
414
|
+
- `rel="modulepreload"` links as script resources.
|
|
415
|
+
|
|
416
|
+
It does not infer broader CSP directives from icons, images, fonts, analytics endpoints, or connection hints.
|
|
417
|
+
|
|
418
|
+
For CDN scripts and styles, combine browser-level SRI attributes with validation:
|
|
243
419
|
|
|
244
420
|
```ts
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
case 'link':
|
|
265
|
-
if (tag.rel === 'preconnect') priority = 10
|
|
266
|
-
else if (tag.rel === 'preload') priority = 60
|
|
267
|
-
else if (tag.rel === 'prefetch') priority = 80
|
|
268
|
-
else if (tag.rel === 'stylesheet') priority = 50
|
|
269
|
-
else priority = 90
|
|
270
|
-
break
|
|
271
|
-
|
|
272
|
-
case 'style':
|
|
273
|
-
priority = tag.innerHTML?.includes('@import') ? 30 : 51
|
|
274
|
-
break
|
|
275
|
-
|
|
276
|
-
case 'script':
|
|
277
|
-
if (tag.type === 'application/ld+json') priority = 105
|
|
278
|
-
else if (tag.async) priority = 20
|
|
279
|
-
else if (tag.defer) priority = 70
|
|
280
|
-
else priority = 40
|
|
281
|
-
break
|
|
282
|
-
|
|
283
|
-
default:
|
|
284
|
-
priority = 110
|
|
285
|
-
}
|
|
286
|
-
return { ...tag, priority }
|
|
421
|
+
const headItems = {
|
|
422
|
+
title: 'Secure assets',
|
|
423
|
+
link: [
|
|
424
|
+
{
|
|
425
|
+
rel: 'stylesheet',
|
|
426
|
+
href: 'https://cdn.example.com/site.css',
|
|
427
|
+
integrity: 'sha384-...',
|
|
428
|
+
crossorigin: 'anonymous'
|
|
429
|
+
}
|
|
430
|
+
],
|
|
431
|
+
script: [
|
|
432
|
+
{
|
|
433
|
+
src: 'https://cdn.example.com/app.js',
|
|
434
|
+
integrity: 'sha384-...',
|
|
435
|
+
crossorigin: 'anonymous',
|
|
436
|
+
defer: true
|
|
437
|
+
}
|
|
438
|
+
]
|
|
287
439
|
}
|
|
288
440
|
```
|
|
289
441
|
|
|
290
|
-
Provide a custom `applyPriority()` function to reorder head items as needed.
|
|
291
|
-
|
|
292
442
|
## Defaults
|
|
293
443
|
|
|
294
444
|
Default charset and viewport meta tags are included by default.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { HeadItems, HelmetOptions } from './src/main.js'
|
|
2
|
+
|
|
3
|
+
declare const Helmet: (props: {
|
|
4
|
+
headItems: HeadItems | HeadItems[]
|
|
5
|
+
options?: HelmetOptions
|
|
6
|
+
}) => unknown
|
|
7
|
+
|
|
8
|
+
export default Helmet
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
createSeoHead,
|
|
12
|
+
dnsPrefetch,
|
|
13
|
+
externalScript,
|
|
14
|
+
modulepreload,
|
|
15
|
+
preconnect,
|
|
16
|
+
preload,
|
|
17
|
+
preloadFont,
|
|
18
|
+
preloadImage,
|
|
19
|
+
stylesheet,
|
|
20
|
+
validateHeadItems
|
|
21
|
+
} from './src/main.js'
|
|
22
|
+
|
|
23
|
+
export type {
|
|
24
|
+
AlternateLink,
|
|
25
|
+
CrossOrigin,
|
|
26
|
+
ExternalScriptOptions,
|
|
27
|
+
FetchPriority,
|
|
28
|
+
HeadItems,
|
|
29
|
+
HeadValidationCode,
|
|
30
|
+
HeadValidationIssue,
|
|
31
|
+
HeadValidationOptions,
|
|
32
|
+
HeadValidationSeverity,
|
|
33
|
+
HelmetOptions,
|
|
34
|
+
JsonLdItem,
|
|
35
|
+
LinkItem,
|
|
36
|
+
MetaItem,
|
|
37
|
+
NoscriptItem,
|
|
38
|
+
OpenGraphOptions,
|
|
39
|
+
PreloadFontOptions,
|
|
40
|
+
PreloadImageOptions,
|
|
41
|
+
PreloadOptions,
|
|
42
|
+
ReferrerPolicy,
|
|
43
|
+
ResourceHintOptions,
|
|
44
|
+
RobotsOptions,
|
|
45
|
+
ScriptItem,
|
|
46
|
+
SeoHeadOptions,
|
|
47
|
+
SeoImage,
|
|
48
|
+
StyleItem,
|
|
49
|
+
StylesheetOptions,
|
|
50
|
+
Tag,
|
|
51
|
+
TwitterOptions
|
|
52
|
+
} from './src/main.js'
|
package/index.ts
CHANGED
|
@@ -2,4 +2,46 @@ import Helmet from './src/Helmet.astro'
|
|
|
2
2
|
|
|
3
3
|
export default Helmet
|
|
4
4
|
|
|
5
|
-
export
|
|
5
|
+
export {
|
|
6
|
+
createSeoHead,
|
|
7
|
+
dnsPrefetch,
|
|
8
|
+
externalScript,
|
|
9
|
+
modulepreload,
|
|
10
|
+
preconnect,
|
|
11
|
+
preload,
|
|
12
|
+
preloadFont,
|
|
13
|
+
preloadImage,
|
|
14
|
+
stylesheet,
|
|
15
|
+
validateHeadItems
|
|
16
|
+
} from './src/main'
|
|
17
|
+
|
|
18
|
+
export type {
|
|
19
|
+
AlternateLink,
|
|
20
|
+
CrossOrigin,
|
|
21
|
+
ExternalScriptOptions,
|
|
22
|
+
FetchPriority,
|
|
23
|
+
HeadItems,
|
|
24
|
+
HeadValidationCode,
|
|
25
|
+
HeadValidationIssue,
|
|
26
|
+
HeadValidationOptions,
|
|
27
|
+
HeadValidationSeverity,
|
|
28
|
+
HelmetOptions,
|
|
29
|
+
JsonLdItem,
|
|
30
|
+
LinkItem,
|
|
31
|
+
MetaItem,
|
|
32
|
+
NoscriptItem,
|
|
33
|
+
OpenGraphOptions,
|
|
34
|
+
PreloadFontOptions,
|
|
35
|
+
PreloadImageOptions,
|
|
36
|
+
PreloadOptions,
|
|
37
|
+
ReferrerPolicy,
|
|
38
|
+
ResourceHintOptions,
|
|
39
|
+
RobotsOptions,
|
|
40
|
+
ScriptItem,
|
|
41
|
+
SeoHeadOptions,
|
|
42
|
+
SeoImage,
|
|
43
|
+
StyleItem,
|
|
44
|
+
StylesheetOptions,
|
|
45
|
+
Tag,
|
|
46
|
+
TwitterOptions
|
|
47
|
+
} from './src/main'
|
package/package.json
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "astro-helmet",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0-beta.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A document head manager for astro.",
|
|
6
6
|
"author": "Ryan Voitiskis <ryanvoitiskis@pm.me> (https://ryanvoitiskis.com/)",
|
|
7
7
|
"license": "ISC",
|
|
8
8
|
"exports": {
|
|
9
|
-
".":
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"default": "./index.ts"
|
|
12
|
+
}
|
|
10
13
|
},
|
|
14
|
+
"types": "./index.d.ts",
|
|
11
15
|
"files": [
|
|
12
16
|
"src",
|
|
13
|
-
"index.ts"
|
|
17
|
+
"index.ts",
|
|
18
|
+
"index.d.ts"
|
|
14
19
|
],
|
|
15
20
|
"keywords": [
|
|
16
21
|
"astro",
|
|
@@ -30,24 +35,24 @@
|
|
|
30
35
|
"release": "semantic-release"
|
|
31
36
|
},
|
|
32
37
|
"devDependencies": {
|
|
33
|
-
"@astrojs/check": "^0.9.
|
|
34
|
-
"@eslint/js": "^
|
|
38
|
+
"@astrojs/check": "^0.9.9",
|
|
39
|
+
"@eslint/js": "^10.0.1",
|
|
35
40
|
"@semantic-release/changelog": "^6.0.3",
|
|
36
41
|
"@semantic-release/git": "^10.0.1",
|
|
37
|
-
"@semantic-release/github": "^
|
|
38
|
-
"@semantic-release/npm": "^
|
|
39
|
-
"@vitest/coverage-v8": "^4.1.
|
|
40
|
-
"astro": "^
|
|
41
|
-
"eslint": "^
|
|
42
|
-
"prettier": "^3.4
|
|
42
|
+
"@semantic-release/github": "^12.0.9",
|
|
43
|
+
"@semantic-release/npm": "^13.1.5",
|
|
44
|
+
"@vitest/coverage-v8": "^4.1.10",
|
|
45
|
+
"astro": "^7.0.6",
|
|
46
|
+
"eslint": "^10.6.0",
|
|
47
|
+
"prettier": "^3.9.4",
|
|
43
48
|
"prettier-plugin-astro": "^0.14.1",
|
|
44
|
-
"semantic-release": "^
|
|
45
|
-
"typescript": "^
|
|
46
|
-
"typescript-eslint": "^8.
|
|
47
|
-
"vitest": "^4.1.
|
|
49
|
+
"semantic-release": "^25.0.5",
|
|
50
|
+
"typescript": "^6.0.3",
|
|
51
|
+
"typescript-eslint": "^8.63.0",
|
|
52
|
+
"vitest": "^4.1.10"
|
|
48
53
|
},
|
|
49
54
|
"peerDependencies": {
|
|
50
|
-
"astro": "^4.0.0 || ^5.0.0 || ^6.0.0"
|
|
55
|
+
"astro": "^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0"
|
|
51
56
|
},
|
|
52
57
|
"repository": {
|
|
53
58
|
"type": "git",
|
package/src/Helmet.astro
CHANGED
|
@@ -1,17 +1,35 @@
|
|
|
1
1
|
---
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
type HeadItems,
|
|
4
|
+
type HelmetOptions,
|
|
5
|
+
renderHead,
|
|
6
|
+
getInlineContent,
|
|
7
|
+
getExternalResources,
|
|
8
|
+
validateHeadItems
|
|
9
|
+
} from './main'
|
|
3
10
|
|
|
4
11
|
interface Props {
|
|
5
12
|
headItems: HeadItems | HeadItems[]
|
|
6
|
-
options?:
|
|
7
|
-
omitHeadTags?: boolean
|
|
8
|
-
applyPriority?: (tag: Tag) => Required<Tag>
|
|
9
|
-
csp?: boolean
|
|
10
|
-
}
|
|
13
|
+
options?: HelmetOptions
|
|
11
14
|
}
|
|
12
15
|
|
|
13
|
-
const { headItems, options = {} } = Astro.props
|
|
14
|
-
const {
|
|
16
|
+
const { headItems, options = {} } = Astro.props as Props
|
|
17
|
+
const {
|
|
18
|
+
applyPriority,
|
|
19
|
+
omitHeadTags = false,
|
|
20
|
+
csp: cspEnabled = false,
|
|
21
|
+
validate = import.meta.env.DEV
|
|
22
|
+
} = options
|
|
23
|
+
if (validate) {
|
|
24
|
+
const validationOptions = typeof validate === 'object' ? validate : undefined
|
|
25
|
+
for (const issue of validateHeadItems(headItems, validationOptions)) {
|
|
26
|
+
console.warn(
|
|
27
|
+
`[astro-helmet] ${issue.code}: ${issue.message}${
|
|
28
|
+
issue.path ? ` (${issue.path})` : ''
|
|
29
|
+
}`
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
15
33
|
const head = renderHead(headItems, applyPriority)
|
|
16
34
|
|
|
17
35
|
// Register CSP hashes and resources when the consumer opts in via `options.csp`
|
|
@@ -20,14 +38,16 @@ const head = renderHead(headItems, applyPriority)
|
|
|
20
38
|
// silent for users who don't need CSP integration. Cast avoids typecheck
|
|
21
39
|
// failures for downstream consumers on Astro 4/5.
|
|
22
40
|
const csp = cspEnabled
|
|
23
|
-
? (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
41
|
+
? (
|
|
42
|
+
Astro as unknown as {
|
|
43
|
+
csp?: {
|
|
44
|
+
insertScriptHash(hash: string): void
|
|
45
|
+
insertStyleHash(hash: string): void
|
|
46
|
+
insertScriptResource(resource: string): void
|
|
47
|
+
insertStyleResource(resource: string): void
|
|
48
|
+
}
|
|
29
49
|
}
|
|
30
|
-
|
|
50
|
+
).csp
|
|
31
51
|
: undefined
|
|
32
52
|
if (csp) {
|
|
33
53
|
for (const { type, content } of getInlineContent(headItems)) {
|
|
@@ -41,8 +61,27 @@ if (csp) {
|
|
|
41
61
|
}
|
|
42
62
|
|
|
43
63
|
for (const { type, url } of getExternalResources(headItems)) {
|
|
44
|
-
|
|
45
|
-
|
|
64
|
+
const resource = getCspSource(url)
|
|
65
|
+
if (!resource) continue
|
|
66
|
+
if (type === 'script') csp.insertScriptResource(resource)
|
|
67
|
+
else csp.insertStyleResource(resource)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function getCspSource(url: string): string | undefined {
|
|
72
|
+
const trimmed = url.trim()
|
|
73
|
+
if (!trimmed) return undefined
|
|
74
|
+
if (trimmed.startsWith('//')) return new URL(`https:${trimmed}`).origin
|
|
75
|
+
if (trimmed.startsWith('/') || trimmed.startsWith('./')) return "'self'"
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
const parsed = new URL(trimmed)
|
|
79
|
+
if (parsed.protocol === 'http:' || parsed.protocol === 'https:')
|
|
80
|
+
return parsed.origin
|
|
81
|
+
if (parsed.protocol === 'data:' || parsed.protocol === 'blob:')
|
|
82
|
+
return parsed.protocol
|
|
83
|
+
} catch {
|
|
84
|
+
return "'self'"
|
|
46
85
|
}
|
|
47
86
|
}
|
|
48
87
|
---
|