Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9de3931e33 |
@@ -10,10 +10,38 @@ const intlMiddleware = createMiddleware({
|
|||||||
defaultLocale: 'en',
|
defaultLocale: 'en',
|
||||||
});
|
});
|
||||||
|
|
||||||
export default function middleware(request: NextRequest) {
|
const imgproxyStatus = { isDown: false, lastCheck: 0 };
|
||||||
|
|
||||||
|
async function isImgproxyDown() {
|
||||||
|
const now = Date.now();
|
||||||
|
if (now - imgproxyStatus.lastCheck > 60000) {
|
||||||
|
try {
|
||||||
|
const imgproxyUrl = process.env.IMGPROXY_URL || 'https://img.infra.mintel.me';
|
||||||
|
const checkUrl = imgproxyUrl.startsWith('http') ? imgproxyUrl : `https://${imgproxyUrl}`;
|
||||||
|
const res = await fetch(checkUrl, { signal: AbortSignal.timeout(2000) });
|
||||||
|
imgproxyStatus.isDown = res.status >= 500;
|
||||||
|
} catch (e) {
|
||||||
|
imgproxyStatus.isDown = true;
|
||||||
|
}
|
||||||
|
imgproxyStatus.lastCheck = now;
|
||||||
|
}
|
||||||
|
return imgproxyStatus.isDown;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default async function middleware(request: NextRequest) {
|
||||||
const { method, url, headers } = request;
|
const { method, url, headers } = request;
|
||||||
const { pathname } = request.nextUrl;
|
const { pathname } = request.nextUrl;
|
||||||
|
|
||||||
|
if (pathname.startsWith('/_img/')) {
|
||||||
|
if (await isImgproxyDown()) {
|
||||||
|
const originalUrl = request.nextUrl.searchParams.get('url');
|
||||||
|
if (originalUrl) {
|
||||||
|
return NextResponse.redirect(originalUrl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NextResponse.next();
|
||||||
|
}
|
||||||
|
|
||||||
// Explicit bypass for infrastructure routes to avoid locale redirects/interception
|
// Explicit bypass for infrastructure routes to avoid locale redirects/interception
|
||||||
if (
|
if (
|
||||||
pathname.startsWith('/stats') ||
|
pathname.startsWith('/stats') ||
|
||||||
@@ -97,7 +125,7 @@ export default function middleware(request: NextRequest) {
|
|||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
matcher: [
|
matcher: [
|
||||||
'/((?!api|_next/static|_next/image|_img|favicon.ico|manifest.webmanifest|.*\\.(?:svg|png|jpg|jpeg|gif|webp|pdf|txt|vcf|xml|webm|mp4|map)$).*)',
|
'/((?!api|_next/static|_next/image|favicon.ico|manifest.webmanifest|.*\\.(?:svg|png|jpg|jpeg|gif|webp|pdf|txt|vcf|xml|webm|mp4|map)$).*)',
|
||||||
'/(de|en)/:path*',
|
'/(de|en)/:path*',
|
||||||
'/(de|en)/:path*',
|
'/(de|en)/:path*',
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user