Repository navigation
Description
Describe the Bug
When blocks are defined in the top-level config.blocks array and referenced by slug, every dev hot reload of the config crashes the Postgres adapter's schema build with:
TypeError: Cannot read properties of undefined (reading 'name')
at createTableName (@payloadcms/drizzle/src/createTableName.ts:38:13)
at traverseFields (@payloadcms/drizzle/src/schema/traverseFields.ts:390:32) // blocks field
at buildTable (@payloadcms/drizzle/src/schema/build.ts:125:7)
at buildRawSchema (@payloadcms/drizzle/src/schema/buildRawSchema.ts:44:38)
In the Next.js dev server this shows up as ⨯ TypeError: Cannot read properties of undefined (reading 'name') at ignore-listed frames on any admin request after editing a file the config imports. Restarting next dev clears it until the next edit.
Cause. In packages/payload/src/config/sanitize.ts, the blocks loop resets config.blocks and then skips blocks already marked _sanitized, without adding them back:
config.blocks = []
if (incomingConfig.blocks?.length) {
for (const block of incomingConfig.blocks) {
const sanitizedBlock = block
if (sanitizedBlock._sanitized === true) {
continue // <- block is dropped from config.blocks
}
sanitizedBlock._sanitized = true
// ...
config.blocks.push(flattenBlock({ block }))
}
}On a hot reload, block modules that didn't change are reused, so they're the same objects that were marked on the first run. Every one is skipped and config.blocks is empty. reload() then builds payload.blocks from that empty array, and @payloadcms/drizzle resolves slug references with adapter.payload.blocks[slug], which is undefined, so createTableName reads .name from it.
Logging around reload() confirms it: Object.keys(payload.blocks) is ['carousel', 'caseStudySection', 'mediaSingle', 'mediaGrid', 'spacer', 'text', 'quote'] before the reload and [] after it.
This continue came in with #10905 (v3.24.0). We used slug-referenced blocks on 3.92.x without hitting it. It only started after upgrading to 4.0.0-beta.6 with Next.js 16.4.3. Our guess is that the v4 dev reload path (nextJsDevReloadStrategy on /_next/hmr) now reuses block modules across reloads where v3 didn't, but we haven't confirmed that part.
Suggested fix. Still add already-sanitized blocks to config.blocks:
if (sanitizedBlock._sanitized === true) {
config.blocks.push(flattenBlock({ block: sanitizedBlock }))
continue
}Link to the code that reproduces this issue
Minimal repro below. It runs without Next.js by calling reload() directly.
Reproduction Steps
-
Postgres project with a block defined at the top level and referenced by slug:
// blocks/Text.ts export const Text: Block = { slug: 'text', fields: [{ name: 'content', type: 'textarea' }] } // payload.config.ts export default buildConfig({ blocks: [Text], collections: [{ slug: 'pages', fields: [{ name: 'layout', type: 'blocks', blocks: ['text'] }] }], db: postgresAdapter({ pool: { connectionString: process.env.DATABASE_URI } }), // ... })
-
Run
next dev, open the admin, then save any file imported by the config (e.g. a collection). The next request fails with the error above.Or reproduce without Next.js:
import { getPayload, reload } from 'payload' const configURL = new URL('./payload.config.ts', import.meta.url).href const payload = await getPayload({ config: (await import(configURL)).default }) // Re-importing the config re-runs buildConfig, but the block modules stay cached, // which matches a hot reload where only the collection file changed const next = await (await import(configURL + '?v=2')).default console.log(next.blocks.length) // 0, expected 1 await reload(next, payload, true) // throws: Cannot read properties of undefined (reading 'name')
Which area(s) are affected?
area: core, db: postgres
Environment Info
payload: 4.0.0-beta.6
@payloadcms/db-postgres: 4.0.0-beta.6
@payloadcms/drizzle: 4.0.0-beta.6
next: 16.4.3
react: 19.3.1
node: 24.22.1
OS: Windows 11
Activity