When we go to preview a page from Prismic on localhost, it successfully redirects to the preview page no matter what locale we're previewing.
However, when we go to preview on our Netlify deployment, it doesn't seem to strip the preview url (/prismic-preview) from the final URL.
For example, here's what happens for the en-au version of our FAQ page:
When previewing on localhost, it navigates to http://localhost:3000/en-au/faq
When previewing to our Netlify deployment, it navigates to https://our-project.netlify.app/prismic-preview/en-au/faq
Because this route doesn't exist (with prismic-preview in it), we get a 404. The weird thing is this only happens with non-default locales! Our default locale doesn't have any locale code in the URL, and prismic successfully goes to https://our-project.netlify.app/faq to preview it.
Previewing other locales was definitely working until recently, so we're quite puzzled over how it stopped seemingly overnight.
// We populate dynamicRoutes using json files created by our prismic data pull, like this:
import studies from './data/cms/case_study.json'
let dynamicRoutes = []
studies['en-us'].forEach((study) => {
dynamicRoutes.push(`/case-studies/${study.uid}`)
})
generate: {
fallback: '404.html',
routes: dynamicRoutes
}
Please also see this other help ticket we created, which explains why we don't have preview: '/prismic-preview'
In our nuxt.config for Prismic (for good measure, I tried adding that and it didn't appear to make any difference).
OK, thanks a lot this really helps. My first guess is that the preview system doesn't have access to your i18n file in the link resolver in static mode, so the best way to do this is actually just get the lang from the document itself like we do in our nuxt multi-lang example here:
We already do use the doc.lang from Prismic. With this line: const i18nLocale = matchLanguage(prismicLocale) in our link-resolver, we simply match Prismic's locale code to the language prefixes that i18n uses.
See here:
/**
* Using the Prismic locale code, return the matching locale code that i18n uses
* @param {string} entryLang - Prismic languge code
* @returns {string} i18n language code
*/
const matchLanguage = (entryLang) => {
const languages = {
// prismicCode: i18nCode
'en-ca': 'en-us',
'en-gb': 'en-uk',
'en-au': 'en-au',
'en-eu': 'en-eu'
}
const i18nLocale = languages[entryLang]
if (i18nLocale) {
return i18nLocale
}
throw new Error(
"Data from Prismic is in a language the doesn't match our languages object"
)
}
I've been trying to debug this all day... I've tried setting up previews from scratch on the project using the docs, downgrading the prismic & nuxt-i18n packages, gradually stripping our project of functionality to see where the issue is, etc.
I managed to fix it!
I realized that the URL returned by link-resolver for non-default locales didn't have a leading slash, whereas the URL for en-us DID have a leading slash (and that one was working). This, combined with some other fixes specific to additional functionality in our codebase, did it!
I wonder if this brittle-ness related to the leading slash is new? In any case, I'm glad to have been able to find a fix.
OK, I understand better why your i18n file is necessary in your case.
I'm really glad you figured this out
I haven't seen this leading slash issue before in the link resolver, although I have seen it in the route resolver (There was a fix pushed for that). So I'll create an issue in our tracker for the team to investigate this issue once they get time.
Is there any chance you can share your fix for this? It would really help future users who encounter this problem.