Skip to content

Commit

Permalink
fix: sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
KrustyC committed Feb 6, 2024
1 parent 8b5dab3 commit e4fbf2f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@graphql-codegen/client-preset": "^4.1.0",
"@graphql-codegen/typescript": "^4.0.1",
"@types/node": "^20.11.13",
"@types/react": "^18.2.54",
"@types/react": "^18.2.55",
"@types/react-dom": "^18.2.18",
"autoprefixer": "^10.4.17",
"eslint": "^8.56.0",
Expand Down
32 changes: 32 additions & 0 deletions src/app/api/projects/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { NextResponse } from "next/server";

import { getContentfulClient } from "@/utils/contentful-client";

export const revalidate = 0;

export async function GET() {
try {
const client = getContentfulClient();

const result = await client.getEntries({
content_type: "project",
select: ["fields.slug", "sys.updatedAt"],
});

const projects = result?.items.map((project) => ({
lastModified: project.sys.updatedAt,
slug: project.fields.slug as unknown as string,
}));

return NextResponse.json({
success: true,
projects,
});
} catch (error) {
console.error(error);
return NextResponse.json({
success: false,
error: "Failed to fetch projects",
});
}
}
32 changes: 18 additions & 14 deletions src/app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
const BASE_PATHS = [""];

async function fetchProjectsRoutes() {
// const res = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL}/api/post`, {
// next: { revalidate: 30 },
// });
// const { posts } = await res.json();
try {
const res = await fetch(
`${process.env.NEXT_PUBLIC_BASE_URL}/api/projects`,
{
next: { revalidate: 30 },
}
);

// return posts.map((post: { href: string; lastModified: Date }) => ({
// url: `${process.env.NEXT_PUBLIC_BASE_URL}${post.href}`,
// lastModified: post.lastModified,
// }));
const { projects } = await res.json();

return [
{
url: "https://beatricecox.com/projects/babingtons-blend",
lastModified: "2020-10-30T15:00:00.000Z",
},
];
return projects.map(
({ slug, lastModified }: { slug: string; lastModified: Date }) => ({
url: `${process.env.NEXT_PUBLIC_BASE_URL}/${slug}`,
lastModified,
})
);
} catch (error) {
console.error(error);
return [];
}
}

export default async function sitemap() {
Expand Down

0 comments on commit e4fbf2f

Please sign in to comment.