diff --git a/package-lock.json b/package-lock.json index 711f161..fa9ca8a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -30,7 +30,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", @@ -2941,9 +2941,9 @@ "devOptional": true }, "node_modules/@types/react": { - "version": "18.2.54", - "resolved": "https://registry.npmjs.org/@types/react/-/react-18.2.54.tgz", - "integrity": "sha512-039k+vrVJymDoe2y+HLk3O3oI3sa+C8KNjuDKofqrIJK26ramnqLNj9VJTaxAzFGMvpW/79HrrAJapHzpQ9fGQ==", + "version": "18.2.55", + "resolved": "https://registry.npmjs.org/@types/react/-/react-18.2.55.tgz", + "integrity": "sha512-Y2Tz5P4yz23brwm2d7jNon39qoAtMMmalOQv6+fEFt1mT+FcM3D841wDpoUvFXhaYenuROCy3FZYqdTjM7qVyA==", "devOptional": true, "dependencies": { "@types/prop-types": "*", diff --git a/package.json b/package.json index 0a65fc2..5029472 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/app/api/projects/route.ts b/src/app/api/projects/route.ts new file mode 100644 index 0000000..69198af --- /dev/null +++ b/src/app/api/projects/route.ts @@ -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", + }); + } +} diff --git a/src/app/sitemap.ts b/src/app/sitemap.ts index 7d2eaa7..5931ebf 100644 --- a/src/app/sitemap.ts +++ b/src/app/sitemap.ts @@ -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() {