-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmiddleware.ts
More file actions
53 lines (45 loc) · 1.46 KB
/
middleware.ts
File metadata and controls
53 lines (45 loc) · 1.46 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { LINKS } from "@feat/navigation/Links";
import { DEFAULT_LOCALE } from "@i18n/config";
import { env } from "@lib/env/server";
import { routing } from "i18n/routing";
import createMiddleware from "next-intl/middleware";
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
const maintenanceEnd = env.MAINTENANCE_END && new Date(env.MAINTENANCE_END);
export default function middleware(request: NextRequest) {
const excludeMaintenance = [
"/api",
"/_next/static",
"/_next/image",
"/favicon",
"/apple-icon",
"/icon",
"/sitemap.xml",
"/manifest",
"/robots.txt",
"/assets",
LINKS.Maintenance.href(),
];
const { pathname } = request.nextUrl;
const isExcluded = excludeMaintenance.some((route) =>
pathname.startsWith(route),
);
const now = new Date();
// Redirect to maintenance page
if (!isExcluded && maintenanceEnd && now < maintenanceEnd) {
const url = request.nextUrl.clone();
const localeMatch = url.pathname.match(/^\/([a-zA-Z]{2})(\/|$)/);
const localePrefix = localeMatch
? `/${localeMatch[1]}`
: `/${DEFAULT_LOCALE}`;
url.pathname = `${localePrefix}${LINKS.Maintenance.href()}`;
return NextResponse.rewrite(url);
}
return createMiddleware(routing)(request);
}
export const config = {
matcher: [
"/((?!api|_next/static|_next/image|favicon.ico|apple-icon.png|icon|sitemap.xml|manifest|robots.txt|assets).*)",
],
runtime: "nodejs",
};