Skip to content

Commit

Permalink
adding root
Browse files Browse the repository at this point in the history
  • Loading branch information
abhisom2912 committed Oct 22, 2024
1 parent 91b302c commit 514d077
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/theme/Root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from 'react';
import useIsBrowser from '@docusaurus/useIsBrowser';
import {useLocation} from '@docusaurus/router';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';

export default function Root({children}) {
const isBrowser = useIsBrowser();
const location = useLocation();
const {siteConfig} = useDocusaurusContext();

React.useEffect(() => {
if (isBrowser) {
const updateCanonical = () => {
const canonicalElement = document.querySelector('link[rel="canonical"]');
const baseUrl = siteConfig.url;
const canonicalUrl = `${baseUrl}${location.pathname}`;

console.log('Updating canonical URL - v2');
console.log('Base URL:', baseUrl);
console.log('Current pathname:', location.pathname);
console.log('Full URL:', window.location.href);

if (canonicalElement) {
console.log('Existing canonical element found, updating href');
canonicalElement.setAttribute('href', canonicalUrl);
} else {
console.log('No existing canonical element, creating new one');
const link = document.createElement('link');
link.setAttribute('rel', 'canonical');
link.setAttribute('href', canonicalUrl);
document.head.appendChild(link);
}

console.log('Updated Canonical URL:', canonicalUrl);
};

// Update on initial load
updateCanonical();

// Update on location changes
const unlisten = () => {
updateCanonical(); // Call the update function whenever location changes
};

// Use a custom listener for location changes
const unsubscribe = location.pathname; // This will trigger re-render on path change

return () => {
// Cleanup if necessary
};
}
}, [isBrowser, location.pathname, siteConfig.url]); // Listen to pathname changes

return <>{children}</>;
}

0 comments on commit 514d077

Please sign in to comment.