import { NextRequest, NextResponse } from 'next/server'
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url)
const targetUrl = searchParams.get('url')
if (!targetUrl) {
return NextResponse.json({ error: 'URL parameter required' }, { status: 400 })
}
try {
// Validate URL
const url = new URL(targetUrl)
// Only allow http/https
if (!['http:', 'https:'].includes(url.protocol)) {
return NextResponse.json({ error: 'Invalid URL protocol' }, { status: 400 })
}
// Fetch the page
const response = await fetch(targetUrl, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
},
})
if (!response.ok) {
return NextResponse.json(
{ error: `Failed to fetch: ${response.status}` },
{ status: response.status }
)
}
let html = await response.text()
// Inject base tag to fix relative URLs
const baseTag = `