-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmiddleware.js
More file actions
37 lines (29 loc) · 1021 Bytes
/
middleware.js
File metadata and controls
37 lines (29 loc) · 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { NextResponse } from 'next/server';
export function middleware(request) {
const isDown = process.env.IS_DOWN === 'true';
if (!isDown) {
return NextResponse.next();
}
const { pathname } = request.nextUrl;
// Allow maintenance page and essential/static resources to be served
const isMaintenance = pathname.startsWith('/maintenance');
const isApi = pathname.startsWith('/api');
const isNext = pathname.startsWith('/_next');
const isStaticAsset =
pathname.startsWith('/favicon.ico') ||
pathname.startsWith('/robots.txt') ||
pathname.startsWith('/sitemap.xml') ||
pathname.startsWith('/manifest.json') ||
pathname.startsWith('/android-chrome-') ||
pathname.startsWith('/public');
if (isMaintenance || isApi || isNext || isStaticAsset) {
return NextResponse.next();
}
const url = request.nextUrl.clone();
url.pathname = '/maintenance';
url.search = '';
return NextResponse.redirect(url);
}
export const config = {
matcher: ['/((?!_next).*)'],
};