Hello! I'm new to Prismic and am struggling to sunset a particular document. This document is linked from over a hundred other documents. What I'd like to be able to do is unpublish the document, set up a 301 redirect from the old URL to an active URL, and be done with it.
When I unpublish the document, my next build fails because the internal links are now broken. Is there a way to easily avoid this, perhaps by deleting the old document altogether?
The only way I know how to unpublish the old document without the build failing is to go one by one into each document and remove or update the internal link. This is incredibly time consuming.
What's the easiest way to get to my desired outcome of unpublishing the old/unwanted document?
If your goal is to unpublish the document without the build failing, I have a simple potential fix.
You can use a Link Resolver to handle broken links. A Link Resolver is just a function that accepts a Link object as a parameter and return a URL, so a simple one would be:
Or, instead of 404, you could direct the user to the homepage.
Then, make sure to include the Link Resolver everywhere in your app where Links appear. (all Rich Text fields and Link fields). All Rich Text and Link methods and components in the Prismic development packages accept a Link Resolver as a prop or parameter. See the docs for your framework for more info.
Another, more involved option, would be to use Import/Export to Export all of your documents, write a script to overwrite all of the broken links, and reimport everything. However, this would be pretty labor intensive, so I wouldn't necessarily recommend it.
That's helpful, Sam—thank you! I think your link resolver suggestion may suit our needs. While we don't want a bunch of dead links unaccounted for on our site, we also don't want a simple dead link to break our whole build, so addressing programmatically as you outlined may be a worthy tradeoff.
On this topic, though, we're struggling a bit to audit the links between documents. In the situation outlined in my original post, the only way I know which documents to update is by archiving the old document, letting the build fail, and going through the error logs to find all the dead links. If you have any suggestions on how to audit internal links, I'm all ears, and having something in the UI to clarify this would be a welcome improvement, from my perspective.
Wow, thank you so much, Sam! Between your link resolver suggestion and the utility you wrote, this puts us in a much better position! I'm going to share this with my engineering counterparts to see if this is something we can implement. Thank you again!