I'm having an issue with API routes and statically generated pages.
I have a set of pages that get generated statically via getStaticProps, this all works fine. I then want to hydrate those pages client side because only users with certain permissions are allowed to view the content.
The static page only contains the skeleton, the content is fetched in the useEffect by calling one of my API routes. The problem is that it's returning 404 and erroring all the time.
I already confirmed that the API route itself works fine when called outside of this page, is this something to do with the page being generated statically?
I replaced getStaticProps with getServerSideProps and i had no difference. I also removed the API call from the useEffect to be triggered by a button, just to check, and it still threw a 404.
I simplified it but that's roughly the code i have:
export const getStaticProps: GetStaticProps<{
post: CompactLesson,
}> = async ({ params }) => {
const post = await getCompactLesson(null, { ...params, user });
if (!post) {
return { notFound: true };
}
return {
props: {
post,
},
revalidate: 10, // In seconds
};
};
export default function Lesson({ post }: Props) {
useEffect(() => {
fetchLesson = async () => {
// this wil throw a 404
const response = await axios.get(`api/getLessonData`, {
params: JSON.stringify({id:post.id}),
});
}
fetchLesson()
}, []);
return {
<>my content</>
}
}
Thansk for the help