````use client`;
import { GetStaticProps, GetStaticPaths } from "next";
import { useRouter } from "next/router";
import client from "../components/contentfulClient";
export const getStaticPaths: GetStaticPaths = async () => {
try {
const response = await client.getEntries({
content_type: "blogPost",
select: "fields.slug",
});
const paths = response.items.map((item) => ({
params: { slug: item.fields.slug },
}));
return {
paths,
fallback: "blocking", // Will wait for the page to be generated on request if not found
};
} catch (error) {
// Log and handle errors
console.error("Error fetching slugs from Contentful:", error);
return { paths: [], fallback: "blocking" };
}
};
export const getStaticProps: GetStaticProps = async ({ params }) => {
try {
const { slug } = params;
const response = await client.getEntries({
content_type: "blogPost",
"fields.slug": slug,
limit: 1,
});
if (!response.items.length) {
// Redirect to the blog list if the post is not found
return {
redirect: {
destination: "/blog",
permanent: false,
},
};
}
const post = response.items[0].fields;
return {
props: { post },
revalidate: 1, // ISR will revalidate the page every second
};
} catch (error) {
console.error(
"Error fetching data from Contentful for slug:",
params.slug,
error
);
return {
redirect: {
destination: "/blog",
permanent: false,
},
};
}
};
Located in src/app/blog/[slug]/page.tsx
is getting error:
./src\app\blog\[slug]\page.tsx
ReactServerComponentsError:
"getStaticProps" is not supported in app/. Read more: https://nextjs.org/docs/app/building-your-application/data-fetching
╭─[F:\Codingan\emporium-carg-next14\src\app\blog\[slug]\page.tsx:26:1]
26 │ }
27 │ };
28 │
29 │ export const getStaticProps: GetStaticProps = async ({ params }) => {
· ──────────────
30 │ try {
31 │ const { slug } = params;
31 │ const response = await client.getEntries({
╰────
File path:
./src\app\blog\[slug]\page.tsx