Rotas no Next.js
Redirecionar /vault
src/app/vault/page.tsx:
import { redirect } from 'next/navigation';
export default function VaultPage() {
redirect('/vault/index.html');
}Servir arquivos estáticos
src/app/vault/[...slug]/route.ts:
import { NextRequest, NextResponse } from 'next/server';
import { readFile } from 'fs/promises';
import { join } from 'path';
export async function GET(
request: NextRequest,
{ params }: { params: { slug: string[] } }
) {
const slug = params.slug || [];
let filePath = join(process.cwd(), 'public', 'vault', ...slug);
try {
const content = await readFile(filePath);
const ext = filePath.split('.').pop();
const contentTypes: Record<string, string> = {
html: 'text/html',
css: 'text/css',
js: 'application/javascript',
json: 'application/json',
png: 'image/png',
jpg: 'image/jpeg',
svg: 'image/svg+xml',
};
return new NextResponse(content, {
headers: {
'Content-Type': contentTypes[ext || 'html'] || 'text/plain',
},
});
} catch {
try {
filePath = filePath.endsWith('.html') ? filePath : `${filePath}.html`;
const content = await readFile(filePath);
return new NextResponse(content, { headers: { 'Content-Type': 'text/html' } });
} catch {
const indexPath = join(filePath, 'index.html');
try {
await readFile(indexPath);
return NextResponse.redirect(new URL(`/vault/${slug.join('/')}/index.html`, request.url));
} catch {
return new NextResponse('Página não encontrada', { status: 404 });
}
}
}
}