Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

[Live preview] Relationship fields and images returning `null`

default discord avatar
tinouti12 months ago
3

I'm currently trying to migrate from

payload-visual-editor

to Payload 2.x's built-in live preview.


My relationship and upload fields all return

null

in the front-end data, regardless of the content of the field or the

depth

parameter passed in

useLivePreview

.



I'm running out of ideas to investigate this further... any suggestions? 🙏



The code I use to grab the data on the front-end:


const { data } = useLivePreview<CmsArticle|null>({
  initialData: null,
  serverURL: "http://localhost:3000",
  depth: 10, // removing/changing this does not change anything
});
console.log(data);
  • default discord avatar
    leerechan12 months ago

    I have a similar issue. I encountered it while using useLivePreview, and it's unclear whether it's a bug or my mistake. I display an image in one place, but after using the useLivePreview hook, the object representing the image is replaced with a string ID of the image. In the screenshot, you can see that the first render of the page have consistent image data, both from GraphQL and the hook's data. Then, the information about the image from the useLivePreview hook becomes a string with an ID. My assumption is that after the page preview renders, preview iframe receives messages from the form where, for some reason, the image is not stored as a complete object. Changing the depth parameter doesn't help too.



    console.log(initialPage.contact.contactInfos.image[0].image) const { data } = useLivePreview<Station>({ initialData: initialPage, serverURL: 'http://localhost:3000', }) console.log(data.contact.contactInfos.image[0].image)
    Bildschirmfoto_2023-11-16_um_23.28.27.png
  • default discord avatar
    tinouti12 months ago

    Hmm interesting, so you are at least getting the data.


    I wonder if, in my case, this is to do with me setting the

    initialData

    to null. 🤔



    In your case, it does indeed very much look like a depth issue, but if the

    depth

    parameter does not change anything... 🤷‍♂️



    Looks like it might be time to take a look under the hood and see how this thing is made! 😄



    Ah, looks like a known issue actually:

    https://github.com/payloadcms/payload/issues/4120


    Hmmm actually, I'm somehow just now noticing CORS errors, which might be related...


    🤔



    Oooh now that I've fixed the CORS problem, it seems that it might be an issue with the live preview system making API calls to get the relationship data, and not being able to due to authentication.



    Welp, adding my domain to the

    csrf

    setting in my payload config did the trick. 🙌



    So tldr for folks that might stumble upon the same issue of getting

    null

    in place of your relationship fields data, make sure your front-end is allowed to make requests to Payload using the

    cors

    and

    csrf

    options in the Payload config. ✅



    eg:


    {
      cors: [ 'http://localhost:3001' ],
      csrf: [ 'http://localhost:3001' ],
    }
    image.png
    image.png
    image.png
  • default discord avatar
    eustachi012 months ago

    I'm having similar issues.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.