Skip to content

Commit afa9479

Browse files
committed
feat: centralize site URL management with SITE_URL and absoluteUrl functions
1 parent f1c743e commit afa9479

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

app/layout.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Geist, Geist_Mono } from "next/font/google";
55
import "./globals.css";
66
import { ThemeProvider } from "@/components/theme-provider";
77
import { MixpanelInit } from "@/components/MixpanelInit";
8+
import { SITE_URL } from "@/lib/site-metadata";
89

910
const geistSans = Geist({
1011
variable: "--font-geist-sans",
@@ -16,7 +17,7 @@ const geistMono = Geist_Mono({
1617
subsets: ["latin"],
1718
});
1819

19-
const siteUrl = "https://devcontext.xyz";
20+
const siteUrl = SITE_URL;
2021
const siteTitle = "DevContext – AI Coding Guidelines & Repo Analyzer";
2122
const siteDescription =
2223
"DevContext helps developers generate AI config files like Copilot instructions, Cursor rules, and agents.md. Start fresh with a guided wizard or analyze your GitHub repo to auto-detect stack, frameworks, and best practices — consistent, fast, and IDE-ready.";

app/robots.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type { MetadataRoute } from "next";
22

3-
const baseUrl = "https://devcontext.com";
3+
import { SITE_URL, absoluteUrl } from "@/lib/site-metadata";
44

55
export default function robots(): MetadataRoute.Robots {
66
return {
77
rules: {
88
userAgent: "*",
99
allow: "/",
1010
},
11-
host: baseUrl,
12-
sitemap: `${baseUrl}/sitemap.xml`,
11+
host: SITE_URL,
12+
sitemap: absoluteUrl("/sitemap.xml"),
1313
};
1414
}

app/sitemap.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import type { MetadataRoute } from "next"
22

33
import stacksData from "@/data/stacks.json"
44
import type { DataQuestionSource } from "@/types/wizard"
5-
6-
const baseUrl = "https://devcontext.com"
5+
import { absoluteUrl } from "@/lib/site-metadata"
76

87
const STATIC_ENTRIES: Array<{
98
path: string
@@ -48,7 +47,7 @@ export default function sitemap(): MetadataRoute.Sitemap {
4847
])
4948

5049
return [...STATIC_ENTRIES, ...stackEntries].map(({ path, priority, changeFrequency }) => ({
51-
url: `${baseUrl}${path}`,
50+
url: absoluteUrl(path),
5251
priority,
5352
changeFrequency,
5453
lastModified,

app/stacks/[stack]/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { notFound } from "next/navigation"
44

55
import stacksData from "@/data/stacks.json"
66
import type { DataQuestionSource } from "@/types/wizard"
7+
import { absoluteUrl } from "@/lib/site-metadata"
78

89
const stackQuestionSet = stacksData as DataQuestionSource[]
910
const stackQuestion = stackQuestionSet[0]
@@ -120,7 +121,7 @@ export function generateMetadata({ params }: { params: { stack: string } }): Met
120121
title,
121122
description,
122123
alternates: {
123-
canonical: `https://devcontext.xyz/stacks/${slug}`,
124+
canonical: absoluteUrl(`/stacks/${slug}`),
124125
},
125126
}
126127
}

lib/site-metadata.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const DEFAULT_SITE_URL = "https://www.devcontext.xyz";
2+
3+
const normalizeSiteUrl = (input: string): string => {
4+
const trimmed = input.trim();
5+
if (!trimmed) {
6+
return DEFAULT_SITE_URL;
7+
}
8+
9+
const withoutTrailingSlash = trimmed.endsWith("/") ? trimmed.slice(0, -1) : trimmed;
10+
return withoutTrailingSlash || DEFAULT_SITE_URL;
11+
};
12+
13+
export const SITE_URL = normalizeSiteUrl(
14+
process.env.NEXT_PUBLIC_SITE_URL ?? process.env.SITE_URL ?? DEFAULT_SITE_URL,
15+
);
16+
17+
export const absoluteUrl = (path = ""): string => {
18+
if (!path || path === "/") {
19+
return SITE_URL;
20+
}
21+
22+
return `${SITE_URL}${path.startsWith("/") ? path : `/${path}`}`;
23+
};

0 commit comments

Comments
 (0)