Implement Server-Sent Events for real-time updates and enhance API route handling. Added loading state and last update indicator in the home page. Improved folder and post detail fetching logic in the admin page. Added webhook notification on file changes.
Some checks failed
Deploy / build-and-deploy (push) Failing after 2s

This commit is contained in:
ZockerKatze
2025-06-24 07:23:34 +02:00
parent da5fbfa687
commit 7e2ada529d
7 changed files with 339 additions and 26 deletions

View File

@@ -0,0 +1,62 @@
import { NextRequest, NextResponse } from 'next/server';
// Prevent static generation of this route
export const dynamic = 'force-dynamic';
export const runtime = 'nodejs';
// Store connected clients for webhook notifications
const webhookClients = new Set<{ id: string; controller: ReadableStreamDefaultController }>();
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const clientId = searchParams.get('clientId') || Math.random().toString(36).substr(2, 9);
const stream = new ReadableStream({
start(controller) {
// Add this client to the set
webhookClients.add({ id: clientId, controller });
// Send initial connection message
controller.enqueue(`data: ${JSON.stringify({ type: 'connected', clientId, message: 'Webhook connection established' })}\n\n`);
// Clean up when client disconnects
request.signal.addEventListener('abort', () => {
webhookClients.delete({ id: clientId, controller });
});
}
});
return new NextResponse(stream, {
headers: {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Cache-Control'
}
});
}
// Webhook endpoint that can be called when files change
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { type = 'update', slug } = body;
// Notify all connected clients
const message = JSON.stringify({ type, slug, timestamp: new Date().toISOString() });
webhookClients.forEach(({ controller }) => {
try {
controller.enqueue(`data: ${message}\n\n`);
} catch (error) {
// Remove disconnected clients
webhookClients.delete({ id: '', controller });
}
});
return NextResponse.json({ success: true, clientsNotified: webhookClients.size });
} catch (error) {
console.error('Webhook error:', error);
return NextResponse.json({ error: 'Invalid webhook payload' }, { status: 400 });
}
}