I was able to query my post document both by its present uid and old uid. But now there's an issue that after changing a document's UID, I can't access it with its previous uid anymore.
According to this document, we should still can query any document by its old uid, right? Is there any update on UID field behavior?
I'm developing my project with Next.js and I found that querying document with old uid led to 404 page is related to fallback value in getStaticPaths.
Now I can access document with old uid again after changing fallback value from false to true.
But now I encountered another difficulty is I can't handle non-existent uid case.
Originally it will return 404 page if the uid is not in the paths returned by getStaticPaths with fallback set to false. I will get error message: "no documents were returned" in getStaticProps function with the query below.
const doc = await client.getByUID('post', params.uid, {
lang: language,
}) || {}; // Get no documents returned error if the entered uid is not existed
FYI: I was trying to implement a 301 redirect practice with custom hook according to the this discussion. Not sure if it's the best practice since the custom hook in the example is called conditionally.
Querying by UID using non-existing UID yields a PrismicError, this is intended;
Querying by UID using a non-existing document type yields a ParsingError, this is intended as the document type doesn't exist in the queried Prismic repository.
You have to handle error by applying try... catch block.
Thanks for the try and catch solution. But I found that we also need to return a notFound attribute in catch so the page can correctly redirect to 404 page if the page doesn't exist:
Another option, as you suggested, is to show the statusCode to tell Next to use the 404 page by returning notfound: true in getStaticProps. If we return notfound: true, the statusCode will always show the 404 page, and we know the status code will be 404.