Custom Search
Write a custom search plugin.
A custom search plugin connects another search engine or service to the search dialog. It's built on vuesho/search and vuesho/theme, like the built-in search and the search services, and takes the same options, like filter, buildIndex and tags. It has two parts:
- On the server, it builds or uploads an index per language.
- In the browser, its theme fills the
search-dialogslot.
A site turns off the built-in search with search: false in the recommended preset, then adds the plugin.
Search Engine
An engine searches an index in the browser on a static site, or on the server with server-side search. Its module exports a function that takes the index and returns a SearchFunction:
import type { SearchFunction } from "vuesho/search";
export type AcmeIndex = { url: string; title: string; text: string }[];
export default function searchIndex(index: AcmeIndex): SearchFunction {
return (query) => {
const words = query.toLowerCase();
return index
.filter((page) => `${page.title} ${page.text}`.toLowerCase().includes(words))
.map((page) => ({ id: page.url, url: page.url, type: "page" as const, content: page.title }));
};
}Prop
Type
The plugin creates the index routes with createIndexRoutes(), and passes the dialog options with createSearchRootProps():
import type { AppShape, Plugin } from "vuesho/config";
import {
createIndexRoutes,
getIndexValues,
createSearchRootProps,
type SearchOptions,
} from "vuesho/search";
import searchIndex, { type AcmeIndex } from "./engine";
export function acmeSearchPlugin<C extends AppShape>(options: SearchOptions<C> = {}): Plugin<C> {
return {
name: "acme-search",
theme: "acme-search/theme",
rootProps: createSearchRootProps(options, function () {
return getIndexValues(this, options);
}),
routes(fns) {
createIndexRoutes(this, fns, options, async (indexes) => {
const index: AcmeIndex = (await indexes()).map((page) => ({
url: page.url,
title: page.title,
text: page.structuredData.contents.map((item) => item.content).join(" "),
}));
// `export` returns the index that the browser loads, and `search` answers queries on the server.
return { export: () => index, search: searchIndex(index) };
});
},
};
}Each entry in indexes() is a page's search index:
Prop
Type
The theme fills the slot with the default dialog. useIndexSearch() loads the engine on the first search, or calls the search API on the server:
import type { PluginTheme } from "vuesho/client";
import { defineDefaultSearchDialog, useIndexSearch } from "vuesho/theme";
export default {
slots: {
"search-dialog": defineDefaultSearchDialog((props, tag) =>
useIndexSearch(props, tag, { client: () => import("./engine") }),
),
},
} satisfies PluginTheme;Search Service
The build hook uploads the indexes to a hosted service, and the dialog queries the service. syncIndexes() returns that hook, which skips the upload when writeApiKey is missing:
import type { AppShape, Plugin } from "vuesho/config";
import { createSearchRootProps, syncIndexes, type SearchOptions } from "vuesho/search";
import { upload } from "./service";
export interface AcmeCloudOptions<C extends AppShape> extends Omit<SearchOptions<C>, "render"> {
writeApiKey?: string;
}
export function acmeCloudPlugin<C extends AppShape>(
options: AcmeCloudOptions<NoInfer<C>>,
): Plugin<C> {
return {
name: "acme-cloud",
theme: "acme-cloud/theme",
rootProps: createSearchRootProps(options, (lang) => ({ index: `docs_${lang}` })),
build: syncIndexes("acmeCloudPlugin", options, async function (key, indexesOf) {
for (const lang of this.languages) await upload(key, `docs_${lang}`, await indexesOf(lang));
}),
};
}The search-footer slot of the plugin theme renders the service's label in the dialog's footer. A site can build a custom dialog with the exported composable:
import { computed, h, type Ref } from "vue";
import type { PluginTheme } from "vuesho/client";
import { defineDefaultSearchDialog, type SearchPluginDialogProps } from "vuesho/theme";
import { useSearchFunction } from "vuesho/ui/search";
import { query } from "./service";
export function useAcmeSearch(
props: SearchPluginDialogProps & { index: string },
tag: Ref<string | undefined>,
) {
const run = computed(() => {
const { index } = props;
const selected = tag.value;
return (text: string) => query(index, text, selected);
});
return useSearchFunction(run, { delayMs: props.delayMs });
}
export default {
slots: {
"search-dialog": defineDefaultSearchDialog(useAcmeSearch),
"search-footer": () => h("span", "Acme Search"),
},
} satisfies PluginTheme;When a service's search key is secret, query the service through a route on your site instead. createIndexRoutes() with render: "dynamic" creates that route, and the theme passes useIndexSearch to defineDefaultSearchDialog(), so service.ts stays on the server:
const routes = { path: "/_acme-cloud", render: "dynamic" } as const;
// In the plugin:
rootProps: createSearchRootProps(options, function () {
return getIndexValues(this, options);
}),
routes(fns) {
// Never calls `indexes()`, so a request reads no page.
createIndexRoutes(this, fns, routes, (_indexes, { lang }) => ({
search: (text, { tag }) => query(`docs_${lang}`, text, tag?.[0]),
export: () => [],
}));
},Search Outside the Dialog
To search from another component, call createIndexSearch() from vuesho/theme with the values that getIndexValues() returns. The plugin serves the values from a data route:
routes(fns) {
const options = { path: "/_acme/search" };
createIndexRoutes(this, fns, options, async (indexes) => {
const index = (await indexes()).map(({ url, title }) => ({ url, title, text: "" }));
return { export: () => index, search: searchIndex(index) };
});
fns.createDataRoute({
path: "/_acme/search.json",
shared: true,
load: () => getIndexValues(this, options),
});
},The component reads the values with useDataRoute<IndexValues>("/_acme/search.json"), and the language with useSite().
Search Helpers
vuesho/search exports these helpers for search plugins:
Prop
Type