Skip to content

Commit 5dd794b

Browse files
committed
Migrate to new app router
1 parent 3af275e commit 5dd794b

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

app/api/search/route.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { NextResponse } from 'next/server'
2+
import Fuse from 'fuse.js'
3+
import { readIndex } from '../../../utils/posts.js'
4+
5+
export const runtime = 'edge'
6+
7+
// Define the handler for GET requests
8+
export async function GET (req: Request) {
9+
const { searchParams } = new URL(req.url)
10+
const q = searchParams.get('q') ?? ''
11+
12+
const search = Array.isArray(q) ? q.join(' ') : q
13+
14+
const index = await readIndex()
15+
16+
const fuse = new Fuse(index, {
17+
keys: ['text'],
18+
includeScore: true,
19+
includeMatches: true,
20+
minMatchCharLength: 2
21+
})
22+
23+
const results = fuse.search(search).slice(0, 5)
24+
25+
console.log(results)
26+
27+
// Return the results as a JSON response
28+
return NextResponse.json(results)
29+
}

pages/api/search.js

-21
This file was deleted.

0 commit comments

Comments
 (0)