Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved version #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/app/api/scrape/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@ export const runtime = "edge";

async function scrapeText(url: string): Promise<string> {
try {
const response = await fetch(url);
const controller = new AbortController(); // Create an AbortController
const timeoutId = setTimeout(() => controller.abort(), 1000); // Set timeout

const response = await fetch(url, { signal: controller.signal }); // Pass the signal

clearTimeout(timeoutId); // Clear timeout if successful

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const html = await response.text();
const text = extractBodyText(html);
return text;
} catch (error) {
console.error(`Error fetching URL ${url}:`, error);
if (error.name === 'AbortError') {
console.error(`Timeout for URL ${url}`); // Handle timeout
} else {
console.error(`Error fetching URL ${url}:`, error);
}
return "";
}
}
Expand Down
Loading