@@ -8,80 +8,81 @@ export async function GET(request: NextRequest, { params }: { params: { path: st
88 const token = searchParams . get ( "token" ) || request . headers . get ( "Authorization" ) ?. split ( " " ) [ 1 ] ;
99
1010 // Check if this is an SSE endpoint
11- const isSSE = path === "timeline" || path === "notifications" || path . includes ( "/comments" ) ;
12-
13- if ( isSSE ) {
14- try {
15- // Try SSE first
16- const response = await fetch ( `${ process . env . API_URL } /api/${ path } ` , {
17- headers : {
18- Authorization : `Bearer ${ token } ` ,
19- Accept : "text/event-stream" ,
20- } ,
21- } ) ;
22-
23- const text = await response . text ( ) ;
24-
25- if ( response . ok && text . trim ( ) ) {
26- try {
27- const data = JSON . parse ( text ) ;
28-
29- // Set up SSE response
30- const encoder = new TextEncoder ( ) ;
31- const stream = new ReadableStream ( {
32- async start ( controller ) {
33- controller . enqueue ( encoder . encode ( `data: ${ JSON . stringify ( data ) } \n\n` ) ) ;
34-
35- // Keep the connection open
36- const interval = setInterval ( ( ) => {
37- controller . enqueue ( encoder . encode ( ": keepalive\n\n" ) ) ;
38- } , 30000 ) ;
39-
40- // Clean up on close
41- request . signal . addEventListener ( "abort" , ( ) => {
42- clearInterval ( interval ) ;
43- controller . close ( ) ;
44- } ) ;
45- } ,
46- } ) ;
47-
48- return new NextResponse ( stream , {
49- headers : {
50- "Content-Type" : "text/event-stream" ,
51- "Cache-Control" : "no-cache" ,
52- Connection : "keep-alive" ,
53- } ,
54- } ) ;
55- } catch ( jsonError ) {
56- console . error ( "SSE JSON parse error:" , jsonError ) ;
57- return NextResponse . json ( { error : "Invalid JSON response from SSE" } , { status : 502 } ) ;
58- }
59- } else {
60- console . log ( "SSE response empty, falling back to basic fetch..." ) ;
61-
62- const fallbackResponse = await fetch ( `${ process . env . API_URL } /api/${ path } ` , {
63- headers : { Authorization : `Bearer ${ token } ` } ,
64- } ) ;
65-
66- const fallbackText = await fallbackResponse . text ( ) ;
67-
68- if ( ! fallbackText . trim ( ) ) {
69- return NextResponse . json ( { error : "Empty response from server" } , { status : 502 } ) ;
70- }
71-
72- try {
73- return NextResponse . json ( JSON . parse ( fallbackText ) , { status : fallbackResponse . status } ) ;
74- } catch {
75- return NextResponse . json ( { error : "Invalid JSON from fallback" } , { status : 502 } ) ;
76- }
77- }
78- } catch ( error : any ) {
79- console . error ( "SSE error:" , error ) ;
80- if ( error . code !== "UND_ERR_HEADERS_TIMEOUT" ) {
81- return NextResponse . json ( { error : "Failed to connect to SSE" } , { status : 500 } ) ;
82- }
83- }
84- }
11+ // const isSSE = path === "timeline" || path === "notifications" ||path.includes("/comments");
12+
13+ // if (isSSE) {
14+ // try {
15+ // // Try SSE first
16+ // const response = await fetch(`${process.env.API_URL}/api/${path}`, {
17+ // headers: {
18+ // Authorization: `Bearer ${token}`,
19+ // Accept: "text/event-stream",
20+ // },
21+ // });
22+ // console.log(response)
23+
24+ // const text = await response.text();
25+
26+ // if (response.ok && !text.trim()) {
27+ // try {
28+ // const data = JSON.parse(text);
29+
30+ // // Set up SSE response
31+ // const encoder = new TextEncoder();
32+ // const stream = new ReadableStream({
33+ // async start(controller) {
34+ // controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}\n\n`));
35+
36+ // // Keep the connection open
37+ // const interval = setInterval(() => {
38+ // controller.enqueue(encoder.encode(": keepalive\n\n"));
39+ // }, 30000);
40+
41+ // // Clean up on close
42+ // request.signal.addEventListener("abort", () => {
43+ // clearInterval(interval);
44+ // controller.close();
45+ // });
46+ // },
47+ // });
48+
49+ // return new NextResponse(stream, {
50+ // headers: {
51+ // "Content-Type": "text/event-stream",
52+ // "Cache-Control": "no-cache",
53+ // Connection: "keep-alive",
54+ // },
55+ // });
56+ // } catch (jsonError) {
57+ // console.error("SSE JSON parse error:", jsonError);
58+ // return NextResponse.json({ error: "Invalid JSON response from SSE" }, { status: 502 });
59+ // }
60+ // } else {
61+ // console.log("SSE response empty, falling back to basic fetch...");
62+
63+ // const fallbackResponse = await fetch(`${process.env.API_URL}/api/${path}`, {
64+ // headers: { Authorization: `Bearer ${token}` },
65+ // });
66+
67+ // const fallbackText = await fallbackResponse.text();
68+
69+ // if (!fallbackText.trim()) {
70+ // return NextResponse.json({ error: "Empty response from server" }, { status: 502 });
71+ // }
72+
73+ // try {
74+ // return NextResponse.json(JSON.parse(fallbackText), { status: fallbackResponse.status });
75+ // } catch {
76+ // return NextResponse.json({ error: "Invalid JSON from fallback" }, { status: 502 });
77+ // }
78+ // }
79+ // } catch (error: any) {
80+ // console.error("SSE error:", error);
81+ // if (error.code !== "UND_ERR_HEADERS_TIMEOUT") {
82+ // return NextResponse.json({ error: "Failed to connect to SSE" }, { status: 500 });
83+ // }
84+ // }
85+ // }
8586
8687 // Regular API request
8788 try {
0 commit comments