Skip to content

Commit 3341c44

Browse files
author
agent
committed
fix canonical tags
1 parent c9b50d2 commit 3341c44

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<meta name="robots" content="index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1" />
1414
<meta name="googlebot" content="index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1" />
1515
<meta name="bingbot" content="index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1" />
16-
<link rel="canonical" href="https://snapforest.in" />
16+
<!-- Canonical tag is dynamically set per page via React -->
1717

1818
<!-- Open Graph / Facebook -->
1919
<meta property="og:type" content="website" />

src/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import RoastBattle from './pages/play/RoastBattle'
3939
import CustomCursor from './components/CustomCursor'
4040
import KonamiEasterEgg from './components/KonamiEasterEgg'
4141
import PWAInstallPrompt from './components/PWAInstallPrompt'
42+
import CanonicalTag from './components/CanonicalTag'
4243

4344
function PageWrapper({ children }: { children: React.ReactNode }) {
4445
const location = useLocation()
@@ -60,6 +61,7 @@ function PageWrapper({ children }: { children: React.ReactNode }) {
6061
export default function App() {
6162
return (
6263
<>
64+
<CanonicalTag />
6365
<CustomCursor />
6466
<KonamiEasterEgg />
6567
<Routes>

src/components/CanonicalTag.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { useEffect } from 'react'
2+
import { useLocation } from 'react-router'
3+
4+
export default function CanonicalTag() {
5+
const location = useLocation()
6+
7+
useEffect(() => {
8+
const canonicalUrl = `https://www.snapforest.in${location.pathname}`
9+
10+
let link = document.querySelector('link[rel="canonical"]') as HTMLLinkElement | null
11+
12+
if (!link) {
13+
link = document.createElement('link')
14+
link.rel = 'canonical'
15+
document.head.appendChild(link)
16+
}
17+
18+
link.href = canonicalUrl
19+
}, [location.pathname])
20+
21+
return null
22+
}

0 commit comments

Comments
 (0)