Skip to content

Commit 7141b82

Browse files
committed
docs: add head examples
closes #1500, closes #1131
1 parent 9009065 commit 7141b82

File tree

1 file changed

+81
-16
lines changed

1 file changed

+81
-16
lines changed

docs/reference/site-config.md

Lines changed: 81 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,56 @@ export default {
155155

156156
Additional elements to render in the `<head>` tag in the page HTML. The user-added tags are rendered before the closing `head` tag, after VitePress tags.
157157

158+
```ts
159+
type HeadConfig =
160+
| [string, Record<string, string>]
161+
| [string, Record<string, string>, string]
162+
```
163+
164+
#### Example: Adding a favicon
165+
166+
```ts
167+
export default {
168+
head: [['link', { rel: 'icon', href: '/favicon.ico' }]]
169+
} // put favicon.ico in public directory, if base is set, use /base/favicon.ico
170+
171+
/* Would render:
172+
<link rel="icon" href="/favicon.ico">
173+
*/
174+
```
175+
176+
#### Example: Adding Google Fonts
177+
158178
```ts
159179
export default {
160180
head: [
181+
[
182+
'link',
183+
{ rel: 'preconnect', href: 'https://fonts.googleapis.com' }
184+
],
161185
[
162186
'link',
163187
{ rel: 'preconnect', href: 'https://fonts.gstatic.com', crossorigin: '' }
164-
// would render:
165-
//
166-
// <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
167188
],
189+
[
190+
'link',
191+
{ href: 'https://fonts.googleapis.com/css2?family=Roboto&display=swap', rel: 'stylesheet' }
192+
]
193+
]
194+
}
195+
196+
/* Would render:
197+
<link rel="preconnect" href="https://fonts.googleapis.com">
198+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
199+
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
200+
*/
201+
```
202+
203+
#### Example: Registering a service worker
168204

205+
```ts
206+
export default {
207+
head: [
169208
[
170209
'script',
171210
{ id: 'register-sw' },
@@ -174,24 +213,50 @@ export default {
174213
navigator.serviceWorker.register('/sw.js')
175214
}
176215
})()`
177-
// would render:
178-
//
179-
// <script id="register-sw">
180-
// ;(() => {
181-
// if ('serviceWorker' in navigator) {
182-
// navigator.serviceWorker.register('/sw.js')
183-
// }
184-
// })()
185-
// </script>
186216
]
187217
]
188218
}
219+
220+
/* Would render:
221+
<script id="register-sw">
222+
;(() => {
223+
if ('serviceWorker' in navigator) {
224+
navigator.serviceWorker.register('/sw.js')
225+
}
226+
})()
227+
</script>
228+
*/
189229
```
190230

231+
#### Example: Using Google Analytics
232+
191233
```ts
192-
type HeadConfig =
193-
| [string, Record<string, string>]
194-
| [string, Record<string, string>, string]
234+
export default {
235+
head: [
236+
[
237+
'script',
238+
{ async: '', src: 'https://www.googletagmanager.com/gtag/js?id=TAG_ID' }
239+
],
240+
[
241+
'script',
242+
{},
243+
`window.dataLayer = window.dataLayer || [];
244+
function gtag(){dataLayer.push(arguments);}
245+
gtag('js', new Date());
246+
gtag('config', 'TAG_ID');`
247+
]
248+
]
249+
}
250+
251+
/* Would render:
252+
<script async src="https://www.googletagmanager.com/gtag/js?id=TAG_ID"></script>
253+
<script>
254+
window.dataLayer = window.dataLayer || [];
255+
function gtag(){dataLayer.push(arguments);}
256+
gtag('js', new Date());
257+
gtag('config', 'TAG_ID');
258+
</script>
259+
*/
195260
```
196261

197262
### lang
@@ -239,7 +304,7 @@ Enabling this may require additional configuration on your hosting platform. For
239304

240305
- Type: `Record<string, string>`
241306

242-
Defines custom directory <-> URL mappings. See [Routing: Route Rewrites](../guide/routing#route-rewrites) for more details.
307+
Defines custom directory &lt;-&gt; URL mappings. See [Routing: Route Rewrites](../guide/routing#route-rewrites) for more details.
243308

244309
```ts
245310
export default {

0 commit comments

Comments
 (0)