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.

What is the proper way to get payload?

default discord avatar
awfaaraxlast year
14

I am using

nextjs

with

payload

, in order to use payload should i alway git is like:


import { getPayload } from 'payload'
import config from '@payload-config'

const payload = await getPayload({ config })


Does this also work same as above:


import payload from "payload"


Do Ineed to have global file where init, the export like

prisma

?

  • default discord avatar
    zed0547last year

    This will not work

    import payload from "payload"

    You should proceed with your first example



    It will give you a BasePayload instance that is not practically useful for you as it wasn't initialized at that point

  • default discord avatar
    kaspartrlast year

    This approach breaks my production:


    Error: An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error
  • default discord avatar
    zed0547last year

    Dude you use get Payload in a client component?

  • default discord avatar
    kaspartrlast year

    Inside a collection hook. What other way to access local API in collection hook is there.



    Colletion -> Hook -> LocalApi

  • default discord avatar
    zed0547last year

    Well you wouldn't use getPayload there, you would access the Payload instance directly through req:

    req.payload
  • default discord avatar
    kaspartrlast year

    Collection -> Hook -req.payload -> LocalApi (use payload in argument)?



    But this apporach works in another payload project. So onfusing. It's in production and working.

  • default discord avatar
    zed0547last year

    Every hook gets passed a req, you can access the local api through the Payload instance there yes.



    Not sure, but it might be due to import paths or resolution



    But it's redundant to call getPayload anywhere outside of the frontend tbh



    Everywhere else you can access it via req

  • default discord avatar
    kaspartrlast year

    I understand its reduntant, but how come it works locally but is completely broken in production deployment?

  • default discord avatar
    rilromlast year

    Could be any number of reasons. It wasn’t intended to be used in this way so it’s expected there would be quirks.

  • default discord avatar
    kaspartrlast year

    I am a little bit confused. The newes documentation clearly states the way to import payload.


    https://payloadcms.com/docs/local-api/overview#accessing-payload

    import { getPayload } from 'payload'
    import config from '@payload-config'
    
    const payload = await getPayload({ config })



    But this only works locally and not in production then?

  • default discord avatar
    rilromlast year

    The documentation says if you don’t have access to payload in either function arguments or the req object then you can do it that way.



    getPayload works both locally and in production when used properly.

  • default discord avatar
    kaspartrlast year

    I guess then the question becomes how to use it properly. I understand that I cannot use it as documented if it is used by the front-end although in the docs it explicilty sayd it doesn't matter?

  • default discord avatar
    rilromlast year

    You mentioned above it’s for a collection hook. In this case you would access payload using req.payload

  • default discord avatar
    zed0547last year
    I understand that I cannot use it as documented if it is used by the front-end

    This was misinterpreted from what I said I think.



    Usage of local api:


    Hooks, Access Controls, Custom Endpoints, Anywhere else you have access to

    req

    automatically -> use

    req.payload

    Areas where you do not have access to a Payload request

    req

    object, such as in the pages of your frontend app ->

    getPayload

    The documentation states this too.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.