Why the lang parameter isn't passed down to getStaticProps from getStaticPaths?

Viewed 55

getStaticPaths method:

export const getStaticPaths: GetStaticPaths = async () => {
  let ed = await fetch(`${baseURL}getEvents2`, {
    method: "post",
  });
  let events = await ed.json();
  const paths = ["hu", "en"].flatMap((lang) =>
    events.map((eventId) => ({
      params: { lang: lang, eventId: eventId },
    }))
  );
  return {
    paths,
    fallback: true,
  };
};

getStaticProps:

export const getStaticProps: GetStaticProps = async ({ ...context }) => {
  console.log(context);
}

console.log output:

enter image description here

I would like to see the lang somehow in the context. How could I achieve this?

1 Answers

To return a locale variant from getStaticPaths to be rendered in getStaticProps you should use the locale field in the path object.

export const getStaticPaths: GetStaticPaths = async () => {
    let ed = await fetch(`${baseURL}getEvents2`, {
        method: "post",
    });
    let events = await ed.json();
    const paths = ["hu", "en"].flatMap((lang) =>
        events.map((eventId) => ({
            params: { eventId: eventId },
            locale: lang // Pass `locale` here
        }))
    );
    return {
        paths,
        fallback: true,
    };
};
Related