Production ready
This commit is contained in:
57
frontend/middleware.ts
Normal file
57
frontend/middleware.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
|
||||
const LANDING_ALLOWED_PATHS = ['/', '/blog', '/privacy', '/admin', '/use-cases', '/features']
|
||||
const NEXT_METADATA_PATHS = [
|
||||
'/favicon.ico',
|
||||
'/icon',
|
||||
'/apple-icon',
|
||||
'/opengraph-image',
|
||||
'/twitter-image',
|
||||
]
|
||||
|
||||
function isAllowedLandingPath(pathname: string): boolean {
|
||||
if (pathname === '/') return true
|
||||
|
||||
return LANDING_ALLOWED_PATHS
|
||||
.filter((path) => path !== '/')
|
||||
.some((path) => pathname === path || pathname.startsWith(`${path}/`))
|
||||
}
|
||||
|
||||
function isStaticOrMetadataPath(pathname: string): boolean {
|
||||
// Never treat API routes as static paths
|
||||
if (pathname.startsWith('/api')) return false
|
||||
|
||||
if (pathname.startsWith('/_next')) return true
|
||||
|
||||
if (/\.[^/]+$/.test(pathname)) return true
|
||||
|
||||
return NEXT_METADATA_PATHS.some(
|
||||
(path) => pathname === path || pathname.startsWith(`${path}/`)
|
||||
)
|
||||
}
|
||||
|
||||
export function middleware(request: NextRequest) {
|
||||
if (process.env.NEXT_PUBLIC_LANDING_ONLY_MODE !== 'true') {
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
||||
const { pathname } = request.nextUrl
|
||||
|
||||
if (isStaticOrMetadataPath(pathname)) {
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
||||
if (isAllowedLandingPath(pathname)) {
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
||||
const redirectUrl = request.nextUrl.clone()
|
||||
redirectUrl.pathname = '/'
|
||||
redirectUrl.search = ''
|
||||
|
||||
return NextResponse.redirect(redirectUrl, 307)
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: '/:path*',
|
||||
}
|
||||
Reference in New Issue
Block a user