astro-helmet 1.0.0 → 1.0.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 +78 -78
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
Install `astro-helmet` using npm:
|
|
18
18
|
|
|
19
19
|
```bash
|
|
20
|
-
npm
|
|
20
|
+
npm i astro-helmet
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
## Usage
|
|
@@ -29,23 +29,23 @@ In your Layout component, import `astro-helmet` and pass an object of `headItems
|
|
|
29
29
|
import Helmet from 'astro-helmet'
|
|
30
30
|
|
|
31
31
|
const headItems = {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
32
|
+
title: 'My Site Title',
|
|
33
|
+
base: [{ href: 'https://example.com' }],
|
|
34
|
+
meta: [
|
|
35
|
+
{ name: 'description', content: 'My site description' },
|
|
36
|
+
{ property: 'og:type', content: 'website' }
|
|
37
|
+
],
|
|
38
|
+
link: [{ rel: 'stylesheet', href: 'styles.css' }],
|
|
39
|
+
style: [{ innerHTML: 'body { color: red; }' }],
|
|
40
|
+
script: [{ innerHTML: 'console.log("Hello, world!")' }],
|
|
41
|
+
noscript: [{ innerHTML: 'Please enable JavaScript' }]
|
|
42
42
|
}
|
|
43
43
|
---
|
|
44
44
|
|
|
45
45
|
<!doctype html>
|
|
46
46
|
<html lang="en">
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
<Helmet {headItems} />
|
|
48
|
+
<body> ... </body>
|
|
49
49
|
</html>
|
|
50
50
|
```
|
|
51
51
|
|
|
@@ -59,8 +59,8 @@ To control the order of head items, use the `priority` key.
|
|
|
59
59
|
|
|
60
60
|
```ts
|
|
61
61
|
const headItems: HeadItems = {
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
// priority 1 will move the script to just below the <title>
|
|
63
|
+
script: [{ src: '/scripts/importantScript.js', priority: 1 }]
|
|
64
64
|
}
|
|
65
65
|
```
|
|
66
66
|
|
|
@@ -74,15 +74,15 @@ import Helmet from 'astro-helmet'
|
|
|
74
74
|
import type { HeadItems } from 'astro-helmet'
|
|
75
75
|
|
|
76
76
|
interface Props {
|
|
77
|
-
|
|
77
|
+
headItems: HeadItems
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
const layoutHeadItems: HeadItems = {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
81
|
+
link: [
|
|
82
|
+
{ rel: 'icon', type: 'image/svg+xml', href: '/favicon.svg' },
|
|
83
|
+
{ rel: 'sitemap', href: '/sitemap-index.xml' }
|
|
84
|
+
],
|
|
85
|
+
meta: [{ property: 'og:type', content: 'website' }]
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
const { headItems: pageHeadItems } = Astro.props
|
|
@@ -90,8 +90,8 @@ const { headItems: pageHeadItems } = Astro.props
|
|
|
90
90
|
|
|
91
91
|
<!doctype html>
|
|
92
92
|
<html lang="en">
|
|
93
|
-
|
|
94
|
-
|
|
93
|
+
<Helmet headItems={[layoutHeadItems, pageHeadItems]} />
|
|
94
|
+
<body> ... </body>
|
|
95
95
|
</html>
|
|
96
96
|
```
|
|
97
97
|
|
|
@@ -102,14 +102,14 @@ Then in your page components (or elsewhere), you can define additional head item
|
|
|
102
102
|
import type { HeadItems } from 'astro-helmet'
|
|
103
103
|
|
|
104
104
|
const headItems: HeadItems = {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
105
|
+
title: 'My Site Title',
|
|
106
|
+
meta: [{ name: 'description', content: 'My site description' }],
|
|
107
|
+
link: [{ rel: 'canonical', href: 'https://example.com' }]
|
|
108
108
|
}
|
|
109
109
|
---
|
|
110
110
|
|
|
111
111
|
<Layout {headItems}>
|
|
112
|
-
|
|
112
|
+
<main>content</main>
|
|
113
113
|
</Layout>
|
|
114
114
|
```
|
|
115
115
|
|
|
@@ -128,11 +128,11 @@ The `Helmet` component takes two props:
|
|
|
128
128
|
|
|
129
129
|
```ts
|
|
130
130
|
interface Props {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
131
|
+
headItems: HeadItems | HeadItems[]
|
|
132
|
+
options?: {
|
|
133
|
+
omitHeadTags?: boolean
|
|
134
|
+
applyPriority?: (tag: Tag) => Required<Tag>
|
|
135
|
+
}
|
|
136
136
|
}
|
|
137
137
|
```
|
|
138
138
|
|
|
@@ -152,10 +152,10 @@ const options = { omitHeadTags: true }
|
|
|
152
152
|
|
|
153
153
|
<!doctype html>
|
|
154
154
|
<html lang="en">
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
155
|
+
<head>
|
|
156
|
+
<Helmet {headItems} {options} />
|
|
157
|
+
</head>
|
|
158
|
+
<body> ... </body>
|
|
159
159
|
</html>
|
|
160
160
|
```
|
|
161
161
|
|
|
@@ -188,46 +188,46 @@ This is the default implementation of `applyPriority()`:
|
|
|
188
188
|
|
|
189
189
|
```ts
|
|
190
190
|
function applyPriority(tag: Tag): Required<Tag> {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
191
|
+
if (typeof tag.priority === 'number') return tag as Required<Tag>
|
|
192
|
+
let priority: number
|
|
193
|
+
switch (tag.tagName) {
|
|
194
|
+
case 'title':
|
|
195
|
+
priority = 0
|
|
196
|
+
break
|
|
197
|
+
|
|
198
|
+
case 'base':
|
|
199
|
+
priority = -2
|
|
200
|
+
break
|
|
201
|
+
|
|
202
|
+
case 'meta':
|
|
203
|
+
if (tag.charset) priority = -4
|
|
204
|
+
else if (tag.name === 'viewport') priority = -3
|
|
205
|
+
else if (tag['http-equiv']) priority = -1
|
|
206
|
+
else priority = 100
|
|
207
|
+
break
|
|
208
|
+
|
|
209
|
+
case 'link':
|
|
210
|
+
if (tag.rel === 'preconnect') priority = 10
|
|
211
|
+
else if (tag.rel === 'preload') priority = 60
|
|
212
|
+
else if (tag.rel === 'prefetch') priority = 80
|
|
213
|
+
else if (tag.rel === 'stylesheet') priority = 50
|
|
214
|
+
else priority = 90
|
|
215
|
+
break
|
|
216
|
+
|
|
217
|
+
case 'style':
|
|
218
|
+
priority = tag.innerHTML.includes('@import') ? 30 : 51
|
|
219
|
+
break
|
|
220
|
+
|
|
221
|
+
case 'script':
|
|
222
|
+
if (tag.async) priority = 20
|
|
223
|
+
else if (tag.defer) priority = 70
|
|
224
|
+
else priority = 40
|
|
225
|
+
break
|
|
226
|
+
|
|
227
|
+
default:
|
|
228
|
+
priority = 110
|
|
229
|
+
}
|
|
230
|
+
return { ...tag, priority }
|
|
231
231
|
}
|
|
232
232
|
```
|
|
233
233
|
|
|
@@ -240,8 +240,8 @@ Default charset and viewport meta tags are included by default.
|
|
|
240
240
|
```ts
|
|
241
241
|
const DEFAULT_CHARSET = { charset: 'UTF-8' }
|
|
242
242
|
const DEFAULT_VIEWPORT = {
|
|
243
|
-
|
|
244
|
-
|
|
243
|
+
name: 'viewport',
|
|
244
|
+
content: 'width=device-width, initial-scale=1'
|
|
245
245
|
}
|
|
246
246
|
```
|
|
247
247
|
|