1

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

1 Answer 1

5

I think that problems is with request url, It should be made to /api/getLessonData instead of api/getLessonData

Sign up to request clarification or add additional context in comments.

2 Comments

I knew it was something small ahah Thanks for spotting my dumb mistake, i was trying to find a complicated answer to a very simple issue. That solved it.
Thank you so much! Spent 20m trying to figure this out. Damn these small bugs ^^"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.