Skip to content

Commit ca2acbe

Browse files
maitamdevmaitamgk
andcommitted
feat(seo): add meta tag management utilities
Co-authored-by: maitamgk <169973104+maitamgk@users.noreply.github.com>
1 parent 74c040e commit ca2acbe

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/utils/metaTags.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Meta tag management utilities
2+
export function setMetaTag(name: string, content: string): void {
3+
let el = document.querySelector('meta[name=' + JSON.stringify(name) + ']');
4+
if (!el) {
5+
el = document.createElement('meta');
6+
el.setAttribute('name', name);
7+
document.head.appendChild(el);
8+
}
9+
el.setAttribute('content', content);
10+
}
11+
12+
export function setOgTag(property: string, content: string): void {
13+
let el = document.querySelector('meta[property=' + JSON.stringify(property) + ']');
14+
if (!el) {
15+
el = document.createElement('meta');
16+
el.setAttribute('property', property);
17+
document.head.appendChild(el);
18+
}
19+
el.setAttribute('content', content);
20+
}
21+
22+
export function setPageMeta(title: string, description: string): void {
23+
document.title = title;
24+
setMetaTag('description', description);
25+
setOgTag('og:title', title);
26+
setOgTag('og:description', description);
27+
}

0 commit comments

Comments
 (0)