Next.js
Use Insightcn in Next.js applications.
App Router
Track Installation Events
import { NextRequest, NextResponse } from "next/server";
import { captureRegistryEvent } from "@insightcn/sdk";
export async function GET(
request: NextRequest,
{ params }: { params: { slug: string[] } }
) {
const registryUrl = `https://your-registry.com/r/${params.slug.join("/")}.json`;
const registryResponse = await fetch(registryUrl);
await captureRegistryEvent(
request,
process.env.INSIGHTCN_API_KEY!,
"installed"
);
return NextResponse.json(await registryResponse.json());
}Generate RSS Feed
import { NextResponse } from "next/server";
import { generateRegistryRssFeed } from "@insightcn/sdk";
export async function GET() {
const rssFeed = await generateRegistryRssFeed({
baseUrl: "https://your-registry.com",
rss: {
title: "My Shadcn Registry",
description: "Custom components for shadcn/ui",
endpoint: "/api/rss",
},
github: {
owner: "your-org",
repo: "your-registry",
token: process.env.GITHUB_TOKEN,
},
});
if (!rssFeed) {
return new NextResponse("No items found", { status: 404 });
}
return new NextResponse(rssFeed, {
headers: {
"Content-Type": "application/rss+xml",
"Cache-Control": "public, max-age=3600",
},
});
}Pages Router
Track Installation Events
import type { NextApiRequest, NextApiResponse } from "next";
import { captureRegistryEvent } from "@insightcn/sdk";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const slug = req.query.slug as string[];
const registryUrl = `https://your-registry.com/r/${slug.join("/")}.json`;
const registryResponse = await fetch(registryUrl);
await captureRegistryEvent(
req as unknown as Request,
process.env.INSIGHTCN_API_KEY!,
"installed"
);
res.json(await registryResponse.json());
}Generate RSS Feed
import type { NextApiRequest, NextApiResponse } from "next";
import { generateRegistryRssFeed } from "@insightcn/sdk";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const rssFeed = await generateRegistryRssFeed({
baseUrl: "https://your-registry.com",
rss: {
title: "My Shadcn Registry",
description: "Custom components for shadcn/ui",
endpoint: "/api/rss",
},
github: {
owner: "your-org",
repo: "your-registry",
token: process.env.GITHUB_TOKEN,
},
});
if (!rssFeed) {
return res.status(404).send("No items found");
}
res.setHeader("Content-Type", "application/rss+xml");
res.setHeader("Cache-Control", "public, max-age=3600");
res.send(rssFeed);
}Environment Variables
INSIGHTCN_API_KEY=your-api-key
GITHUB_TOKEN=your-github-token