Skip to content

Commit cca8415

Browse files
committed
moved to static api route instead
1 parent b22c1e2 commit cca8415

File tree

5 files changed

+82
-14
lines changed

5 files changed

+82
-14
lines changed

apps/framework-docs-v2/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"test:snippets": "tsx scripts/test-snippets.ts"
1313
},
1414
"dependencies": {
15+
"@iarna/toml": "^2.2.5",
1516
"@icons-pack/react-simple-icons": "^13.8.0",
1617
"@mdx-js/loader": "^3.1.1",
1718
"@mdx-js/react": "^3.1.1",
@@ -39,7 +40,6 @@
3940
"class-variance-authority": "^0.7.1",
4041
"clsx": "^2.1.1",
4142
"cmdk": "^1.0.0",
42-
"@iarna/toml": "^2.2.5",
4343
"gray-matter": "^4.0.3",
4444
"html-to-text": "^9.0.5",
4545
"motion": "^12.23.12",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { NextResponse } from "next/server";
2+
import { getAllItems } from "@/lib/templates";
3+
4+
export const dynamic = "force-static";
5+
6+
export async function GET() {
7+
try {
8+
const items = getAllItems();
9+
10+
return NextResponse.json(items, {
11+
headers: {
12+
"Cache-Control": "public, s-maxage=3600, stale-while-revalidate=86400",
13+
},
14+
});
15+
} catch (error) {
16+
console.error("Failed to generate templates data:", error);
17+
return NextResponse.json(
18+
{ error: "Internal Server Error" },
19+
{ status: 500 },
20+
);
21+
}
22+
}
Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,49 @@
1-
import { TemplateGrid } from "@/components/mdx/template-grid";
2-
import { getAllItems } from "@/lib/templates";
1+
"use client";
2+
3+
import * as React from "react";
4+
import { TemplateGrid } from "./template-grid";
5+
import type { ItemMetadata } from "@/lib/template-types";
6+
7+
export function TemplatesGridServer() {
8+
const [items, setItems] = React.useState<ItemMetadata[]>([]);
9+
const [loading, setLoading] = React.useState(true);
10+
const [error, setError] = React.useState<string | null>(null);
11+
12+
React.useEffect(() => {
13+
fetch("/api/templates")
14+
.then((res) => {
15+
if (!res.ok) {
16+
throw new Error("Failed to fetch templates");
17+
}
18+
return res.json();
19+
})
20+
.then((data) => {
21+
setItems(data);
22+
setLoading(false);
23+
})
24+
.catch((err) => {
25+
console.error("Error fetching templates:", err);
26+
setError(err.message);
27+
setLoading(false);
28+
});
29+
}, []);
30+
31+
if (loading) {
32+
return (
33+
<div className="text-center py-12 text-muted-foreground">
34+
<p>Loading templates...</p>
35+
</div>
36+
);
37+
}
38+
39+
if (error) {
40+
return (
41+
<div className="text-center py-12 text-muted-foreground">
42+
<p className="text-lg font-medium mb-2">Error loading templates</p>
43+
<p className="text-sm">{error}</p>
44+
</div>
45+
);
46+
}
347

4-
export async function TemplatesGridServer() {
5-
const items = getAllItems();
648
return <TemplateGrid items={items} />;
749
}

apps/framework-docs-v2/src/lib/templates.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export function getAllTemplates(): TemplateMetadata[] {
164164

165165
try {
166166
const configContent = fs.readFileSync(configPath, "utf-8");
167-
const config = parse(configContent) as TemplateConfig;
167+
const config = parse(configContent) as unknown as TemplateConfig;
168168

169169
// Skip if visible is explicitly false
170170
if (config.visible === false) {
@@ -186,7 +186,7 @@ export function getAllTemplates(): TemplateMetadata[] {
186186
slug: templateName,
187187
language: config.language,
188188
description: config.description,
189-
visible: config.visible !== false,
189+
visible: config.visible ?? true,
190190
category,
191191
frameworks,
192192
features,

pnpm-lock.yaml

Lines changed: 11 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)