fix(runtime): use dynamic import for #content/adapter to prevent prerender failure - #3830
Conversation
|
Someone is attempting to deploy a commit to the Nuxt Team on Vercel. A member of the Team first needs to authorize it. |
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 8 included reviews per hour; 4 remain after this review. 📝 WalkthroughWalkthroughThe runtime now loads the production database adapter through a cached dynamic import. Estimated code review effort: 3 (Moderate) | ~20 minutes Merge Risk: ⚪ Minimal · up to The PR changes adapter loading to avoid resolving Bun-specific modules during Node.js prerendering while preserving the production adapter path; no actionable merge-blocking risk remains beyond normal checks and review. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/runtime/internal/database.server.ts`:
- Around line 24-30: Update the connector initialization flow around the
module-level db guard and getAdapter so concurrent first calls share a cached
initialization promise, ensuring the adapter factory runs only once and all
callers receive the same connector. Preserve the existing dev/localAdapter and
production adapter selection behavior, and add a Promise.all regression test
covering concurrent initial calls.
In `@test/mock/content-adapter.ts`:
- Line 3: Rename the unused prepare callback parameter from sql to _sql in
test/mock/content-adapter.ts:3-3, test/mock/content-local-adapter.ts:3-3, and
each affected callback in test/unit/database.server.prerender.test.ts:48-48,
57-57, and 87-87, while preserving the mock interface and callback behavior.
In `@test/unit/database.server.prerender.test.ts`:
- Line 75: Replace the `config as any` casts in
`test/unit/database.server.prerender.test.ts` at lines 75, 109, and 113 with one
shared fixture typed as `RuntimeConfig['content']`, and pass that fixture
directly to `loadDatabaseAdapter` at each site.
- Around line 31-42: The static import assertion using staticImportPattern must
reject every top-level `#content/adapter` import form, including named, namespace,
side-effect, and combined imports, while continuing to allow dynamic
import('`#content/adapter`'). Replace the regex-only check with an import parser
or parser-backed assertion that distinguishes static imports from dynamic
imports.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 21366656-2258-4171-b08b-41890e02e463
📒 Files selected for processing (7)
src/runtime/api/query.post.tssrc/runtime/internal/database.server.tstest/mock/content-adapter.tstest/mock/content-local-adapter.tstest/mock/content-manifest.tstest/unit/database.server.prerender.test.tsvitest.config.ts
commit: |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
40c7c23 to
944cb8e
Compare
…erender failure When `sqliteConnector: 'bun'` is configured and the build runs on Node.js, the prerender stage fails because Node.js cannot resolve the `bun:` protocol. Root cause: `database.server.ts` used a static top-level import for `#content/adapter`. Node.js ESM loader resolves all static imports at module load time, regardless of whether the binding is called at runtime. During prerender only `localAdapter` is used, but the static import of `adapter` still forces resolution of `bun:sqlite`. Fix: Replace the static import with a lazy dynamic `import()` that is only resolved in the production code path (non-prerender, non-dev). This makes `loadDatabaseAdapter` async, which is a minimal API change since all callers already operate in async contexts. Closes nuxt#3829 Co-authored-by: Cursor <cursoragent@cursor.com>
- Prefix unused parameters with `_` to satisfy @typescript-eslint/no-unused-vars - Replace `as any` casts with a shared typed config fixture - Remove unused variable assignment Co-authored-by: Cursor <cursoragent@cursor.com>
944cb8e to
e335c56
Compare
Summary
Fixes #3829
When
sqliteConnector: 'bun'is configured and the build runs on Node.js (e.g.nuxt build --preset bun), the prerender stage fails with:Root Cause
database.server.tsuses a static top-level import for#content/adapter:Node.js ESM loader resolves ALL static imports at module load time, regardless of whether the imported binding is actually called. During prerender, only
localAdapteris used (line 18), but the static import forces Node.js to resolvebun:sqlite— which fails becausebun:is not a valid Node.js URL scheme.Fix
Replace the static import with a lazy dynamic
import()that is only resolved in the production code path:This makes
loadDatabaseAdapterasync — a minimal API change since all callers (query.post.tsevent handler and_checkAndImportDatabaseIntegrity) already operate in async contexts.Why This Works
bun:sqlite→ ERRORlocalAdapter(Node.js-compatible) → ✅localAdapterlocalAdapter(unchanged)adapterawait getAdapter()→ dynamic import → works in BunChanges
src/runtime/internal/database.server.ts— Remove static import of#content/adapter, add lazygetAdapter(), makeloadDatabaseAdapterasyncsrc/runtime/api/query.post.ts— Await the now-asyncloadDatabaseAdapterTest Plan
#content/adapterloadDatabaseAdapterreturns a workingDatabaseAdaptervia dynamic importMade with Cursor