fixed SSE Connection error and Rust error when compiling (last brace in /src/markdown.rs)
Some checks failed
Deploy / build-and-deploy (push) Failing after 2s

also updated the markdown post that you see upon entering.
This commit is contained in:
2025-06-29 15:56:30 +02:00
parent 6b5705680a
commit b0033a5671
3 changed files with 105 additions and 58 deletions

View File

@@ -8,50 +8,107 @@ export const runtime = 'nodejs';
// Store connected clients
const clients = new Set<ReadableStreamDefaultController>();
// Handle CORS preflight requests
export async function OPTIONS(request: NextRequest) {
return new NextResponse(null, {
status: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Headers': 'Cache-Control, Content-Type',
'Access-Control-Max-Age': '86400',
},
});
}
export async function GET(request: NextRequest) {
const stream = new ReadableStream({
start(controller) {
// Add this client to the set
clients.add(controller);
try {
const stream = new ReadableStream({
start(controller) {
// Add this client to the set
clients.add(controller);
// Send initial connection message
controller.enqueue(`data: ${JSON.stringify({ type: 'connected', message: 'SSE connection established' })}\n\n`);
// Send initial connection message
try {
controller.enqueue(`data: ${JSON.stringify({ type: 'connected', message: 'SSE connection established' })}\n\n`);
} catch (error) {
console.error('Error sending initial message:', error);
clients.delete(controller);
return;
}
// Set up file watcher if not already set up
if (clients.size === 1) {
watchPosts(() => {
// Notify all connected clients about the update
const message = JSON.stringify({ type: 'update', timestamp: new Date().toISOString() });
clients.forEach(client => {
try {
client.enqueue(`data: ${message}\n\n`);
} catch (error) {
// Set up file watcher if not already set up
if (clients.size === 1) {
try {
watchPosts(() => {
// Notify all connected clients about the update
const message = JSON.stringify({ type: 'update', timestamp: new Date().toISOString() });
const clientsToRemove: ReadableStreamDefaultController[] = [];
clients.forEach(client => {
try {
client.enqueue(`data: ${message}\n\n`);
} catch (error) {
// Mark client for removal
clientsToRemove.push(client);
}
});
// Remove disconnected clients
clients.delete(client);
}
});
});
}
clientsToRemove.forEach(client => {
clients.delete(client);
});
// Stop watching if no clients are connected
if (clients.size === 0) {
stopWatching();
}
});
} catch (error) {
console.error('Error setting up file watcher:', error);
}
}
// Clean up when client disconnects
request.signal.addEventListener('abort', () => {
clients.delete(controller);
// Stop watching if no clients are connected
// Clean up when client disconnects
request.signal.addEventListener('abort', () => {
clients.delete(controller);
// Stop watching if no clients are connected
if (clients.size === 0) {
stopWatching();
}
});
},
cancel() {
// Handle stream cancellation - we can't access the specific controller here
// The abort event handler will handle cleanup for the specific controller
if (clients.size === 0) {
stopWatching();
}
});
}
});
}
});
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'
}
});
return new NextResponse(stream, {
headers: {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Connection': 'keep-alive',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Cache-Control, Content-Type',
'X-Accel-Buffering': 'no', // Disable nginx buffering
},
});
} catch (error) {
console.error('SSE route error:', error);
return new NextResponse(
JSON.stringify({ error: 'Internal server error' }),
{
status: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
}
);
}
}