Initial commit
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect } from 'react';
|
||||
|
||||
export interface UsePageMetaParams {
|
||||
title?: string;
|
||||
description?: string;
|
||||
}
|
||||
|
||||
export function usePageMeta({ title, description }: UsePageMetaParams) {
|
||||
useEffect(() => {
|
||||
if (!title) return;
|
||||
|
||||
const previousTitle = document.title;
|
||||
document.title = title;
|
||||
|
||||
return () => {
|
||||
document.title = previousTitle;
|
||||
};
|
||||
}, [title]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!description) return;
|
||||
|
||||
let tag = document.querySelector<HTMLMetaElement>('meta[name="description"]');
|
||||
|
||||
const created = !tag;
|
||||
if (!tag) {
|
||||
tag = document.createElement('meta');
|
||||
tag.name = 'description';
|
||||
document.head.appendChild(tag);
|
||||
}
|
||||
|
||||
const previousContent = tag.content;
|
||||
tag.content = description;
|
||||
|
||||
return () => {
|
||||
if (created) {
|
||||
tag.remove();
|
||||
} else {
|
||||
tag.content = previousContent;
|
||||
}
|
||||
};
|
||||
}, [description]);
|
||||
}
|
||||
Reference in New Issue
Block a user