Cannot access document with old uid

Hi,

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?

Hello @kelly_ke

Thanks for reaching out to us.

There are no changes in the UID behavior from our side. You should still query that document from the old UID. Have you wholly deleted that document?

Thanks,
Priyanka

Hi @Priyanka ,

I didn't delete the document or archive it. It has been published and I just simply changed the doc's uid.
Here's the screenshot of edit history.

And if I try to create another document with the old uid, it will show the warning information about the uid has been taken.

Hi,

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.

Hello @kelly_ke

  • 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.

import {
	createClient,
	PrismicError,
	ParsingError,
} from "@prismicio/client"

const client = createClient("200629-sms-hoy")

// Valid call, yields document
console.log(await client.getByUID("page", "home"))

// Unknown UID, yields PrismicError
try {
	await client.getByUID("page", "unknown")
} catch (error) {
	console.error(error instanceof PrismicError)
	console.error(error)
}

// Unknown custom type, yields ParsingError
try {
	await client.getByUID("unknown", "home")
} catch (error) {
	console.error(error instanceof ParsingError)
	console.error(error.message)
}

Give this a try and let me know.

Thanks,
Priyanka

Hi @Priyanka ,

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:

catch (err){
  return {
    notFound: true
}

Here's the related discussion reference:

Hello @kelly_ke

Indeed, there are two solutions to handle errors in next.js getstaticprops:

  1. We can create custom 404 and 500 error pages.
  2. 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.

Thanks for pointing this out.
Priyanka

1 Like