#richText (slate) link depth too deep

3 messages · Page 1 of 1 (latest)

umbral quartz
#

When using a richText field and using the link option to select a document from a collection, the document is populated with too much depth. The only collection I allow to be linked is my routes collection. Loading a page with 10 route links can easily load the data of 60 - 100 documents linked to the route. Is there any way to limit this depth?

I've found the following discussion https://github.com/payloadcms/payload/discussions/3952 which hints at using a hook on the richText field but i've been unable to limit the depth this way as the document doesn't seem to be fully populated at execution of the afterRead hook yet. Limiting the depth on the routes collection is not an option as there are too many pages and other features linked to it unless there is a sure way to determine the request comes from the richText field. The richText field also seems to be the only thing that produces this "bug".

Does anyone happen to have an implemtation for this in place?

GitHub

Describe the Bug I encountered an issue related to data loading. On a single page, I have several richText fields, and this page contains numerous internal links (around 50) to other pages of my we...

dusky hemlockBOT
umbral quartz
#

So I've found a way to solve this problem using an afterRead hook for adding a specific value to the request, even though it is a bit specific to my setup and use case, it might help out someone else coming across this problem here as well.

On your richText field add an afterRead hook

{
   name: 'my_rich_text',
   type: 'richText',
   hooks: { afterRead: [richTextAfterRead] },
},

In the hook, you can add a value to the request

export const richTextAfterRead = ({ req }) => {
  req['FromRichText'] = true
}

With a little bit of background info for my specific use case, I am only allowing routes to be linked in the rich text to make it easier to references urls of the website that are defined there. My routes have a relation to pages which can contain any kind of (in this case unwanted) data. So on my routes collection I added an afterRead which will check for the FromRichText value added in the richTextAfterRead. If the value exists on the request, I remove all the unwanted data from the related document

const afterReadHook = ({
  doc,
  req,
}) => {
  if (req['FromRichText']) {
    doc.target.value = null
  }
  return doc
}

And that's it!