Skip to content

refactor(search): run browser search on comark-content's worker API - #102

Draft
larbish wants to merge 2 commits into
mainfrom
refactor/search-content-worker
Draft

larbish wants to merge 2 commits into
mainfrom
refactor/search-content-worker

Conversation

@larbish

@larbish larbish commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Why

comarkdown/comark-content#172 adds a worker API to comark-content. This PR moves comark-docs' browser search onto it. Not meant to be merged: it checks the API in a real app with per-commit previews, alongside nuxt/content#3931, which builds Nuxt Content's worker search on it.

How it fits

  • comark-content (#172, and #175 stacked on it): the framework-free worker API, serveContentWorker() and connectContentWorker(), plus buildSearchIndex().
  • Nuxt Content v4 (nuxt/content#3931): wraps it for Nuxt apps: app/content.worker.ts, a generated worker entry, snapshots published in server-only mode, and useNuxtContentWorker() for <UContentSearch>.
  • Docus, comark-docs, nuxt.com: get client-side full-text search from that layer. Docus can then take comark-docs in as its live mode.

This PR uses comark-content directly, without the Nuxt Content layer, to test the API on its own.

What changed

  • Worker (app/workers/search.ts, 153 → 29 lines): reads { sha, debug } from its name and passes one instance, built from that commit's snapshot, to serveContentWorker(). The SHA cache, eviction, fetch wrapper and artifact logging are removed.
  • Composable (app/composables/useSearch.ts): one worker per commit. A switch terminates the current worker and starts the next commit's, passing { sha, debug } through the Worker's name. client.buildSearchIndex() drives the <UContentSearch> status. A search still pending on a terminated worker resolves to no results instead of an error.
  • Dependencies: nuxt-workers is removed, since a plain new Worker(new URL(...), { type: 'module', name }) is enough. comark-content is installed from https://pkg.pr.new/comark-content@ec7a084, #175's build, which contains #172 too.

Evidence

Before, the worker exported its own RPC, with an SHA cache and eviction, and warmed the index with an empty search:

export async function warmupSearch(target: SearchTarget): Promise<void> { /* cache, eviction */ }
export async function searchContent(query: string, opts?: SearchOptions) { /* ... */ }
// ...
await content.search('') // pulls the snapshot in and builds the FTS index

After, the whole worker:

const { sha, debug } = JSON.parse(self.name) as SearchWorkerInit
setDebug(debug)

const apiBase = sha ? `/api/content/blob/${sha}` : '/api/content'

serveContentWorker(comarkContent({
  source: snapshot(`${apiBase}/snapshot/${DEFAULT_CONTENT_NAME}.json`, `${apiBase}/manifest.json`),
  plugins: [sqliteFullTextSearch({ database: sqliteWasm() })],
  logger,
}))

And the page side of a switch:

active?.connection.terminate()
active = start(key, { sha: current.sha, debug })

The production build keeps name in the bundled worker call:

new Worker(new URL(new URL(`search-sNxe9F3I.js`,import.meta.url).href,``+import.meta.url),{type:`module`,name:JSON.stringify(t)})

Production build, in the browser, switching in the page from prod to /blob/96c1546 (a commit from before the "Code highlighting" section) and back. Each step starts a new worker. The local server had just restarted, so it fetched each commit from GitHub on first use:

Step Query Result Ready in
prod vitesse 1 result, /getting-started/configuration#code-highlighting 1.9 s
preview 96c1546 vitesse 0 results 1.1 s
preview 96c1546 snapshot links under /blob/96c1546/…
back to prod vitesse 1 result again 677 ms

A read pending when the worker is terminated rejects with "The content worker was terminated." (checked with connectContentWorker() on a MessageChannel), and useSearch() turns that into no results.

Client assets (_nuxt), main → this branch: the search worker chunk goes from 272.8 to 263.1 kB and the entry chunk grows by 1.4 kB.

pnpm lint, pnpm typecheck, pnpm test (211 passed) and pnpm build pass. pack fails because pnpm blocks the pkg.pr.new URL as a dependency of a dependency (ERR_PNPM_EXOTIC_SUBDEP); it passes again once comark-content is a version range.

Notes

  • A switch starts a new worker, so going back to prod loads and indexes it again (about 115 ms in an earlier run with warm caches). Before, prod stayed loaded in a separate worker.
  • A missing snapshot (404) still surfaces as "could not initialize", without the status or URL.
  • The two unused sqlite-wasm helper files (about 245 kB) are still emitted. The separate comark-content PR with sqliteWasmWorker() removes them.

ELI5

comark-docs searches its pages inside a background helper in the browser, so the page doesn't freeze while the search index loads. It used to wire that helper up itself. Now it uses the shared helper from comark-content, which Nuxt Content and Docus will use too, and starts a fresh helper when you switch between versions of the docs.

🤖 Generated with Amp, reviewed by @larbish before opening.

@vercel

vercel Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
comark-docs-layer Ready Ready Preview Nov 26, 2026 4:00pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

@pkg-pr-new

pkg-pr-new Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
npm i https://pkg.pr.new/comarkdown/comark-docs@102

commit: d262529

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Bundle size

Explore the comparison →

Client 860.7 kB $\color{red}\textsf{(+354 B, +0.0}\%\textsf{)}$ · Server 6.55 MB $\color{green}\textsf{(-6.65 kB, -0.1}\%\textsf{)}$

Initial page load PR Δ
/ 320.9 kB +354 B (+0.1%)
/:slug(.+) 718.6 kB +354 B (+0.0%)
/docs 233.1 kB +354 B (+0.2%)
/full 233.2 kB +354 B (+0.2%)
/logos 240.2 kB +354 B (+0.1%)
Prerendered URL Initial load Δ HTML Δ
/ 390.4 kB
69.8 kB from components
−2.78 kB (−0.7%)
route +354 B, components −3.13 kB
14.6 kB +12 B (+0.1%)
/getting-started/introduction 788.3 kB
69.8 kB from components
−2.78 kB (−0.4%)
route +354 B, components −3.13 kB
16.1 kB −9 B (−0.1%)
/page 788.3 kB
69.8 kB from components
−2.78 kB (−0.4%)
route +354 B, components −3.13 kB
12.5 kB −1 B (−0.0%)
Largest changes in the client
Package or file Base PR Δ
node_modules/comark-content 584 B 1.03 kB +442 B (+75.7%)
virtual:nuxt-workers.mjs?source=/home/runner/work/comark-docs/comark-docs/app/workers/search.ts 166 B n/a −166 B (−100.0%)
app/composables/useSearch.ts 286 B 429 B +143 B (+50.0%)
Largest changes in the server
Package or file Base PR Δ
node_modules/comark-content 6.03 kB n/a −6.03 kB (−100.0%)
app/workers/search.ts 939 B n/a −939 B (−100.0%)
[unassigned] 5.29 MB 5.29 MB +670 B (+0.0%)
app/workers/internal/search-logger.ts 508 B n/a −508 B (−100.0%)
app/composables/useSearch.ts 384 B 576 B +192 B (+50.0%)
node_modules/elkjs 368.6 kB 368.5 kB −136 B (−0.0%)
virtual:nuxt-workers.mjs?source=/home/runner/work/comark-docs/comark-docs/app/workers/search.ts 79 B n/a −79 B (−100.0%)

Brotli sizes. Changes under 100 B are left out of the tables. Initial page load counts the JavaScript and CSS a route loads before it renders. Prerendered URLs count the JavaScript and CSS their HTML preloads (module scripts, modulepreload and stylesheets, not prefetch), and the HTML document. Components are the lazy-loaded components the page rendered: its HTML preloads them, but its route's static imports don't reach them. Measured by nuxt-bundle-analyzer for d262529.

This branch was successfully deployed

1 active deployment
Preview — d2625294 Deployed Nov 26, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants