Skip to content

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

Closed
@BorsBorsen

Description

@BorsBorsen

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions