Skip to content

Commit e683445

Browse files
committed
WIP: perf(search): support building fuse.json from cache
1 parent ba68567 commit e683445

1 file changed

Lines changed: 55 additions & 3 deletions

File tree

src/content.config.ts

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,68 @@ type CacheMapping = {
2121
lastUpdated: string;
2222
};
2323

24+
/**
25+
* Create a cache loader function for fuse.json generation.
26+
* This implements InsightJournal-specific logic for mapping DPID identifiers
27+
* to Insight Journal IDs and loading from the local cache.
28+
*
29+
* @param cachePath - Path to the cache directory
30+
* @returns Async function that loads page data from cache by identifier
31+
*/
32+
function createCacheLoader(
33+
cachePath: string
34+
): (identifier: string) => Promise<any | null> {
35+
// Load mapping once at startup
36+
const mappingPath = join(cachePath, "myst", "mapping.json");
37+
let mapping: CacheMapping | null = null;
38+
39+
if (existsSync(mappingPath)) {
40+
try {
41+
const content = readFileSync(mappingPath, "utf-8");
42+
mapping = JSON.parse(content) as CacheMapping;
43+
} catch {
44+
mapping = null;
45+
}
46+
}
47+
48+
// Return the async loader function
49+
return async (identifier: string): Promise<any | null> => {
50+
if (!mapping) return null;
51+
52+
// Extract DPID number from identifier (e.g., "dpid-390" -> "390")
53+
const match = identifier.match(/^dpid-(\d+)$/);
54+
if (!match || !match[1]) return null;
55+
56+
const dpid = match[1];
57+
const insightJournalId = mapping.dpidToInsightJournal[dpid];
58+
if (!insightJournalId) return null;
59+
60+
const cacheFilePath = join(cachePath, "myst", `${insightJournalId}.json`);
61+
if (!existsSync(cacheFilePath)) return null;
62+
63+
try {
64+
const content = readFileSync(cacheFilePath, "utf-8");
65+
return JSON.parse(content);
66+
} catch {
67+
return null;
68+
}
69+
};
70+
}
71+
72+
const cachePath = resolve(process.cwd(), "cache");
73+
2474
const server: InsightJournalMystConfig = {
2575
baseUrl: "https://insight-test.desci.com",
2676
timeout: 15000,
2777
// Enable fuse index generation for search
28-
generateSearchIndex: false,
78+
generateSearchIndex: true,
2979
includeKeywords: true,
3080
pageConcurrency: 4,
31-
// Cache settings - read from local cache first
81+
// Cache settings for custom pages loader
3282
useCache: true,
33-
cachePath: resolve(process.cwd(), "cache"),
83+
cachePath: cachePath,
84+
// Cache loader for fuse.json generation (uses same cache)
85+
cacheLoader: createCacheLoader(cachePath),
3486
};
3587

3688
const project: ProjectConfig = {

0 commit comments

Comments
 (0)