Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions app/composables/useDocsContent.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { createContentClient } from 'comark-content/client'
import { createContentClient, type ContentClient } from 'comark-content/client'
import type { ActiveContent, ContentMode } from '../types/content'
import { withLeadingSlash } from 'ufo'

export const prodContent = createContentClient({
basePath: '/api/content',
fetch: $fetch,
})

const clients = new Map<string, typeof prodContent>()
// Production reads Nuxt Content's own client, on `/__nuxt_content`. Previews have one client each.
const clients = new Map<string, ContentClient<string>>()

function getClient(basePath: string) {
let client = clients.get(basePath)
Expand Down Expand Up @@ -54,7 +50,7 @@ export function useDocsContent(): ComputedRef<ActiveContent> {
ref,
routeBase,
path,
client: mode === 'prod' ? prodContent : getClient(apiBase),
client: mode === 'prod' ? content : getClient(apiBase),
apiBase
}
})
Expand Down
83 changes: 15 additions & 68 deletions app/composables/useSearch.ts
Original file line number Diff line number Diff line change
@@ -1,92 +1,39 @@
import type { SearchOptions, SearchResult } from 'comark-content'

type SearchStatus = 'idle' | 'loading' | 'ready' | 'error'

const status = ref<SearchStatus>('idle')

/**
* Hydration logging switch: `?debug=search`
*/
function searchDebug(): boolean {
if (!import.meta.client) return false
return new URLSearchParams(location.search).get('debug') === 'search'
}

/**
* Client-side full-text search over the active route's content (sqlite-wasm FTS5)
* Hydrated from the per-commit snapshot artifacts.
* Full-text search over the active route's content, in Nuxt Content's worker (sqlite-wasm FTS5).
* Loaded from the per-commit snapshot of the commit the route serves.
*/
export function useSearch() {
const content = useDocsContent()
const sha = inject<Ref<string | null | undefined>>('sha', ref(null))

/** What to hydrate from, or `null` when this route has nothing searchable (yet). */
const target = computed(() => {
const isPreview = content.value.mode !== 'prod'
if (sha.value) return {
sha: sha.value,
isPreview,
apiBase: `/api/content/blob/${sha.value}`
}

// Prod in dev mode
if (!isPreview && import.meta.dev) return {
sha: null,
isPreview,
apiBase: '/api/content'
}

return null
})

/**
* Load the database for the current target.
* No-ops if the target is unchanged.
*/
async function warmup(): Promise<void> {
// Not resolved yet — the `watch` below re-runs this once `sha` lands.
if (sha.value === undefined) return

const current = target.value

if (!current) {
if (content.value.mode === 'prod' && !import.meta.dev) {
console.error('[search] prod route resolved no commit pin — search hidden')
}
return
}
// The URL never changes for a commit, so the CDN keeps it. A new commit starts a new worker.
const worker = useNuxtContentWorker({ basePath: () => (sha.value ? `/api/content/blob/${sha.value}` : undefined) })

status.value = 'loading'
try {
const debug = searchDebug()
if (debug) console.info(`[search] warmup from ${current.apiBase} (sha ${current.sha ?? 'unpinned'})`)
// Production in dev has no commit: the worker reads the dev server, which follows the working tree.
const searchable = computed(() => Boolean(sha.value) || (import.meta.dev && content.value.mode === 'prod'))

await warmupSearch({
apiBase: current.apiBase,
sha: current.sha,
isPreview: current.isPreview,
origin: location.origin,
debug,
})
status.value = 'ready'
} catch (error) {
status.value = 'error'
console.error('[search] could not load the search database', error)
}
function warmup() {
if (searchable.value) worker.warmup().catch((error) => console.error('[search] could not load the search database', error))
}

if (import.meta.client) {
onNuxtReady(warmup)
watch(() => target.value && `${target.value.isPreview}:${target.value.sha}`, warmup)
watch(searchable, warmup)
}

async function search(query: string, opts?: SearchOptions): Promise<SearchResult[]> {
return prefixSearchResults(await searchContent(query, opts), content.value.routeBase)
if (!searchable.value) return []
return prefixSearchResults(
await worker.search(query, { limit: 25, snippet: { columns: ['content'] }, ...opts }),
content.value.routeBase,
)
}

return {
search,
status: readonly(status),
status: worker.status,
warmup,
}
}
6 changes: 6 additions & 0 deletions app/content.worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createNuxtContentWorker, sqliteFullTextSearch, sqliteWasm } from '@nuxt/content/worker'

// Only evaluated in the search worker, so sqlite-wasm stays out of the pages' bundle.
export default createNuxtContentWorker({
plugins: [sqliteFullTextSearch({ database: sqliteWasm() })],
})
66 changes: 0 additions & 66 deletions app/workers/internal/search-logger.ts

This file was deleted.

153 changes: 0 additions & 153 deletions app/workers/search.ts

This file was deleted.

Loading
Loading