added suspend
This commit is contained in:
52
proxy.ts
52
proxy.ts
@@ -1,56 +1,26 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import type { NextRequest } from 'next/server'
|
||||
|
||||
export default async function proxy(request: NextRequest) {
|
||||
const { pathname } = request.nextUrl
|
||||
export function proxy(request: NextRequest) {
|
||||
const isSuspended = process.env.SITE_SUSPENDED === 'true'
|
||||
|
||||
// Skip middleware for admin routes, API routes, and static files
|
||||
if (pathname.startsWith('/admin') ||
|
||||
pathname.startsWith('/api') ||
|
||||
pathname.startsWith('/_next') ||
|
||||
pathname === '/favicon.ico') {
|
||||
// Fast path: do nothing if the site is not suspended.
|
||||
if (!isSuspended) {
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
||||
// Check if maintenance mode is enabled via API
|
||||
try {
|
||||
const maintenanceResponse = await fetch(new URL('/api/maintenance-check', request.url), {
|
||||
headers: {
|
||||
// Forward cookies to maintain session for admin check
|
||||
cookie: request.headers.get('cookie') || ''
|
||||
}
|
||||
})
|
||||
|
||||
if (maintenanceResponse.ok) {
|
||||
const { maintenanceMode } = await maintenanceResponse.json()
|
||||
|
||||
// If maintenance mode is ON and user is not on maintenance page
|
||||
if (maintenanceMode && pathname !== '/maintenance') {
|
||||
return NextResponse.redirect(new URL('/maintenance', request.url))
|
||||
}
|
||||
|
||||
// If maintenance mode is OFF and user is on maintenance page
|
||||
if (!maintenanceMode && pathname === '/maintenance') {
|
||||
return NextResponse.redirect(new URL('/', request.url))
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Middleware error:', error)
|
||||
// If maintenance check fails, allow the request to continue
|
||||
const { pathname } = request.nextUrl
|
||||
|
||||
// Allow access to the suspended page itself.
|
||||
if (pathname === '/suspended') {
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
||||
return NextResponse.next()
|
||||
return NextResponse.redirect(new URL('/suspended', request.url))
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: [
|
||||
/*
|
||||
* Match all request paths except for the ones starting with:
|
||||
* - api (API routes)
|
||||
* - _next/static (static files)
|
||||
* - _next/image (image optimization files)
|
||||
* - favicon.ico (favicon file)
|
||||
*/
|
||||
'/((?!api|_next/static|_next/image|favicon.ico).*)',
|
||||
'/((?!_next/static|_next/image|favicon.ico|suspended).*)',
|
||||
],
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user