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.12k stars 1.44k forks source link

Plugin package's hooks ends up in client. #7049

Open GeorgeHulpoi opened 2 months ago

GeorgeHulpoi commented 2 months ago

Link to reproduction

No response

Payload Version

3.0.0-beta.58

Node Version

v20.9.0

Next.js Version

15.0.0-rc.0

Describe the Bug

Using a Payload hook with import 'server-only'; seems to not produce any issue, but if the same logic is moved to a package, that piece of code seems to be in client (therefore throwing error).

Reproduction Steps

  1. Create a hook with import 'server-only'; in the local Payload project.
  2. Create a local package with a hook that is exported (for referrence, you can use my own plugin https://github.com/GeorgeHulpoi/payload-recaptcha-v3/tree/beta).
  3. Import that hook (or if using in my plugin there is a dev folder for e2e testing).
  4. You will see the error thrown by server-only.

Therefore, if you want to use my plugin as reference, during end-to-end tests there is no problem with that piece. The only problem arise when packing the local package and importing it into a external Payload project.

Adapters and Plugins

No response

GeorgeHulpoi commented 1 month ago

Hello @AlessioGr ! I did a reproduction over here: https://github.com/GeorgeHulpoi/payload-package-hook-client

  1. There is a hook with import 'server-only'; in package folder.
  2. Build the package with pnpm build and pnpm pack.
  3. After packing, go to payload (a PayloadCMS blank project) and install the package.
  4. In the payload project it is a collection called Tests which uses the payload-package.
  5. Adding the payload-package into serverComponentsExternalPackages (next.config.js) will generate the error.

Now, seems like skipping step 5 will not produce the issue. Thus, it may not be issue, but needs to be documented for plugin creators.