Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions packages/payload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1000,17 +1000,6 @@ export const reload = async (
})
}

// Generate import map
if (skipImportMapGeneration !== true && config.admin?.importMap?.autoGenerate !== false) {
// This may run outside of the admin panel, e.g. in the user's frontend, where we don't have an import map file.
// We don't want to throw an error in this case, as it would break the user's frontend.
// => just skip it => ignoreResolveError: true
await generateImportMap(config, {
ignoreResolveError: true,
log: true,
})
}

if (payload.db?.init) {
await payload.db.init()
}
Expand All @@ -1025,6 +1014,25 @@ export const reload = async (
;(global as any)._payload_doNotCacheClientConfig = true // This will help refreshing the client config cache more reliably. If you remove this, please test HMR + client config refreshing (do new fields appear in the document?)
;(global as any)._payload_doNotCacheSchemaMap = true
;(global as any)._payload_doNotCacheClientSchemaMap = true

// Generate import map
if (skipImportMapGeneration !== true && config.admin?.importMap?.autoGenerate !== false) {
// We need to run import map generation in the background, using setTimeout + a floating promise.
// Otherwise, in Next.js 16 and turbopack, the console will be spammed with errors that look like this:
// Error: Could not find the module "[project]/node_modules/.pnpm/[path-to-importmap-entry]" in the React Client Manifest. This is probably a bug in the React Server Components bundler.
// This will happen when running HMR for multiple getPayload calls, e.g. getPayload from admin panel and frontend.
// This usually happens for live-preview enabled collections.
// Once the generateImportMap accesses the import map file using fs, these errors will be thrown.
setTimeout(() => {
void generateImportMap(config, {
// This may run outside of the admin panel, e.g. in the user's frontend, where we don't have an import map file.
// We don't want to throw an error in this case, as it would break the user's frontend.
// => just skip it => ignoreResolveError: true
ignoreResolveError: true,
log: true,
})
}, 0)
}
}

let _cached: Map<
Expand Down Expand Up @@ -1116,6 +1124,7 @@ export const getPayload = async (
await reload(config, cached.payload, false, options)

resolve()
cached.reload = false
}

if (cached.reload instanceof Promise) {
Expand Down Expand Up @@ -1156,6 +1165,14 @@ export const getPayload = async (
)

cached.ws.onmessage = (event) => {
if (cached.reload instanceof Promise) {
// If there is an in-progress reload in the same getPayload
// cache instance, do not set reload to true again, which would
// trigger another reload.
// Instead, wait for the in-progress reload to finish.
return
}

if (typeof event.data === 'string') {
const data = JSON.parse(event.data)

Expand Down
Loading