fix: getPayload generate import map only when used in Payload Admin Panel (#9371)

After the change with removing `getPayloadHMR`, we do generate import
map even outside of Next.js, which leads to errors when using in a
project without it:

![image](https://github.com/user-attachments/assets/e8dc2af6-566b-443c-a6d8-8b02e719bd30)
This commit is contained in:
Sasha
2024-12-04 21:46:45 +02:00
committed by GitHub
parent fa39b37a44
commit d8f7034ab8

View File

@@ -807,7 +807,7 @@ export const getPayload = async (
// will reach `if (cached.reload instanceof Promise) {` which then waits for the first reload to finish. // will reach `if (cached.reload instanceof Promise) {` which then waits for the first reload to finish.
cached.reload = new Promise((res) => (resolve = res)) cached.reload = new Promise((res) => (resolve = res))
const config = await options.config const config = await options.config
await reload(config, cached.payload) await reload(config, cached.payload, !options.importMap)
resolve() resolve()
} }
@@ -815,7 +815,6 @@ export const getPayload = async (
if (cached.reload instanceof Promise) { if (cached.reload instanceof Promise) {
await cached.reload await cached.reload
} }
if (options?.importMap) { if (options?.importMap) {
cached.payload.importMap = options.importMap cached.payload.importMap = options.importMap
} }
@@ -839,6 +838,7 @@ export const getPayload = async (
) { ) {
try { try {
const port = process.env.PORT || '3000' const port = process.env.PORT || '3000'
cached.ws = new WebSocket( cached.ws = new WebSocket(
`ws://localhost:${port}${process.env.NEXT_BASE_PATH ?? ''}/_next/webpack-hmr`, `ws://localhost:${port}${process.env.NEXT_BASE_PATH ?? ''}/_next/webpack-hmr`,
) )