-
Notifications
You must be signed in to change notification settings - Fork 45
getPayloadClient crashing NextJS frontend #35
Comments
@Eric-Arz I am unable to replicate with 0.0.26. If you can recreate and provide more details that would be great! Here is the repo I used to test for reference: https://github.com/payloadcms/next-payload-demo |
im having the same problem, it gets stuck in an endless loop where it just restarts the same failing process. the payloadClient.ts file its referencing is unchanged. at first when the site launches after running "npm run dev" it works, but if you reload it collapses:
|
@olarsson can you recreate this with freshly installed next-payload-demo app by chance? |
Just following up here. It looks like the latest Next.js version is not compatible with how we are doing this, so we will need to revise and come up with a game plan to get this working again. For now, you can pin |
@JarrodMFlesch I had been exploring this issue, but FYI, even with The global scope just doesn't seem to be persisted, as I logged the calls with the code below and there were many instances of Note: the code below inits the Payload client with proper type inference. import { getPayload } from 'payload/dist/payload'
import config from './payload.config'
if (!process.env.MONGODB_URI) {
throw new Error('MONGODB_URI environment variable is missing')
}
if (!process.env.PAYLOAD_SECRET) {
throw new Error('PAYLOAD_SECRET environment variable is missing')
}
type PayloadPromise = ReturnType<typeof getPayload>
type Payload = Awaited<PayloadPromise>
/**
* Global is used here to maintain a cached connection across hot reloads
* in development. This prevents connections growing exponentially
* during API Route usage.
*
* Source: https://github.com/vercel/next.js/blob/canary/examples/with-mongodb-mongoose/lib/dbConnect.js
*/
let globalWithPayload = global as typeof globalThis & {
payload: {
client: Payload | undefined
promise?: PayloadPromise | undefined
}
}
let cached = globalWithPayload.payload
if (!cached) {
cached = globalWithPayload.payload = { client: undefined, promise: undefined }
}
export const getPayloadClient = async () => {
if (cached.client) {
console.info(`--- USING CACHED CLIENT ---`)
return cached.client
}
if (!cached.promise) {
console.info(`--- NEW PAYLOAD ---`)
cached.promise = getPayload({
// Make sure that your environment variables are filled out accordingly
mongoURL: process.env.MONGODB_URI as string,
secret: process.env.PAYLOAD_SECRET as string,
config
})
}
try {
console.info(`--- AWAITING NEW PAYLOAD INIT ---`)
cached.client = await cached.promise
} catch (err) {
console.error(`--- ERROR ---`, err)
cached.promise = undefined
throw err
}
return cached.client
};
export default getPayloadClient |
const getPayload = await cache(() => getPayloadClient())
const payload = await getPayload()
Never mind, that stopped working as soon as I tried accessing the admin pages. |
@Eric-Arz this has been resolved in the latest update. The fix was this single line, thanks to @jmikrut for saving the day. |
When using getPayloadClient as described in your readme the Next.js hot reload crashes the app:
The text was updated successfully, but these errors were encountered: