Repository navigation
Description
Describe the Bug
In development, a config change reported while Payload is already reloading the config is lost. Payload keeps the config read at the start of that reload until another file change is reported or the dev server restarts.
This happens whenever several files are saved within a short time: a new component saved as several files one after another, git checkout / git pull, a formatter or codemod rewriting files, an editor's "save all".
Cause. The dev reload callback in connectDevReload (packages/payload/src/index.ts) ignores a change while a reload is in progress:
cached.devReloadCleanup = strategy.connect(() => {
if (cached.reload instanceof Promise) {
return
}
cached.reload = true
})and getPayload clears the flag unconditionally once that reload finishes:
await reload(config, cached.payload, false, options)
resolve()
cached.reload = falseThe reload in progress already awaited options.config from the module graph that was current when it started, so it may hold the config from before the second change. Nothing marks that another reload is needed, so the next getPayload call returns the stale instance.
This is independent of the reload strategy: it affects the default Next.js HMR WebSocket strategy, the TanStack Start strategy and a custom devReloadStrategy alike.
Expected: the config contains both changes.
Actual: only the first change is applied; the second appears only after another file change or a restart.
Link to the code that reproduces this issue
Reproduction Steps
- Run a Payload app in dev (
next dev). - Change the config (for example add a field to a collection) and, while Payload is still reloading after that change, save a second change (for example in another file imported by the config). Easiest to hit with a reload that takes a moment (Postgres/SQLite schema push, import map generation) or with a
git checkoutbetween two branches with different configs. - Open the admin panel or call
getPayloadagain: only the first change is there.
The linked unit test reproduces it without a dev server: it captures the callback passed to devReloadStrategy.connect, holds a reload in progress with a pending config promise, reports a second change, and checks that the next getPayload call reloads again (it fails on main).
Which area(s) are affected?
area: core
Environment Info
Payload: 4.0.0-beta.4 (also main at aebbf75b9; same code in 3.x)
Next.js: 16.4.2
Node.js: 24.22.1
Activity