How to redirect to a 404 when translated page doesnt exist in Prismic

What is the best practice to render a 404 page in Gatsby when a page exists in the default language, but a prismic translation hasn't been created yet?

We are using gatsby-node to delete the original and regenerate all pages in all locales, so some of them exist as url but the frontend shows us a blank page because the content is not there yet.

Is there a better approach to the following code? I would love to avoid adding conditional rendering in every page.

const result = !data.prismicAboutPage ? (
    <NotFound locale={locale}/>
  ) : (
    <Page data={data} locale={locale} />

Hello, thanks for reaching out!

How are you handling the dynamical generation of pages, are you using the uids of the docs? And, are you creating a route for all existing pages based on the number of languages on your repo?

You could instead, query the metadata of a document, and check if it has alternateLanguages, and then use the metadata of the alternate languages to create the correct ones.

1 Like

Thank you.
Not all of our pages are generated by gatsby-node, in fact, most of them are not, so we don't generate them via a graphql query in gatsby-node and there is no place where we can query the metadata of a document in order to check if they have alternateLangues. How would you proceed with those pages that are not generated via gatsby-node?

I see, could you tell me how are you generating all the other pages, are you using any of the Gatsby plugins that work with Prismic:

  • gatsby-source-prismic
  • gatsby-source-prismic-graphql

In the last one, pages are generated dynamically with the help of the plugin, and you access to the metadata in the queries, as you would in any other GraphQL query.

Could you show me an example of how you're generating the queries so I have a better idea?

We are using gatsby-source-prismic.

We two types of pages: the static ones and the dynamic ones.
Dynamic ones are generated in gatsby-node. Here is a snippet of how it looks like.

The static ones are under the /src/pages directory and they are doing their queries. They have different layouts, it makes no sense for us to generate them under gatsby-node.

Ok, well, the thing is that, if the document does not exist, the route shouldn’t exist either. We're working on a multi language project that will soon use the same plugin that you're using.

Right now you can still see the un official code of the project to check the stucture of how dynamic multi locale pages are generated. Here's the project code, check the gatsby-node. Right now, if you hit a url for a doc that doesn't exist, you'll get the default gatsby 404 page, (Read the official Gatsby docs: Adding a 404 page).

This issue has been closed due to inactivity. Flag to reopen.