I did initialize getStaticPaths
with multiple locales
by getting all the posts slug
property with their specified locale
in a single fetch request.
The response was like this:
{
"1": {
"en": "Discover-the-Art-of-Culinary",
"fa": "کشف-هنر-آشپزی"
},
"2": {
"en": "Embrace-Wellness",
"fa": "آغوش-سلامت"
},
"3": {
"en": "Innovation-in-Technology",
"fa": "نوآوری-در-فناوری"
},
"4": {
"en": "Journey-Through-History",
"fa": "سفری-از-میان-تاریخ"
},
"5": {
"en": "The-Art-of-Photography",
"fa": "هنر-عکاسی"
}
}
Then I pushed each of them to the paths
variable inside the locales
loop like this:
export const getStaticPaths: GetStaticPaths = async ({ locales }) => {
const paths: {
params: {
slug: string;
};
locale: string;
}[] = [];
let postSlugs;
try {
const { data } = await axios.get("/posts");
postSlugs = data.data;
} catch (e) {
postSlugs = null;
}
locales?.forEach((locale: string) => {
Object.keys(postSlugs).forEach((eachSlug) => {
paths.push({
params: {
slug: postSlugs[eachSlug][locale],
},
locale,
});
});
});
return {
paths,
fallback: false,
};
};
Finally I got the slug
from getStaticProps
function and fetched the exact post like this:
export const getStaticProps: GetStaticProps = async ({ locale, params }) => {
const slug = params?.slug;
let blogPost;
try {
const { data } = await axios.get(`/${locale}/posts/${slug}`);
blogPost = data.data;
} catch (e) {
blogPost = null;
}
return {
props: { blogPost, slug },
revalidate: 1,
};
};
params
andlocale
I have found there is no difference if you include locale than if you do not. Would you care explaining why you added locale to paths? – Junko