payload icon indicating copy to clipboard operation
payload copied to clipboard

Not clear docs on the right way to instantiate payload object with `getPayload()`

Open BorsBorsen opened this issue 7 months ago • 0 comments

Documentation Issue

At the page https://payloadcms.com/docs/local-api/overview we see module-level (file-level) initialization. This is not specifically stated, so might be just simplified. Then in next local-api topic pages we see function-level initialization. In blog posts, we see mostly function-level init. In payload repo "examples" directory it is mixed, but for function-level there are more cases.

Additional Details

AI struggles to answer this question definitely, claude suggests using module-level init, chatgpt suggests function-level.

Could you please clarify whether we should init it at module-level like so:

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

const payload = await getPayload({config})

export default async function Page() {
  ...

or function-level:

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

export default async function Page() {
  const payload = await getPayload({config})
  ...

its pros cons or does it not matter.

BorsBorsen avatar Jun 01 '25 04:06 BorsBorsen