I am using
nextjswith
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?
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
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 errorDude you use get Payload in a client component?
Inside a collection hook. What other way to access local API in collection hook is there.
Colletion -> Hook -> LocalApi
Well you wouldn't use getPayload there, you would access the Payload instance directly through req:
req.payloadCollection -> Hook -req.payload -> LocalApi (use payload in argument)?
But this apporach works in another payload project. So onfusing. It's in production and working.
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
I understand its reduntant, but how come it works locally but is completely broken in production deployment?
Could be any number of reasons. It wasn’t intended to be used in this way so it’s expected there would be quirks.
I am a little bit confused. The newes documentation clearly states the way to import 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?
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.
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?
You mentioned above it’s for a collection hook. In this case you would access payload using req.payload
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
reqautomatically -> use
req.payloadAreas where you do not have access to a Payload request
reqobject, such as in the pages of your frontend app ->
getPayloadThe documentation states this too.
Star
Discord
online
Get dedicated engineering support directly from the Payload team.