AI-Powered Search for Static and Edge Sites
Add AI-powered search to Astro sites. Works with island hydration, static builds, and edge deployment.
Astro\'s zero-JS philosophy makes search tricky. You need something that respects static performance while delivering instant results.
Algolia pricing and complexity are designed for large apps. For a static Astro site, it's expensive overhead you don't need.
Lunr.js downloads the entire index to the client. Large content sites see multi-second load times with no typo tolerance.
Build-time indexing requires complex scripts, and there's no real-time updates when content changes in your CMS.
Drop-in search that respects Astro\'s island architecture. Hydrate only what you need, keep the rest static.
Only the search box ships JavaScript — a small script or a framework island. The rest of your page stays static with zero JavaScript.
The search API allows direct browser calls (CORS), so a search-only key works on a fully static site. Zero server required. Works perfectly with astro build.
Deploys to Cloudflare Pages, Vercel Edge, and Netlify Edge. Sub-150ms search, called straight from the edge.
Index .md and .mdx files from Astro content collections with one POST /index/bulk request after each build.
Search is one HTTP request, so it works anywhere JavaScript runs. Use it in Astro islands, vanilla scripts, endpoints, or any component.
No SDK to download. The search script is a few lines of your own code, and it only loads on pages that include it.
Two approaches — Astro island or content collection indexing
---
// src/pages/search.astro — static HTML; the only JS is the small <script> below
import BaseLayout from '../layouts/BaseLayout.astro';
---
<BaseLayout title="Search">
<h1>Search Our Content</h1>
<input id="q" type="search" placeholder="Search articles..." />
<div id="results"></div>
</BaseLayout>
<script>
// Astro bundles this as a module script; no framework runtime is shipped.
const API = 'https://app.searchjetengine.com/api/v1/search';
const SEARCH_KEY = import.meta.env.PUBLIC_SEARCHJET_SEARCH_KEY; // search-only key
const input = document.getElementById('q') as HTMLInputElement;
const out = document.getElementById('results')!;
let timer;
input.addEventListener('input', () => {
clearTimeout(timer);
timer = setTimeout(async () => {
const q = input.value.trim();
if (!q) return out.replaceChildren();
const res = await fetch(`${API}?q=${encodeURIComponent(q)}&limit=10`, {
headers: { Authorization: `Bearer ${SEARCH_KEY}` },
});
const { results = [] } = await res.json(); // results is the array of hits
out.replaceChildren(...results.map((hit) => {
const article = document.createElement('article');
const link = document.createElement('a');
link.href = hit.url;
link.textContent = hit.title;
const excerpt = document.createElement('p');
excerpt.textContent = hit.excerpt ?? '';
article.append(link, excerpt);
return article;
}));
}, 200); // debounce
});
</script>// src/pages/search-docs.json.ts — astro build writes dist/search-docs.json
import { getCollection } from 'astro:content';
export async function GET() {
const posts = await getCollection('blog');
const documents = posts.map((post) => ({
id: post.id,
title: post.data.title,
content: post.body ?? '',
url: `https://example.com/blog/${post.id}/`,
excerpt: post.data.description,
type: 'post',
date: post.data.date.toISOString(),
metadata: { tags: post.data.tags ?? [] },
}));
return new Response(JSON.stringify({ documents }));
}
// scripts/index-content.mjs — run after build: node scripts/index-content.mjs
// Server-side only: the ADMIN key comes from an env var, never from browser code.
import { readFile } from 'node:fs/promises';
const { documents } = JSON.parse(
await readFile('dist/search-docs.json', 'utf8')
);
const res = await fetch('https://app.searchjetengine.com/api/v1/index/bulk', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.SEARCHJET_PRIVATE_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ documents }),
});
const data = await res.json();
console.log(`Indexed ${documents.length} posts (task ${data.taskUid})`);Need a complete starter project?
View GitHub ExamplesIllustrative comparison with a client-side Lunr.js index. Your results will vary.
| Metric | Before | After | Impact |
|---|---|---|---|
| Build Time Impact | Index generated in every build | One POST after deploy | No search index in your build |
| Search Bundle Size | 45KB+ (Lunr) | No SDK — a few lines of fetch | No search library to ship |
| Page Load Time | Full index downloaded | No index to download | Lighter search pages |
| SEO Rankings | Baseline | Faster, lighter pages | Can support better site metrics |
Everything you need to know about SearchJet for Astro
Still have questions?
Contact our support teamJoin developers building exceptional search experiences with SearchJet
Already have an account? Log in