payloadcms / payload

Payload is the open-source, fullstack Next.js framework, giving you instant backend superpowers. Get a full TypeScript backend and admin panel instantly. Use Payload as a headless CMS or for building powerful applications.
https://payloadcms.com
MIT License
23.42k stars 1.49k forks source link

In NextJS, passing options to getPayloadHMR doesn't affect the returned payload instance #7832

Open chrisvanmook opened 1 month ago

chrisvanmook commented 1 month ago

Link to reproduction

No response

Payload Version

3.0.0-beta.90

Node Version

v20.11.1

Next.js Version

15.0.0-canary.104

Describe the Bug

When passing options to getPayloadHMR, for example in a server action, it returns a payload instance without those options set. I think the issue here is because RootLayout is responsible initiating a payload instance, but we can only pass the config to the props (and thus RootLayout will use the default InitOptions to create a payload instance). So when calling getPayloadHMR, I think it returns a cached version of this instance.

I think there are some possible solutions here:

  1. Introduce an initOptions prop to RootLayout, like <RootLayout importMap={importMap} config={configPromise} initOptions={{ loggerOptions: { level: process.env.LOG_LEVEL ?? 'debug' } }}>. Then perhaps add a warning when trying to call getPayloadHMR with options if a cached version is found?
  2. Make sure that passing options to getPayloadHMR overrides the initOptions. Problem here is we still can't set the options initially...

Reproduction Steps

  1. Create a server component with an action like so:

    export const UploadFormServer = () => {
    const onUploadForm = async (formData: FormData) => {
    'use server';
    const payload = await getPayloadHMR({
      config: payloadConfig,
      loggerOptions: {
        level: "debug",
      },
    });
    
    console.log(payload.logger.level);
    ....
  2. Call the server action
  3. Note that it prints "info" instead of the expected "debug".

Adapters and Plugins

db-postgres

denolfe commented 1 month ago

Hey @chrisvanmook , the getPayloadHMR arg typing is a bit too wide here. It is not intended to change options other than the config and the importMap. We'll be trimming up this type to avoid any confusion here.

chrisvanmook commented 1 month ago

Hey @chrisvanmook , the getPayloadHMR arg typing is a bit too wide here. It is not intended to change options other than the config and the importMap. We'll be trimming up this type to avoid any confusion here.

Ah that makes sense! However, how should I be able to set these initial options with NextJS, since we only have the RootLayout component that initiates payload?

denolfe commented 4 weeks ago

Ideally, most of these values should be able to be set in the config itself. I'll be investigating this, since it doesn't look like the logger options can be set in the config currently.

chrisvanmook commented 1 week ago

Hi @denolfe, do you perhaps have any updates on this?