From b0033a5671bd8dc5b841a5e38586c2b27083536c Mon Sep 17 00:00:00 2001 From: rattatwinko Date: Sun, 29 Jun 2025 15:56:30 +0200 Subject: [PATCH] fixed SSE Connection error and Rust error when compiling (last brace in /src/markdown.rs) also updated the markdown post that you see upon entering. --- markdown_backend/src/markdown.rs | 6 +- posts/welcome.md | 26 +++--- src/app/api/posts/stream/route.ts | 131 +++++++++++++++++++++--------- 3 files changed, 105 insertions(+), 58 deletions(-) diff --git a/markdown_backend/src/markdown.rs b/markdown_backend/src/markdown.rs index 9dab07c..3cc0d2d 100644 --- a/markdown_backend/src/markdown.rs +++ b/markdown_backend/src/markdown.rs @@ -673,8 +673,4 @@ pub fn checkhealth() -> HealthReport { cache_stats_count, errors, } -} - cache_stats_count, - errors, - } -} \ No newline at end of file +} diff --git a/posts/welcome.md b/posts/welcome.md index f41009c..7aa12a0 100644 --- a/posts/welcome.md +++ b/posts/welcome.md @@ -434,7 +434,6 @@ Key API endpoints include: All API routes are implemented using Next.js API routes and are available out of the box. For more details, check the code in the `src/app/api/posts/` directory. --- ## Train of Thought for this Project and Todo @@ -446,29 +445,24 @@ If you have seen this is not very mindfull of browser resources tho. |-------|----| |Done|**Rust Parser for Markdown**| |LTS|_Long Term Support and upkeep_| -|Not Done|Full Inline _CSS_ Support for **HTML**| +|Partially Done|**Caching with Rust**| +|Done|Full Inline _CSS_ Support for **HTML**| --- -## Recent Changes +## Recent Changes cute lil guy +

Ferris the Cutie


-If you have noticed the Project has switched to a more reliable and _faster_ **Rust** Markdown-Parser! -If you are wondering: +### Caching: +Recently the _Rust Parser recived a update_ which introduced **Caching using RAM**. This is not yet _fully implemented_, but it is in the works. -#### Why? - -- To make **loading times** / _compile times_ (For the **HTML of the Pages** || _slugification_) of posts quicker. - -#### What changed? - -- The **RustMarkdownParser** (_RMP_) only really changed the **Architecture** of the _Parsing for Markdown Files_. If you are wondering how _fast_ the Parser is then have a look at the **Admin-Panel**, which also _changed quite a lot_. - -#### Do I need to do anything? - -- If you havent already, then **Update** the Blog! Blogging stays the same. No difference. Just _🗲 faster 🗲_. +### Recursive Folder-Scanning via Rust: +The Rust Parser was **not implemented fully** _(28.JUN.2025)_. **BUT**, I can proudly state that since _29.JUN.25_ **The Rust Parser is parsing** with ease and with _BLAZING SPEEDS_ +### Fixed SSE CORS Connection: +Since Ages the **SSE Streaming _was_ broken**, now its fixed (see _@src/app/api/posts/stream/route.ts_). This works fine, needs some work still.
diff --git a/src/app/api/posts/stream/route.ts b/src/app/api/posts/stream/route.ts index c0638d1..af6a0a0 100644 --- a/src/app/api/posts/stream/route.ts +++ b/src/app/api/posts/stream/route.ts @@ -8,50 +8,107 @@ export const runtime = 'nodejs'; // Store connected clients const clients = new Set(); +// 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': '*', + }, + } + ); + } } \ No newline at end of file