Hi there.
I am currently exploring the use of Prismic as a headless CMS for static multilingual websites based on NextJS. So far, I am really liking what I'm seeing, especially since the recent updates that introduced TypeScript support.
I am currently looking into how I could best implement translatable URLs into a website. I have been looking around, and I couldn't find a straight answer, so that's why I'm here.
Let's say I have a /products page in English (en-us) and a Dutch (nl-nl) /producten. Ideally, I would not manually create a mapping for this in the code.
Scenario
In an ideal situation:
- the person using Prismic could specify their desired slug in the CMS (the last part: example.com/some-english-category/some-english product. The category name would be specified in a NextJS pages directory or dynamically via
getStaticPaths
). - the slugs can be different per language (/my-article vs /mijn-artikel)
- the language switch on the static page can link to the translated version of the page (if it is available)
- this all works on a static site with a private Prismic repo
Given this scenario, I don't think I can make pages/[lang]/[uid].tsx
work as suggested on the route resolver page, but I hope I'm wrong here
My findings
I did some testing. When I get my page by UID as follows...
export const getStaticProps: GetStaticProps = async () => {
const page = await Client().getByUID("page", "home", {});
return {
props: { page },
};
};
... and then log this page
to the console, I notice the following options:
An alternative_languages
array:
[
{
"id": "abcdefg",
"uid": "home",
"type": "page",
"lang": "en-us"
}
]
A slugs
array, but that seems deprecated: What are Slugs?
When I query a single custom type (my footer) and select the homepage in a "Content Relationship" link and inspect its properties I don't see any custom fields from the static zone of my page besides the UID, so I can't just add a custom slug
field (which can then be translated per locale) and query it this way I think.
const footer = await Client().getSingle("footer");
// logging footer.data.menuLinks[0].link (content relationship link) gives me
{
"id": "abcdefg",
"type": "page",
"tags": [],
"slug": "this-is-my-hero", // from the first slice??? Kind of odd
"lang": "nl-nl",
"first_publication_date": "2021-11-30T17:01:13+0000",
"last_publication_date": "2021-12-24T15:16:40+0000",
"uid": "home",
"url": "/home",
"link_type": "Document",
"isBroken": false
}
A possible solution?
Something I thought of (but haven't tried) was:
- querying all Prismic documents in
getStaticProps
(because that would provide customslug
fields from the static zone thus allowing the CMS operator to specify their desired slug) - computing the full URLs the same way I would in
getStaticPaths
- and passing those translatable URLs for the same page as props to my page.
Do I have any other (better) options?
I'm still relatively new to using Prismic, so please tell me if I'm missing something
Happy holidays