Spaces:
Running
Running
File size: 2,003 Bytes
c10f8f8 0b02673 c10f8f8 0b02673 c10f8f8 0b02673 c10f8f8 0b02673 c10f8f8 0b02673 c10f8f8 0b02673 c10f8f8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import { NextRequest, NextResponse } from "next/server";
// Timeout configuration (in milliseconds)
const FETCH_TIMEOUT = 30000; // 30 seconds for external fetch
// Extend the maximum execution time for this route
export const maxDuration = 60; // 1 minute
export async function PUT(request: NextRequest) {
const body = await request.json();
const { url } = body;
if (!url) {
return NextResponse.json({ error: "URL is required" }, { status: 400 });
}
try {
// Create an AbortController for timeout
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), FETCH_TIMEOUT);
try {
const response = await fetch(
`https://r.jina.ai/${encodeURIComponent(url)}`,
{
method: "POST",
signal: controller.signal,
}
);
clearTimeout(timeoutId);
if (!response.ok) {
return NextResponse.json(
{ error: "Failed to fetch redesign" },
{ status: 500 }
);
}
const markdown = await response.text();
return NextResponse.json(
{
ok: true,
markdown,
},
{ status: 200 }
);
} catch (fetchError: any) {
clearTimeout(timeoutId);
if (fetchError.name === 'AbortError') {
return NextResponse.json(
{ error: "Request timeout: The external service took too long to respond. Please try again." },
{ status: 504 }
);
}
throw fetchError;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
if (error.name === 'AbortError' || error.message?.includes('timeout')) {
return NextResponse.json(
{ error: "Request timeout: The external service took too long to respond. Please try again." },
{ status: 504 }
);
}
return NextResponse.json(
{ error: error.message || "An error occurred" },
{ status: 500 }
);
}
}
|