Major changes
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const baseUrl = request.nextUrl.origin;
|
||||
|
||||
try {
|
||||
const response = await fetch(
|
||||
'https://en.wikipedia.org/api/rest_v1/page/random/summary',
|
||||
{
|
||||
headers: {
|
||||
'User-Agent': 'WikiPlus/1.0',
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch from Wikipedia API');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
// Convert title to URL-friendly format (replace spaces with underscores)
|
||||
const urlTitle = data.title.replace(/ /g, '_');
|
||||
|
||||
return NextResponse.redirect(
|
||||
new URL(`/summary/${encodeURIComponent(urlTitle)}`, baseUrl)
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Error fetching random article:', error);
|
||||
return NextResponse.redirect(
|
||||
new URL('/?error=failed', baseUrl)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user