Skip to content

Commit 58a7bb1

Browse files
committed
docs: add detailed templated title and deduplication
1 parent 72df9bc commit 58a7bb1

1 file changed

Lines changed: 72 additions & 9 deletions

File tree

README.md

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,30 +169,93 @@ const head = new HeadBuilder({
169169

170170
### With Templated Title
171171

172-
Set a title template once and dynamically update titles on different pages:
172+
Set a title template with a default value, then pass page-specific titles as strings. The builder automatically applies the saved template to subsequent title updates:
173173

174174
```typescript
175175
import { HeadBuilder } from '@devsantara/head';
176176

177-
// Shared head
177+
// Create a builder and set title template with default
178+
// The template stays active for all future addTitle() calls
178179
const sharedHead = new HeadBuilder().addTitle({
179-
template: '%s | My Awesome site', // <- Set title template
180-
default: 'Home',
180+
template: '%s | My Awesome site', // Store template (%s is the placeholder)
181+
default: 'Home', // Initial title using template
181182
});
182-
183-
// Home page
184-
const homeHead = sharedHead;
185183
// Output: <title>Home | My Awesome site</title>
186184

187-
// Posts page
185+
// Update title for Posts page
186+
// Pass a string, builder applies the saved template automatically
188187
const postHead = sharedHead.addTitle('Posts').build();
189188
// Output: <title>Posts | My Awesome site</title>
190189

191-
// About page
190+
// Update title for About page
191+
// Template is still active from the first addTitle() call
192192
const aboutHead = sharedHead.addTitle('About Us').build();
193193
// Output: <title>About Us | My Awesome site</title>
194194
```
195195

196+
**How it works:**
197+
198+
1. First `addTitle()` with template object stores the template internally
199+
2. Subsequent `addTitle()` calls with strings automatically use the stored template
200+
3. The `%s` placeholder gets replaced with your page title
201+
4. Each title replaces the previous one (deduplication)
202+
203+
### With Element Deduplication
204+
205+
HeadBuilder automatically deduplicates elements—when you add an element matching an existing one, the new one replaces the old:
206+
207+
```typescript
208+
import { HeadBuilder } from '@devsantara/head';
209+
210+
const head = new HeadBuilder()
211+
.addTitle('My Site')
212+
.addTitle('Updated Title') // Replaces previous title
213+
214+
.addDescription('First description')
215+
.addDescription('Updated description') // Replaces previous
216+
217+
.addMeta({ name: 'keywords', content: 'web, development' })
218+
.addMeta({ name: 'author', content: 'John Doe' }) // Separate meta tags coexist
219+
220+
.addCanonical('https://devsantara.com/page1')
221+
.addCanonical('https://devsantara.com/page2') // Replaces previous canonical
222+
223+
.build();
224+
```
225+
226+
```typescript
227+
// Output (HeadElement[]):
228+
[
229+
{ type: 'title', attributes: { children: 'Updated Title' } },
230+
{
231+
type: 'meta',
232+
attributes: { name: 'description', content: 'Updated description' },
233+
},
234+
{
235+
type: 'meta',
236+
attributes: { name: 'keywords', content: 'web, development' },
237+
},
238+
{ type: 'meta', attributes: { name: 'author', content: 'John Doe' } },
239+
{
240+
type: 'link',
241+
attributes: { rel: 'canonical', href: 'https://devsantara.com/page2' },
242+
},
243+
];
244+
```
245+
246+
**How it works:**
247+
248+
- **Title**: Only one per document
249+
- **Meta by name**: One per unique `name` attribute (e.g., description, keywords)
250+
- **Meta by property**: One per unique `property` attribute (e.g., `og:title`, `og:description`)
251+
- **Charset**: Only one per document
252+
- **Canonical**: Only one per document
253+
- **Manifest**: Only one per document
254+
- **Alternate locales**: One per unique language code
255+
- **Other tags**: Deduplicated by exact attribute match
256+
257+
This ensures clean metadata without accidental duplicates.
258+
196259
### With React Adapter
197260

198261
```tsx

0 commit comments

Comments
 (0)