-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathroute.ts
More file actions
72 lines (65 loc) · 1.93 KB
/
Copy pathroute.ts
File metadata and controls
72 lines (65 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import type { StructuredData } from "fumadocs-core/mdx-plugins";
import type { DocumentRecord } from "fumadocs-core/search/algolia";
import { NextResponse } from "next/server";
import { programsSource, source } from "@/lib/source";
export const revalidate = false;
// Type for page data with structured data
interface PageWithStructuredData {
url: string;
data: {
title: string;
description?: string;
structuredData: StructuredData;
};
}
// Helper function to map pages to DocumentRecord format
function mapPageToDocumentRecord(
page: PageWithStructuredData,
tag: string,
): DocumentRecord {
return {
_id: page.url,
structured: page.data.structuredData,
url: page.url,
title: page.data.title,
description: page.data.description || "",
tag,
};
}
// Helper function to safely get pages and map them
function mapPagesToDocuments(
sourceInstance: typeof source,
tag: string,
): DocumentRecord[] {
try {
return sourceInstance
.getPages()
.map((page) => mapPageToDocumentRecord(page, tag));
} catch (error) {
console.error(`Error mapping ${tag} pages:`, error);
return [];
}
}
export function GET() {
try {
// Process both sources efficiently with better error handling
const docsPages = mapPagesToDocuments(source, "docs");
const programPages = mapPagesToDocuments(programsSource, "programs");
// Combine arrays efficiently
const results: DocumentRecord[] = [...docsPages, ...programPages];
// Add cache headers for better performance
return new NextResponse(JSON.stringify(results), {
status: 200,
headers: {
"Content-Type": "application/json",
"Cache-Control": "public, max-age=3600, stale-while-revalidate=86400",
},
});
} catch (error) {
console.error("Error generating static.json:", error);
return NextResponse.json(
{ error: "Failed to generate search data" },
{ status: 500 },
);
}
}