-
-
Notifications
You must be signed in to change notification settings - Fork 749
fix(runtime): use dynamic import for #content/adapter to prevent prerender failure
#3830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
farnabaz
merged 3 commits into
nuxt:main
from
gepotumu:fix/lazy-adapter-import-prerender
Aug 26, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| export default function mockAdapter(_opts: unknown) { | ||
| return { | ||
| prepare: (_sql: string) => ({ | ||
| all: (..._params: unknown[]) => Promise.resolve([]), | ||
| get: (..._params: unknown[]) => Promise.resolve(null), | ||
| run: (..._params: unknown[]) => Promise.resolve(undefined), | ||
| }), | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| export default function mockLocalAdapter(_opts: unknown) { | ||
| return { | ||
| prepare: (_sql: string) => ({ | ||
| all: (..._params: unknown[]) => Promise.resolve([]), | ||
| get: (..._params: unknown[]) => Promise.resolve(null), | ||
| run: (..._params: unknown[]) => Promise.resolve(undefined), | ||
| }), | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,13 @@ | ||
| export const tables = { | ||
| test: '_content_test', | ||
| info: '_content_info', | ||
| } | ||
|
|
||
| export const checksums: Record<string, string> = {} | ||
| export const checksumsStructure: Record<string, string> = {} | ||
|
|
||
| const manifest: Record<string, { fields: Record<string, string> }> = { | ||
| test: { fields: { id: 'string', title: 'string' } }, | ||
| } | ||
|
|
||
| export default manifest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import { readFile } from 'node:fs/promises' | ||
| import { resolve } from 'node:path' | ||
| import { afterEach, describe, expect, test, vi } from 'vitest' | ||
| import type { RuntimeConfig } from '@nuxt/content' | ||
|
|
||
| /** | ||
| * Regression test for https://github.com/nuxt/content/issues/3829 | ||
| * | ||
| * When `sqliteConnector: 'bun'` is configured and the build runs on Node.js, | ||
| * the prerender stage fails because Node.js cannot resolve `bun:sqlite`. | ||
| * | ||
| * Root cause: `database.server.ts` had a static top-level import for | ||
| * `#content/adapter`. Node.js ESM loader resolves ALL static imports at | ||
| * module load time, even when the imported binding is never called at runtime. | ||
| * | ||
| * Fix: `#content/adapter` is now loaded via dynamic `import()` only in the | ||
| * production code path, so the prerender stage never triggers `bun:sqlite` | ||
| * resolution. | ||
| */ | ||
| describe('database.server - lazy adapter loading (issue #3829)', () => { | ||
| const config: RuntimeConfig['content'] = { | ||
| database: { type: 'sqlite', filename: ':memory:' }, | ||
| localDatabase: { type: 'sqlite', filename: ':memory:' }, | ||
| databaseVersion: 'test', | ||
| } as RuntimeConfig['content'] | ||
|
|
||
| afterEach(() => { | ||
| vi.resetModules() | ||
| vi.restoreAllMocks() | ||
| }) | ||
|
|
||
| test('source does NOT contain a static top-level import of #content/adapter', async () => { | ||
| const source = await readFile( | ||
| resolve(__dirname, '../../src/runtime/internal/database.server.ts'), | ||
| 'utf-8', | ||
| ) | ||
|
|
||
| // Should NOT have a static import statement for #content/adapter | ||
| const staticImportPattern = /^import\s+\w+\s+from\s+['"]#content\/adapter['"]/m | ||
| expect(source).not.toMatch(staticImportPattern) | ||
|
|
||
| // Should still have the local-adapter static import (that one is fine, | ||
| // it resolves to a Node.js-compatible connector) | ||
| const localAdapterPattern = /^import\s+\w+\s+from\s+['"]#content\/local-adapter['"]/m | ||
| expect(source).toMatch(localAdapterPattern) | ||
|
|
||
| // Should have a dynamic import of #content/adapter | ||
| const dynamicImportPattern = /import\(['"]#content\/adapter['"]\)/ | ||
| expect(source).toMatch(dynamicImportPattern) | ||
| }) | ||
|
|
||
| test('loadDatabaseAdapter is async and returns a DatabaseAdapter', async () => { | ||
| vi.doMock('#content/adapter', () => ({ | ||
| default: (_opts: unknown) => ({ | ||
| prepare: (_sql: string) => ({ | ||
| all: (..._params: unknown[]) => Promise.resolve([{ id: '1', title: 'Hello' }]), | ||
| get: (..._params: unknown[]) => Promise.resolve({ id: '1', title: 'Hello' }), | ||
| run: (..._params: unknown[]) => Promise.resolve(undefined), | ||
| }), | ||
| }), | ||
| })) | ||
| vi.doMock('#content/local-adapter', () => ({ | ||
| default: (_opts: unknown) => ({ | ||
| prepare: (_sql: string) => ({ | ||
| all: (..._params: unknown[]) => Promise.resolve([]), | ||
| get: (..._params: unknown[]) => Promise.resolve(null), | ||
| run: (..._params: unknown[]) => Promise.resolve(undefined), | ||
| }), | ||
| }), | ||
| })) | ||
|
|
||
| const mod = await import('../../src/runtime/internal/database.server') | ||
| const loadDatabaseAdapter = mod.default | ||
|
|
||
| const result = loadDatabaseAdapter(config) | ||
| expect(result).toBeInstanceOf(Promise) | ||
|
|
||
| const db = await result | ||
| expect(db).toBeDefined() | ||
| expect(db.all).toBeTypeOf('function') | ||
| expect(db.first).toBeTypeOf('function') | ||
| expect(db.exec).toBeTypeOf('function') | ||
| }) | ||
|
|
||
| test('loadDatabaseAdapter production path uses dynamic adapter import', async () => { | ||
| const adapterFn = vi.fn((_opts: unknown) => ({ | ||
| prepare: (_sql: string) => ({ | ||
| all: (..._params: unknown[]) => Promise.resolve([{ id: '1' }]), | ||
| get: (..._params: unknown[]) => Promise.resolve({ id: '1' }), | ||
| run: (..._params: unknown[]) => Promise.resolve(undefined), | ||
| }), | ||
| })) | ||
|
|
||
| vi.doMock('#content/adapter', () => ({ default: adapterFn })) | ||
| vi.doMock('#content/local-adapter', () => ({ | ||
| default: vi.fn(() => ({ prepare: vi.fn() })), | ||
| })) | ||
|
|
||
| const mod = await import('../../src/runtime/internal/database.server') | ||
| const loadDatabaseAdapter = mod.default | ||
|
|
||
| // In the test environment (non-dev, non-prerender), the production path is taken | ||
| const db = await loadDatabaseAdapter(config) | ||
| expect(adapterFn).toHaveBeenCalledOnce() | ||
|
|
||
| // Subsequent calls should reuse the cached connection | ||
| await loadDatabaseAdapter(config) | ||
| expect(adapterFn).toHaveBeenCalledOnce() // still only once | ||
|
|
||
| const result = await db.all('SELECT * FROM test') | ||
| expect(result).toHaveLength(1) | ||
| }) | ||
|
|
||
| test('concurrent first calls share a single adapter initialization', async () => { | ||
| let releaseAdapter!: () => void | ||
| const adapterGate = new Promise<void>((resolve) => { | ||
| releaseAdapter = resolve | ||
| }) | ||
|
|
||
| const adapterFn = vi.fn((_opts: unknown) => ({ | ||
| prepare: (_sql: string) => ({ | ||
| all: (..._params: unknown[]) => Promise.resolve([{ id: '1' }]), | ||
| get: (..._params: unknown[]) => Promise.resolve({ id: '1' }), | ||
| run: (..._params: unknown[]) => Promise.resolve(undefined), | ||
| }), | ||
| })) | ||
|
|
||
| // Keep the dynamic import pending so both callers enter initialization | ||
| // before either can finish creating the connector. | ||
| vi.doMock('#content/adapter', async () => { | ||
| await adapterGate | ||
| return { default: adapterFn } | ||
| }) | ||
| vi.doMock('#content/local-adapter', () => ({ | ||
| default: vi.fn(() => ({ prepare: vi.fn() })), | ||
| })) | ||
|
|
||
| const loadDatabaseAdapter = (await import('../../src/runtime/internal/database.server')).default | ||
|
|
||
| const firstPromise = loadDatabaseAdapter(config) | ||
| const secondPromise = loadDatabaseAdapter(config) | ||
|
|
||
| // Allow both calls to reach the shared initialization await | ||
| await Promise.resolve() | ||
| expect(adapterFn).not.toHaveBeenCalled() | ||
|
|
||
| releaseAdapter() | ||
|
|
||
| const [first, second] = await Promise.all([firstPromise, secondPromise]) | ||
|
|
||
| expect(adapterFn).toHaveBeenCalledOnce() | ||
| expect(first).toBeDefined() | ||
| expect(second).toBeDefined() | ||
|
|
||
| const [a, b] = await Promise.all([ | ||
| first.all('SELECT * FROM test'), | ||
| second.all('SELECT * FROM test'), | ||
| ]) | ||
| expect(a).toHaveLength(1) | ||
| expect(b).toHaveLength(1) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.