| 10 | const isProd = process.env.NODE_ENV === 'production'; |
| 11 | |
| 12 | export default async function middleware(request: NextRequest) { |
| 13 | const url = request.nextUrl.clone(); // clone the request url |
| 14 | const userAgent = request.headers.has('user-agent') |
| 15 | ? request.headers.get('user-agent') |
| 16 | : ''; |
| 17 | |
| 18 | if (await blocked(request)) { |
| 19 | url.pathname = `/500`; |
| 20 | return NextResponse.rewrite(url.toString()); |
| 21 | } |
| 22 | if (isBadBot(userAgent)) { |
| 23 | const host = isProd ? 'www.linen.dev' : url.hostname; |
| 24 | const protocol = isProd ? 'https' : 'http'; |
| 25 | return NextResponse.redirect(`${protocol}://${host}/bot`); |
| 26 | } |
| 27 | |
| 28 | const { pathname } = request.nextUrl; // get pathname of request (e.g. /blog-slug) |
| 29 | const hostname = request.headers.get('host'); // get hostname of request (e.g. demo.vercel.pub) |
| 30 | |
| 31 | // Testing for a custom landing page |
| 32 | if (hostname?.includes('linen.community')) { |
| 33 | url.pathname = `/private-community`; |
| 34 | return NextResponse.rewrite(url.toString()); |
| 35 | } |
| 36 | |
| 37 | const res = rewrite({ |
| 38 | hostname, |
| 39 | pathname, |
| 40 | url, |
| 41 | userAgent, |
| 42 | }); |
| 43 | |
| 44 | if (res && !!res.rewrite) { |
| 45 | return NextResponse.rewrite(res.rewrite); |
| 46 | } |
| 47 | } |