From 44589b5bbd85fdfd3d3506a66628ff6b0808fa42 Mon Sep 17 00:00:00 2001 From: Denshooter Date: Wed, 12 Feb 2025 17:58:59 +0100 Subject: [PATCH] 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. --- Dockerfile | 8 +++++++- app/api/sitemap/route.tsx | 22 +++++++++++++++++++++- app/sitemap.tsx | 4 ++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9fd3e6e..f25e3a5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,9 +13,15 @@ RUN npm install # Copy the application code COPY . . +# Set IS_BUILD environment variable for build process +ENV IS_BUILD=true + # Build the Next.js application RUN npm run build +# Unset IS_BUILD environment variable for runtime +ENV IS_BUILD=false + # Set environmental variable for production mode ENV NODE_ENV=production @@ -23,4 +29,4 @@ ENV NODE_ENV=production EXPOSE 3000 # Run the app with the start script -CMD ["npm", "start"] +CMD ["npm", "start"]s diff --git a/app/api/sitemap/route.tsx b/app/api/sitemap/route.tsx index 852e56a..24a26ce 100644 --- a/app/api/sitemap/route.tsx +++ b/app/api/sitemap/route.tsx @@ -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 => { try { @@ -30,6 +31,25 @@ const generateSitemap = async (): Promise => { }, ]; + // 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`); diff --git a/app/sitemap.tsx b/app/sitemap.tsx index 8eaff30..9941f4a 100644 --- a/app/sitemap.tsx +++ b/app/sitemap.tsx @@ -15,9 +15,9 @@ interface SitemapRoute { } export default async function sitemap(): Promise { - 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}`);