Skip to content
Discussion options

You must be logged in to vote

Found a way to initialize Payload automatically on the Next.js startup - you need to create the src/instrumentation.ts file with content like this:

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

export async function register() {
  if (typeof window !== 'undefined') {
    return; // Skip on client side
  }

  // Only run on Node.js runtime (server side)
  if (process.env.NEXT_RUNTIME === 'nodejs' || !process.env.NEXT_RUNTIME) {
    await getPayload({ config })
  }
}

Not sure that this is the best way, but at least, works well for my case!

More details: https://nextjs.org/docs/app/guides/instrumentation

If you know a more elegant solution for this, please share!

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by MurzNN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant