fix: improve project data handling and sitemap generation
Refactor project data parsing to ensure type safety by casting the project data as a string. Enhance the sitemap generation by fetching data from a dynamic API route, allowing for more accurate and up-to-date sitemap entries. Remove unused project markdown files to clean up the project structure. These changes improve code reliability and maintainability.
This commit is contained in:
@@ -43,11 +43,16 @@ const ProjectDetails = () => {
|
||||
useEffect(() => {
|
||||
const projectData = searchParams.get("project");
|
||||
if (projectData) {
|
||||
setProject(JSON.parse(projectData));
|
||||
setProject(JSON.parse(projectData as string));
|
||||
// Remove the project data from the URL without reloading the page
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.delete("project");
|
||||
window.history.replaceState({}, "", url.toString());
|
||||
// @ts-expect-error window is defined
|
||||
if (typeof window !== "undefined") {
|
||||
// @ts-expect-error window is defined
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.delete("project");
|
||||
// @ts-expect-error window is defined
|
||||
window.history.replaceState({}, "", url.toString());
|
||||
}
|
||||
} else {
|
||||
// Fetch project data based on slug from URL
|
||||
const slug = params.slug as string;
|
||||
@@ -61,8 +66,8 @@ const ProjectDetails = () => {
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to fetch project data");
|
||||
}
|
||||
const projectData = await response.json();
|
||||
setProject(projectData.posts[0]); // Assuming the API returns an array of posts
|
||||
const projectData = (await response.json()) as { posts: Project[] };
|
||||
setProject(projectData.posts[0]);
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch project data:", error);
|
||||
}
|
||||
@@ -80,7 +85,9 @@ const ProjectDetails = () => {
|
||||
);
|
||||
}
|
||||
|
||||
const featureImageUrl = `/api/fetchImage?url=${encodeURIComponent(project.feature_image)}`;
|
||||
const featureImageUrl = project.feature_image
|
||||
? `/api/fetchImage?url=${encodeURIComponent(project.feature_image)}`
|
||||
: "";
|
||||
|
||||
return (
|
||||
<div
|
||||
@@ -88,23 +95,22 @@ const ProjectDetails = () => {
|
||||
>
|
||||
<Header />
|
||||
<div className="flex-grow">
|
||||
{/* Hero Section */}
|
||||
<div className="flex justify-center md:mt-28 px-4 md:px-0">
|
||||
<div className="relative w-full max-w-4xl h-0 pb-[56.25%] rounded-2xl overflow-hidden">
|
||||
{" "}
|
||||
{/* 16:9 Aspect Ratio */}
|
||||
<Image
|
||||
src={featureImageUrl}
|
||||
alt={project.title}
|
||||
fill
|
||||
style={{ objectFit: "cover" }}
|
||||
className="rounded-2xl"
|
||||
priority={true}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex justify-center mt-14 md:mt-28 px-4 md:px-0">
|
||||
{featureImageUrl && (
|
||||
<div className="relative w-full max-w-4xl h-0 pb-[56.25%] rounded-2xl overflow-hidden">
|
||||
<Image
|
||||
src={featureImageUrl}
|
||||
alt={project.title}
|
||||
fill
|
||||
style={{ objectFit: "cover" }}
|
||||
className="rounded-2xl"
|
||||
priority={true}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center justify-center mt-4">
|
||||
<h1 className="text-4xl md:text-6xl font-bold text-white">
|
||||
<h1 className="text-4xl md:text-6xl font-bold text-gray-600">
|
||||
{project.title}
|
||||
</h1>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user