Skip to content

Commit a0ea108

Browse files
committed
improve search indexing speed slightly
1 parent 466dfce commit a0ea108

File tree

1 file changed

+44
-31
lines changed

1 file changed

+44
-31
lines changed

src/components/common/Search.astro

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,81 @@
11
---
22
import { render } from "astro:content";
33
import { getCollection } from "astro:content";
4+
import path from "path";
45
import type { searchItem } from "~/services/search";
56
import { searchTerms } from "~/services/search";
67
78
/**
89
* Infers the static URL of a wiki article from its file path.
9-
* @param path filepath of article
10+
* @param filepath filepath of article
1011
* @param target optional target heading
1112
*/
12-
const getContentStaticURL = (path: string, target?: string): string => {
13-
const rgx = /src\/.+?\/(.+)\..*?/g;
13+
const getContentStaticURL = (filepath: string, target?: string): string => {
14+
const relative = path.relative("src/content", filepath);
15+
const extension = path.extname(relative);
1416
15-
console.log(path);
16-
17-
const result = rgx.exec(path);
18-
if (result === null) throw new Error(`Failed to infer static URL of ${path}`);
17+
const absolute = `/${relative.replace(extension, "")}/`;
1918
2019
if (target) {
21-
return `/${result[1]}/#${target}`;
20+
return absolute + "#" + target
2221
} else {
23-
return `/${result[1]}/`;
22+
return absolute
2423
}
2524
};
2625
27-
const searchTags: searchItem[] = [...searchTerms];
26+
const needles: searchItem[] = [...searchTerms];
2827
29-
if (searchTags.length === searchTerms.length) {
28+
const loadNeedles = async () => {
3029
const pages = await getCollection("wiki");
3130
31+
const promises = [];
32+
3233
for (const page of pages) {
33-
if (!page.filePath) {
34-
throw new Error(`Page ${page.id} does not have a file path}`);
35-
}
34+
promises.push(
35+
new Promise(async (resolve, reject) => {
36+
if (!page.filePath) {
37+
return reject(
38+
`Page ${page.id} does not have a file path. This shouldn't ever happen unless something has gone wrong with Astro.`
39+
);
40+
}
3641
37-
const { headings } = await render(page);
42+
const { headings } = await render(page);
3843
39-
const href = getContentStaticURL(page.filePath);
44+
const href = getContentStaticURL(page.filePath);
4045
41-
// Add article title to search options
42-
searchTags.push({ text: page.data.title, href });
46+
// Article title entry
47+
needles.push({ text: page.data.title, href });
4348
44-
// Add custom article tags to search options
45-
for (const tag of page.data.tags ?? []) {
46-
searchTags.push({ text: tag, href });
47-
}
49+
// Article tags
50+
for (const tag of page.data.tags ?? []) {
51+
needles.push({ text: tag, href });
52+
}
4853
49-
// Add article headings to search options
50-
for (const heading of headings) {
51-
searchTags.push({
52-
text: heading.text,
53-
href: getContentStaticURL(page.filePath, heading.slug),
54-
});
55-
}
54+
// Article headings
55+
for (const heading of headings) {
56+
needles.push({
57+
text: heading.text,
58+
href: getContentStaticURL(page.filePath, heading.slug),
59+
});
60+
}
61+
62+
resolve(true);
63+
})
64+
);
5665
}
57-
}
66+
67+
await Promise.all(promises);
68+
};
69+
70+
await loadNeedles();
5871
---
5972

6073
<div id="site-search">
6174
<dialog>
6275
<input type="search" placeholder="Search..." autofocus />
6376
<div class="suggestions" tabindex="-1">
6477
{
65-
searchTags.map((entry) => (
78+
needles.map((entry) => (
6679
<a
6780
href={entry.href}
6881
target={entry.href.startsWith("/") ? undefined : "_blank"}

0 commit comments

Comments
 (0)