Files
nobat724_front/lib/getCanonicalUrl.js
T
hamed 40df2db108 chore: update dependencies in package.json
- Bumped versions for several packages including:
  - @emotion/styled from ^11.14.0 to ^11.14.1
  - @maptiler/sdk from ^2.4.0 to ^2.5.1
  - @mui/icons-material from ^7.1.0 to ^7.3.6
  - @mui/material from ^7.1.0 to ^7.3.6
  - @mui/styled-engine-sc from ^6.0.0-alpha.18 to ^6.4.9
  - @mui/x-charts from ^7.16.0 to ^7.29.1
  - @mui/x-date-pickers from ^7.17.0 to ^7.29.4
  - @next/third-parties from ^15.1.0 to ^15.5.7
  - axios from ^1.7.7 to ^1.13.2
  - dayjs from ^1.11.13 to ^1.11.19
  - jspdf from ^3.0.1 to ^3.0.4
  - next from ^14.2.20 to ^15.5.7
  - next-themes from ^0.3.0 to ^0.4.6
  - npm from ^10.8.2 to ^10.9.4
  - react from ^18.0.0 to ^18.3.1
  - react-dom from ^18 to ^18.3.1
  - react-easy-crop from ^5.1.0 to ^5.5.6
  - react-toastify from ^10.0.6 to ^11.0.5
  - sharp from ^0.34.1 to ^0.34.5
  - styled-components from ^6.1.11 to ^6.1.19
  - stylis from ^4.3.4 to ^4.3.6
  - swiper from ^11.1.9 to ^11.2.10
  - postcss from ^8 to ^8.5.6
  - prettier from ^3.5.3 to ^3.7.4
  - tailwindcss from ^3.4.1 to ^3.4.18
2025-12-04 05:26:37 +03:30

80 lines
2.8 KiB
JavaScript

import { headers } from "next/headers";
import citiesData from "@/data/city.json";
const MAIN_DOMAIN = "https://nobat724.com";
/**
* Determines if the current domain should have a canonical tag
* and returns the canonical URL if needed
*/
export async function getCanonicalUrl() {
try {
const headersList = await headers();
const host = headersList.get("host") || "";
const pathname = headersList.get("x-pathname") || "/";
// Normalize host (remove www and convert to lowercase)
const normalizedHost = host.toLowerCase().replace(/^www\./, "");
// Check if current domain is the main domain
if (normalizedHost === "nobat724.com") {
return null; // No canonical needed for main domain
}
// Check if this is a subdomain/different domain from our city data
const subdomain = normalizedHost.split(".")[0];
const matchedCity = citiesData.find((city) => {
const cityDomain = city.domain.toLowerCase();
return cityDomain.includes(subdomain) || cityDomain === normalizedHost;
});
// If we found a matching city or any non-main domain, canonicalize to main domain
if (matchedCity || normalizedHost !== "nobat724.com") {
// Ensure pathname starts with /
const cleanPathname = pathname.startsWith("/") ? pathname : `/${pathname}`;
return `${MAIN_DOMAIN}${cleanPathname}`;
}
return null;
} catch (error) {
console.error("Error generating canonical URL:", error);
return null;
}
}
/**
* Client-side version for dynamic routes or client components
*/
export function getCanonicalUrlClient() {
if (typeof window === "undefined") return null;
try {
const host = window.location.host;
const pathname = window.location.pathname;
// Normalize host (remove www and convert to lowercase)
const normalizedHost = host.toLowerCase().replace(/^www\./, "");
// Check if current domain is the main domain
if (normalizedHost === "nobat724.com") {
return null; // No canonical needed for main domain
}
// Check if this is a subdomain/different domain from our city data
const subdomain = normalizedHost.split(".")[0];
const matchedCity = citiesData.find((city) => {
const cityDomain = city.domain.toLowerCase();
return cityDomain.includes(subdomain) || cityDomain === normalizedHost;
});
// If we found a matching city or any non-main domain, canonicalize to main domain
if (matchedCity || normalizedHost !== "nobat724.com") {
return `${MAIN_DOMAIN}${pathname}`;
}
return null;
} catch (error) {
console.error("Error generating client canonical URL:", error);
return null;
}
}