Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live Preview doesn't take into consideration hooks #6071

Open
vixalien opened this issue Apr 26, 2024 · 0 comments
Open

Live Preview doesn't take into consideration hooks #6071

vixalien opened this issue Apr 26, 2024 · 0 comments
Assignees
Labels
[possible-bug] Possible bug which hasn't been reproduced yet

Comments

@vixalien
Copy link

Link to reproduction

No response

Describe the Bug

I have a collection named Posts where I have a hook like the following:

// ... Posts collection
{
  hooks: {
    afterRead: [populateAuthors],
  },
}

// populateAuthors hook
import type { User } from "payload/auth";
import type { CollectionAfterReadHook } from "payload/types";

export const populateAuthors: CollectionAfterReadHook = async ({
  doc,
  req: { payload },
}) => {
  if (doc?.authors) {
    const ids = doc.authors
      .map((author: User) => (typeof author === "object" ? author?.id : author))
      .filter((id: string) => typeof id == "string");

    const authorDocs = await payload.find<"users">({
      collection: "users",
      where: { id: { in: ids } },
    });

    doc.populatedAuthors = authorDocs.docs.map((authorDoc) => ({
      id: authorDoc.id,
      name: authorDoc.name,
    }));
  }

  return doc;
};

however, when I turn on live preview, all the data will be there except the populatedAuthors key, because it is populated by a hook, and that hook works after read, and it seems useLivePreview from @payloadcms/live-preview-react doesn't execute the hooks (or at least cache their results).

To Reproduce

Create an afterRead hook.

The useLivePreview function doesn't execute the hooks, so the authors never get updated and is returned as null. It would be useful to cache the result of the previous run and merge it with the result from useLivePreview, or at least simulate the hook being re-run after each edit to avoid some network+database requests.

Payload Version

3.0.0-beta.18

Adapters and Plugins

@payloadcms/live-preview-react

@vixalien vixalien added the [possible-bug] Possible bug which hasn't been reproduced yet label Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[possible-bug] Possible bug which hasn't been reproduced yet
Projects
None yet
Development

No branches or pull requests

2 participants