Skip to content

Commit 80944bc

Browse files
committed
bundled templates approach
1 parent 4b99d35 commit 80944bc

File tree

7 files changed

+378
-120
lines changed

7 files changed

+378
-120
lines changed

apps/docs/.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@ yarn-error.log*
2525
# others
2626
.env*.local
2727
.vercel
28-
next-env.d.ts
28+
next-env.d.ts
29+
30+
# Registry build artifacts
31+
public/registry-templates/

apps/docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.0.2",
44
"private": true,
55
"scripts": {
6-
"build": "next build",
6+
"build": "node scripts/bundle-registry-templates.js && next build",
77
"dev": "next dev -p 3005 --turbo",
88
"start": "next start",
99
"postinstall": "fumadocs-mdx",
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
6+
/**
7+
* This script copies registry template files into the Next.js build
8+
* so they're available as static assets in serverless environments
9+
*/
10+
11+
async function bundleRegistryTemplates() {
12+
console.log('📦 Bundling registry templates...');
13+
14+
try {
15+
// Source: registry package templates
16+
const registryPath = path.resolve(__dirname, '../../../packages/registry/templates');
17+
18+
// Destination: Next.js public directory (served as static assets)
19+
const publicRegistryPath = path.resolve(__dirname, '../public/registry-templates');
20+
21+
// Clean and create destination directory
22+
if (fs.existsSync(publicRegistryPath)) {
23+
fs.rmSync(publicRegistryPath, { recursive: true });
24+
}
25+
fs.mkdirSync(publicRegistryPath, { recursive: true });
26+
27+
// Copy all template files
28+
copyDirectory(registryPath, publicRegistryPath);
29+
30+
console.log('✅ Registry templates bundled successfully!');
31+
console.log(`📁 Templates available at: ${publicRegistryPath}`);
32+
33+
// Also create a manifest of all available templates
34+
const manifest = createTemplateManifest(publicRegistryPath);
35+
fs.writeFileSync(
36+
path.join(publicRegistryPath, 'manifest.json'),
37+
JSON.stringify(manifest, null, 2)
38+
);
39+
40+
console.log(`📋 Template manifest created with ${manifest.templates.length} templates`);
41+
42+
} catch (error) {
43+
console.error('❌ Failed to bundle registry templates:', error);
44+
process.exit(1);
45+
}
46+
}
47+
48+
function copyDirectory(src, dest) {
49+
if (!fs.existsSync(src)) {
50+
throw new Error(`Source directory does not exist: ${src}`);
51+
}
52+
53+
const entries = fs.readdirSync(src, { withFileTypes: true });
54+
55+
for (const entry of entries) {
56+
const srcPath = path.join(src, entry.name);
57+
const destPath = path.join(dest, entry.name);
58+
59+
if (entry.isDirectory()) {
60+
fs.mkdirSync(destPath, { recursive: true });
61+
copyDirectory(srcPath, destPath);
62+
} else {
63+
fs.copyFileSync(srcPath, destPath);
64+
}
65+
}
66+
}
67+
68+
function createTemplateManifest(templatesPath) {
69+
const templates = [];
70+
71+
function scanDirectory(dir, relativePath = '') {
72+
const entries = fs.readdirSync(dir, { withFileTypes: true });
73+
74+
for (const entry of entries) {
75+
const fullPath = path.join(dir, entry.name);
76+
const relPath = path.join(relativePath, entry.name);
77+
78+
if (entry.isDirectory()) {
79+
scanDirectory(fullPath, relPath);
80+
} else if (entry.name === '_meta.ts') {
81+
// Found a template directory
82+
const templatePath = relativePath || '.';
83+
templates.push({
84+
name: templatePath.replace(/\\/g, '/'), // normalize path separators
85+
path: templatePath,
86+
metaFile: relPath.replace(/\\/g, '/'),
87+
});
88+
}
89+
}
90+
}
91+
92+
scanDirectory(templatesPath);
93+
94+
return {
95+
generatedAt: new Date().toISOString(),
96+
templates,
97+
};
98+
}
99+
100+
bundleRegistryTemplates();
Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,52 @@
1-
import { handle } from "hono/vercel";
2-
import app from "./registry";
3-
import { getRegistryIndex } from "@proofkit/registry";
1+
import { NextRequest } from 'next/server';
2+
import {
3+
getRegistryIndex,
4+
getComponentMeta,
5+
getStaticComponentForShadcn
6+
} from '@/lib/registry-server';
47

5-
const handler = handle(app);
6-
export {
7-
handler as GET,
8-
};
9-
10-
export const dynamicParams = false
11-
12-
// Generate static params for all registry routes
13-
export async function generateStaticParams() {
8+
export async function GET(
9+
request: NextRequest,
10+
{ params }: { params: Promise<{ name?: string[] }> }
11+
) {
1412
try {
15-
const index = await getRegistryIndex();
13+
const { name = [] } = await params;
14+
const path = name.join('/');
15+
const url = new URL(request.url);
16+
17+
// Handle root registry request
18+
if (path === '') {
19+
const index = await getRegistryIndex();
20+
return Response.json(index);
21+
}
22+
23+
// Handle meta requests
24+
if (path.startsWith('meta/')) {
25+
const componentPath = path.replace('meta/', '');
26+
const meta = await getComponentMeta(componentPath);
27+
return Response.json(meta);
28+
}
29+
30+
// Handle component requests
31+
const routeNameRaw = url.searchParams.get('routeName');
32+
const routeName = routeNameRaw ? routeNameRaw.replace(/^\/+/, '') : undefined;
33+
34+
const component = await getStaticComponentForShadcn(path, { routeName });
1635

17-
const params = [
18-
// Root registry route
19-
{ name: [] },
20-
// Individual component routes
21-
...index.map((item) => ({
22-
name: item.name.split('/'),
23-
})),
24-
// Meta routes for each component
25-
...index.map((item) => ({
26-
name: ['meta', ...item.name.split('/')],
27-
})),
28-
];
36+
// Replace {proofkit} placeholders with the current origin
37+
const responseData = {
38+
...component,
39+
registryDependencies: component.registryDependencies?.map((dep: string) =>
40+
dep.replace('{proofkit}', url.origin)
41+
),
42+
};
2943

30-
console.log('Generated static params for registry:', params);
31-
return params;
44+
return Response.json(responseData);
3245
} catch (error) {
33-
console.error('Failed to generate static params for registry:', error);
34-
return [{ name: [] }];
46+
console.error('Registry route error:', error);
47+
return Response.json(
48+
{ error: 'Component not found.' },
49+
{ status: 404 }
50+
);
3551
}
3652
}

0 commit comments

Comments
 (0)