feat(sitemap): update base URL and add build environment handling
Change the base URL from localhost to the production URL. Implement a check for the build environment to use mock data for generating the sitemap during the build process. This ensures that sitemap is correctly populated with project routes even when the API is not available.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
// portfolio/app/api/sitemap/route.tsx
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
interface Project {
|
||||
@@ -13,7 +14,7 @@ interface SitemapRoute {
|
||||
lastModified: string;
|
||||
}
|
||||
|
||||
const baseUrl = "http://localhost:3000";
|
||||
const baseUrl = "https://dki.one";
|
||||
|
||||
const generateSitemap = async (): Promise<SitemapRoute[]> => {
|
||||
try {
|
||||
@@ -30,6 +31,25 @@ const generateSitemap = async (): Promise<SitemapRoute[]> => {
|
||||
},
|
||||
];
|
||||
|
||||
// Check if running in build environment
|
||||
if (process.env.IS_BUILD) {
|
||||
// Use mock data during build
|
||||
const mockProjectsData: ProjectsData = {
|
||||
posts: [
|
||||
{ slug: "project-1" },
|
||||
{ slug: "project-2" },
|
||||
{ slug: "project-3" },
|
||||
],
|
||||
};
|
||||
const projectRoutes: SitemapRoute[] = mockProjectsData.posts.map(
|
||||
(project) => ({
|
||||
url: `${baseUrl}/projects/${project.slug}`,
|
||||
lastModified: new Date().toISOString(),
|
||||
}),
|
||||
);
|
||||
return [...staticRoutes, ...projectRoutes];
|
||||
}
|
||||
|
||||
// Fetch project data from your API
|
||||
console.log("Fetching project data from API...");
|
||||
const response = await fetch(`${baseUrl}/api/fetchAllProjects`);
|
||||
|
||||
@@ -15,9 +15,9 @@ interface SitemapRoute {
|
||||
}
|
||||
|
||||
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
||||
const baseUrl = "http://localhost:3000";
|
||||
const baseUrl = "https://dki.one";
|
||||
|
||||
// Fetch the sitemap data from the dynamic API route
|
||||
// Fetch the sitemap data from the dynamic API routes
|
||||
const response = await fetch(`${baseUrl}/api/sitemap`);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch sitemap: ${response.statusText}`);
|
||||
|
||||
Reference in New Issue
Block a user