Skip to content

fix(drizzle): lazy load drizzle-kit to prevent production bundling#6

Open
deepshekhardas wants to merge 3 commits into
mainfrom
fix/lazy-load-drizzle-kit
Open

fix(drizzle): lazy load drizzle-kit to prevent production bundling#6
deepshekhardas wants to merge 3 commits into
mainfrom
fix/lazy-load-drizzle-kit

Conversation

@deepshekhardas

@deepshekhardas deepshekhardas commented May 19, 2026

Copy link
Copy Markdown
Owner

Description

drizzle-kit/api was being required at module load time, causing it to be included in production bundle for OpenNext Cloudflare and other edge runtimes.

This moves the require() inside the function so it's only loaded when actually needed (during migrations/schema push).

Also fixes the same issue in postgres adapter.

Testing

  • Build with OpenNext Cloudflare
  • Check that drizzle-kit/api is not in production bundle
  • Migration and schema push still work

Fixes: payloadcms#16470


Summary by cubic

Lazily load drizzle-kit/api so it’s not bundled in production builds for edge runtimes (e.g., OpenNext on Cloudflare); migrations and schema push still work. Fixes payloadcms/payload#16470.

  • Bug Fixes
    • MCP auth: accept both "payload-mcp-api-keys API-Key " and "Bearer " headers. Fixes payloadcms/payload#16572.
    • localizeStatus publish: detect localized _status updates so version timestamps are set and the latest version is retrieved. Fixes payloadcms/payload#16395.

Written for commit d8317b4. Summary will update on new commits. Review in cubic

Developer added 3 commits May 19, 2026 10:24
The MCP endpoint was accepting only Bearer token but Payload convention
uses 'payload-mcp-api-keys API-Key <key>' format. Now accepts both:
- 'payload-mcp-api-keys API-Key <key>' (Payload convention)
- 'Bearer <key>' (backwards compatible)

This makes the plugin consistent with other Payload API-key surfaces.

Fixes: payloadcms#16572
When using localizeStatus, the last version wasn't retrieved properly
when publishing directly. This is because the version_updatedAt and
version_createdAt weren't being set properly when publishing.

The fix adds a check for localized status (_status) in locales to
ensure main row data update happens even when only localized fields
have changed.

Fixes: payloadcms#16395
drizzle-kit/api was being required at module load time, causing it
to be included in production bundle for OpenNext Cloudflare and
other edge runtimes.

This moves the require() inside the function so it's only loaded
when actually needed (during migrations/schema push).

Also fixes the same issue in postgres adapter.

Fixes: payloadcms#16470

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 4 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="packages/drizzle/src/postgres/requireDrizzleKit.ts">

<violation number="1" location="packages/drizzle/src/postgres/requireDrizzleKit.ts:4">
P0: The first line of the function calls `require('module')` but `require` is not available in ESM. The `import { createRequire } from 'module'` that was removed is necessary — it imports a Node.js built-in (not drizzle-kit), so it won't affect production bundling. Only the `const require = createRequire(...)` and the `require('drizzle-kit/api')` call need to be inside the function for lazy loading.</violation>
</file>

<file name="packages/drizzle/src/sqlite/requireDrizzleKit.ts">

<violation number="1" location="packages/drizzle/src/sqlite/requireDrizzleKit.ts:4">
P0: `require` is not defined in ESM scope — `require('module')` will throw a ReferenceError before the next line can define `require`. Keep `import { createRequire } from 'module'` at the top level (it's a Node.js built-in, safe for tree-shaking) and only move `const require = createRequire(import.meta.url)` inside the function to lazy-load `drizzle-kit/api`.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

const require = createRequire(import.meta.url)

export const requireDrizzleKit: RequireDrizzleKit = () => {
const { createRequire } = require('module')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P0: The first line of the function calls require('module') but require is not available in ESM. The import { createRequire } from 'module' that was removed is necessary — it imports a Node.js built-in (not drizzle-kit), so it won't affect production bundling. Only the const require = createRequire(...) and the require('drizzle-kit/api') call need to be inside the function for lazy loading.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/drizzle/src/postgres/requireDrizzleKit.ts, line 4:

<comment>The first line of the function calls `require('module')` but `require` is not available in ESM. The `import { createRequire } from 'module'` that was removed is necessary — it imports a Node.js built-in (not drizzle-kit), so it won't affect production bundling. Only the `const require = createRequire(...)` and the `require('drizzle-kit/api')` call need to be inside the function for lazy loading.</comment>

<file context>
@@ -1,10 +1,8 @@
-const require = createRequire(import.meta.url)
-
 export const requireDrizzleKit: RequireDrizzleKit = () => {
+  const { createRequire } = require('module')
+  const require = createRequire(import.meta.url)
   const {
</file context>

const require = createRequire(import.meta.url)

export const requireDrizzleKit: RequireDrizzleKit = () => {
const { createRequire } = require('module')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P0: require is not defined in ESM scope — require('module') will throw a ReferenceError before the next line can define require. Keep import { createRequire } from 'module' at the top level (it's a Node.js built-in, safe for tree-shaking) and only move const require = createRequire(import.meta.url) inside the function to lazy-load drizzle-kit/api.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/drizzle/src/sqlite/requireDrizzleKit.ts, line 4:

<comment>`require` is not defined in ESM scope — `require('module')` will throw a ReferenceError before the next line can define `require`. Keep `import { createRequire } from 'module'` at the top level (it's a Node.js built-in, safe for tree-shaking) and only move `const require = createRequire(import.meta.url)` inside the function to lazy-load `drizzle-kit/api`.</comment>

<file context>
@@ -1,10 +1,8 @@
-const require = createRequire(import.meta.url)
-
 export const requireDrizzleKit: RequireDrizzleKit = () => {
+  const { createRequire } = require('module')
+  const require = createRequire(import.meta.url)
   const {
</file context>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

db-sqlite is pulling drizzle-kit/api into OpenNext Cloudflare output

1 participant