Skip to content

Commit 1a092f9

Browse files
committed
fix: more seo stuff
1. Enhanced the `useSeo` hook: It now correctly manages canonical URLs and OG/Twitter tags
1 parent e3d8f9a commit 1a092f9

File tree

1 file changed

+111
-2
lines changed

1 file changed

+111
-2
lines changed

src/hooks/useSeo.js

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,117 @@ function useSeo({
151151
document.head.appendChild(newMeta);
152152
}
153153
}
154-
}, [
155-
location.pathname,
154+
155+
// Update URL meta tags (Canonical and OG URL)
156+
const currentUrl = window.location.origin + location.pathname;
157+
158+
// OG URL
159+
const metaOgUrl = document.querySelector('meta[property="og:url"]');
160+
if (metaOgUrl) {
161+
metaOgUrl.setAttribute('content', currentUrl);
162+
} else {
163+
const newMeta = document.createElement('meta');
164+
newMeta.setAttribute('property', 'og:url');
165+
newMeta.setAttribute('content', currentUrl);
166+
document.head.appendChild(newMeta);
167+
}
168+
169+
// Twitter URL
170+
const metaTwitterUrl = document.querySelector('meta[name="twitter:url"]');
171+
if (metaTwitterUrl) {
172+
metaTwitterUrl.setAttribute('content', currentUrl);
173+
} else {
174+
const newMeta = document.createElement('meta');
175+
newMeta.setAttribute('name', 'twitter:url');
176+
newMeta.setAttribute('content', currentUrl);
177+
document.head.appendChild(newMeta);
178+
}
179+
180+
// Canonical link
181+
let canonicalLink = document.querySelector('link[rel="canonical"]');
182+
if (canonicalLink) {
183+
canonicalLink.setAttribute('href', currentUrl);
184+
} else {
185+
canonicalLink = document.createElement('link');
186+
canonicalLink.setAttribute('rel', 'canonical');
187+
canonicalLink.setAttribute('href', currentUrl);
188+
document.head.appendChild(canonicalLink);
189+
}
190+
191+
return () => {
192+
// Cleanup: Restore defaults from index.html on unmount
193+
const defaults = {
194+
title: 'fezcodex',
195+
description: 'codex by fezcode...',
196+
ogTitle: 'Fezcodex - Personal Blog and Projects',
197+
ogDescription:
198+
'Discover logs, posts, projects, and stories from Fezcode.',
199+
ogImage: '/images/asset/ogtitle.png',
200+
ogUrl: 'https://fezcode.com/',
201+
twitterCard: 'summary_large_image',
202+
twitterTitle: 'Fezcodex - Personal Blog and Projects',
203+
twitterDescription:
204+
'Discover logs, posts, projects, and stories from Fezcode.',
205+
twitterImage: '/images/asset/ogtitle.png',
206+
twitterUrl: 'https://fezcode.com/',
207+
};
208+
209+
document.title = defaults.title;
210+
211+
const metaDescription = document.querySelector(
212+
'meta[name="description"]',
213+
);
214+
if (metaDescription)
215+
metaDescription.setAttribute('content', defaults.description);
216+
217+
const metaOgTitle = document.querySelector('meta[property="og:title"]');
218+
if (metaOgTitle) metaOgTitle.setAttribute('content', defaults.ogTitle);
219+
220+
const metaOgDescription = document.querySelector(
221+
'meta[property="og:description"]',
222+
);
223+
if (metaOgDescription)
224+
metaOgDescription.setAttribute('content', defaults.ogDescription);
225+
226+
const metaOgImage = document.querySelector('meta[property="og:image"]');
227+
if (metaOgImage) metaOgImage.setAttribute('content', defaults.ogImage);
228+
229+
const metaOgUrl = document.querySelector('meta[property="og:url"]');
230+
if (metaOgUrl) metaOgUrl.setAttribute('content', defaults.ogUrl);
231+
232+
const metaTwitterCard = document.querySelector(
233+
'meta[name="twitter:card"]',
234+
);
235+
if (metaTwitterCard)
236+
metaTwitterCard.setAttribute('content', defaults.twitterCard);
237+
238+
const metaTwitterTitle = document.querySelector(
239+
'meta[name="twitter:title"]',
240+
);
241+
if (metaTwitterTitle)
242+
metaTwitterTitle.setAttribute('content', defaults.twitterTitle);
243+
244+
const metaTwitterDescription = document.querySelector(
245+
'meta[name="twitter:description"]',
246+
);
247+
if (metaTwitterDescription)
248+
metaTwitterDescription.setAttribute('content', defaults.twitterDescription);
249+
250+
const metaTwitterImage = document.querySelector(
251+
'meta[name="twitter:image"]',
252+
);
253+
if (metaTwitterImage)
254+
metaTwitterImage.setAttribute('content', defaults.twitterImage);
255+
256+
const metaTwitterUrl = document.querySelector('meta[name="twitter:url"]');
257+
if (metaTwitterUrl)
258+
metaTwitterUrl.setAttribute('content', defaults.twitterUrl);
259+
260+
const canonicalLink = document.querySelector('link[rel="canonical"]');
261+
if (canonicalLink) canonicalLink.setAttribute('href', defaults.ogUrl);
262+
};
263+
}, [
264+
location.pathname,
156265
title,
157266
description,
158267
keywords,

0 commit comments

Comments
 (0)