Skip to content

Commit 85c19c8

Browse files
authored
perf(core)!: make TemplateParamsPlugin opt-in (#493)
* perf(core): make `TemplateParamsPlugin` opt-in * chore: fix tests
1 parent b1fd5ce commit 85c19c8

File tree

12 files changed

+335
-267
lines changed

12 files changed

+335
-267
lines changed

bench/ssr-harlanzw-com-e2e.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Head } from '@unhead/schema'
22
import { InferSeoMetaPlugin } from '@unhead/addons'
33
import { definePerson, defineWebPage, defineWebSite, useSchemaOrg } from '@unhead/schema-org/vue'
4+
import { TemplateParamsPlugin } from 'unhead/plugins'
45
import { describe, it } from 'vitest'
56
import { useHead, useSeoMeta, useServerHead } from '../packages/vue/src'
67
import { createHead as createServerHead, renderSSRHead } from '../packages/vue/src/server'
@@ -10,7 +11,11 @@ describe('ssr e2e bench', () => {
1011
// we're going to replicate the logic needed to render the tags for a harlanzw.com page
1112

1213
// 1. Add nuxt.config meta tags
13-
const head = createServerHead()
14+
const head = createServerHead({
15+
plugins: [
16+
TemplateParamsPlugin,
17+
],
18+
})
1419
// nuxt.config app.head
1520
head.push({
1621
title: 'Harlan Wilton',

docs/1.guides/0.titles.md

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Creating your own title like this is simple using `useHead()`{lang="ts"} with a
9090
```ts twoslash [input.vue]
9191
useHead({
9292
title: 'Home',
93-
titleTemplate: '%s %separator MySite'
93+
titleTemplate: '%s | MySite'
9494
})
9595
```
9696

@@ -106,61 +106,26 @@ useHead({
106106

107107
### Template Params
108108

109-
You may ask why we don't just use a function for the title template, and while this is supported, it can create issues with SSR and hydration.
109+
Template params are an opt-in plugin make your tags more dynamic. You get `%s` and `%separator` built-in, and can add your own:
110110

111-
Instead, it's recommended to use the params. Out-of-the-box, Unhead provides:
112-
113-
| Token | Description |
114-
|--------------|-------------------------------------------------|
115-
| `%s` | The current page title. |
116-
| `%separator` | The separator, defaults to a pipe character \|. |
117-
118-
The `%separator` token is smart - it only appears between content and automatically removes itself when the title is empty or when multiple separators would appear.
119-
120-
Define custom template params to maintain consistent formatting:
121-
122-
::code-group
123-
124-
```ts twoslash [input.vue]
111+
::code-block
112+
```ts [Input]
125113
useHead({
126114
title: 'Home',
127115
titleTemplate: '%s %separator %siteName',
128116
templateParams: {
129-
seperator: '',
130-
siteName: 'MySite'
117+
separator: '·',
118+
siteName: 'My Site'
131119
}
132120
})
133121
```
134122

135-
```html [output.html]
136-
<head>
137-
<title>Home — MySite</title>
138-
</head>
123+
```html [Output]
124+
<title>Home · My Site</title>
139125
```
140-
141126
::
142127

143-
I'd suggest choosing your own separator as the `'|'` is a bit ugly in my opinion, you can try:
144-
145-
```ts
146-
type Seperator = '-' | '' | '' | '·' | '❤️'
147-
```
148-
149-
You can use template params in other head tags too, such as meta descriptions and open graph tags.
150-
151-
```ts
152-
useHead({
153-
templateParams: {
154-
siteName: 'MyApp'
155-
},
156-
title: 'Home',
157-
meta: [
158-
{ name: 'description', content: 'Welcome to %siteName - where we make awesome happen' },
159-
{ property: 'og:title', content: 'Home | %siteName' },
160-
{ property: 'og:description', content: 'Check out %siteName today!' }
161-
]
162-
})
163-
```
128+
Check out the [Template Params](/usage/guides/template-params) guide to get started.
164129

165130
### Resetting the Title Template
166131

@@ -201,12 +166,12 @@ Remembering how to use the meta tags can be annoying, so we can use the [`useSeo
201166

202167
```ts [input.vue]
203168
useSeoMeta({
204-
titleTemplate: '%s %separator Health Tips',
169+
titleTemplate: '%s | Health Tips',
205170
title: 'Why you should eat more broccoli',
206171
// og title is not effected by titleTemplate, we can use template params here if we need
207-
ogTitle: 'Hey! Health Tips %separator 10 reasons to eat more broccoli.',
172+
ogTitle: 'Hey! Health Tips - 10 reasons to eat more broccoli.',
208173
// explicit twitter title is only needed when we want to display something just for X
209-
twitterTitle: 'Hey X! Health Tips %separator 10 reasons to eat more broccoli.',
174+
twitterTitle: 'Hey X! Health Tips - 10 reasons to eat more broccoli.',
210175
})
211176
```
212177

docs/1.guides/6.template-params.md

Lines changed: 0 additions & 119 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)