Skip to content

Commit 02df49c

Browse files
authored
fix: ssr search endppoint (#467)
1 parent 91c93fc commit 02df49c

2 files changed

Lines changed: 26 additions & 11 deletions

File tree

packages/app/app/components/RepoSearch.vue

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
<script lang="ts" setup>
22
import type { RepoNode } from "../../server/utils/types";
33
4-
const search = useSessionStorage("search", "");
4+
const route = useRoute();
5+
const router = useRouter();
6+
const search = computed({
7+
get: () => {
8+
const q = route.query.q;
9+
return typeof q === "string" ? q : "";
10+
},
11+
set: (value: string) => {
12+
const queryText = value.trim();
13+
router.replace({
14+
query: {
15+
...route.query,
16+
q: queryText || undefined,
17+
},
18+
});
19+
},
20+
});
521
const searchResults = ref<RepoNode[]>([]);
622
const isLoading = ref(false);
723
@@ -24,11 +40,10 @@ watch(
2440
isLoading.value = true;
2541
2642
try {
27-
const response = await fetch(
28-
`/api/repo/search?text=${encodeURIComponent(query)}`,
29-
{ signal: controller.signal },
30-
);
31-
const data = await response.json();
43+
const data = await $fetch<{ nodes?: RepoNode[] }>("/api/repo/search", {
44+
query: { q: query },
45+
signal: controller.signal,
46+
});
3247
3348
if (abortController !== controller) {
3449
return;
@@ -47,7 +62,7 @@ watch(
4762
}
4863
}
4964
},
50-
{ immediate: false },
65+
{ immediate: true },
5166
);
5267
5368
const examples = [
@@ -78,7 +93,6 @@ const examples = [
7893
},
7994
];
8095
81-
const router = useRouter();
8296
function openFirstResult() {
8397
const [first] = searchResults.value;
8498
if (first) {

packages/app/server/api/repo/search.get.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { useBucket } from "../../utils/bucket";
55
import { useOctokitApp } from "../../utils/octokit";
66

77
const querySchema = z.object({
8-
text: z.string(),
8+
q: z.string().optional(),
99
});
1010

1111
const REPO_INDEX_CACHE_KEY = "repo-search:index";
@@ -168,15 +168,16 @@ export default defineEventHandler(async (event) => {
168168
const query = await getValidatedQuery(event, (data) =>
169169
querySchema.parse(data),
170170
);
171+
const searchQuery = (query.q ?? "").trim();
171172

172-
if (!query.text) {
173+
if (!searchQuery) {
173174
return { nodes: [] };
174175
}
175176

176177
const { repos, cacheStatus } = await getIndexedRepos(event);
177178
setResponseHeader(event, "x-repo-index-cache", cacheStatus);
178179

179180
return {
180-
nodes: findMatches(repos, query.text),
181+
nodes: findMatches(repos, searchQuery),
181182
};
182183
});

0 commit comments

Comments
 (0)