Easiest way to archive/delete a document with many internal links pointing to it?

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?

1 Like

Hey @dan.duett,

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:

const linkResolver = (doc) => {
  if(doc.type === "page") return "/" + doc.uid
}

So the page document with the UID "hello-world" would return the link /hello-world.

(You're probably already using a Link Resolver in your app, but I just like to cover everything to make sure I don't miss anything :sweat_smile: )

Broken links have an isBroken property set to true. So you can do:

const linkResolver = ({ isBroken }) => {
  if(isBroken) return "/404"
}

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.

I hope that helps. Let me know what you decide!

Sam

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.

Hey @dan.duett,

I wrote a utility to check for dead links. (It seems like a useful thing to have.)

Just fill in your repo name at the top.

Let me know if that works for you, or if you need any help customizing it for your needs. (It's probably pretty buggy.)

Sam

1 Like

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!

Great! Let me know if you have more questions. In the meantime, I'll close this thread. But you can always reopen it to keep the conversation going.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.