Repository navigation
Expand file tree
/
Copy pathmiddleware.ts
More file actions
184 lines (160 loc) · 5.97 KB
/
Copy pathmiddleware.ts
File metadata and controls
184 lines (160 loc) · 5.97 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { NextRequest, NextResponse } from "next/server";
// Keep in sync with i18n/locales.ts
const SUPPORTED_LOCALES = ["en", "en-NG", "en-KE", "ar", "ru", "pl"] as const;
type Locale = (typeof SUPPORTED_LOCALES)[number];
const DEFAULT_LOCALE: Locale = "en";
// Cookie name used by next-intl and set by setLocale() in lib/i18n/index.ts
const LOCALE_COOKIE = "NEXT_LOCALE";
/**
* Resolve the preferred locale for a request.
*
* Priority order:
* 1. NEXT_LOCALE cookie (set when user explicitly switches locale)
* 2. Accept-Language header negotiation
* 3. Default locale ('en')
*/
function resolveLocale(request: NextRequest): Locale {
// 1. Explicit cookie
const cookieLocale = request.cookies.get(LOCALE_COOKIE)?.value;
if (cookieLocale && SUPPORTED_LOCALES.includes(cookieLocale as Locale)) {
return cookieLocale as Locale;
}
// 2. Accept-Language header — try each tag in quality order
const acceptLang = request.headers.get("Accept-Language") ?? "";
for (const part of acceptLang.split(",")) {
const tag = part.split(";")[0].trim();
// Exact match first (e.g. "en-NG")
if (SUPPORTED_LOCALES.includes(tag as Locale)) {
return tag as Locale;
}
// Language-only prefix match (e.g. "ar" matches "ar")
const lang = tag.split("-")[0];
const match = SUPPORTED_LOCALES.find(
(l) => l === lang || l.startsWith(`${lang}-`),
);
if (match) return match;
}
// 3. Fallback
return DEFAULT_LOCALE;
}
/**
* Returns true if the pathname starts with one of the supported locale
* prefixes, e.g. /en/..., /en-NG/..., etc.
*/
function hasLocalePrefix(pathname: string): boolean {
return SUPPORTED_LOCALES.some(
(locale) => pathname === `/${locale}` || pathname.startsWith(`/${locale}/`),
);
}
// Paths that should NOT be redirected to a locale-prefixed URL.
// These are either Next.js internals, API routes, or static assets that the
// matcher config already excludes — but we double-check here for safety.
const SKIP_REDIRECT_PREFIXES = [
"/api/",
"/_next/",
"/favicon",
"/robots",
"/sitemap",
"/manifest",
];
function shouldSkipLocaleRedirect(pathname: string): boolean {
return SKIP_REDIRECT_PREFIXES.some((prefix) => pathname.startsWith(prefix));
}
export function middleware(request: NextRequest) {
// Block .md files
if (request.nextUrl.pathname.endsWith(".md")) {
return new NextResponse(null, { status: 404 });
}
const { pathname } = request.nextUrl;
// ── Locale redirect ────────────────────────────────────────────────────
// If the request does not already carry a locale prefix (e.g. /en/mint)
// and is not an internal/API path, resolve the preferred locale and
// redirect so that every page is served under a locale-prefixed URL.
// This ensures the NEXT_LOCALE cookie is honoured on ALL routes, not just
// the [locale] segment pages.
if (!hasLocalePrefix(pathname) && !shouldSkipLocaleRedirect(pathname)) {
const locale = resolveLocale(request);
// Build the redirect URL preserving query string and hash
const redirectUrl = request.nextUrl.clone();
redirectUrl.pathname = `/${locale}${pathname === "/" ? "" : pathname}`;
const redirectResponse = NextResponse.redirect(redirectUrl);
// Persist the resolved locale as a cookie so subsequent requests (and
// the top-level pages that read it) stay consistent.
redirectResponse.cookies.set(LOCALE_COOKIE, locale, {
path: "/",
sameSite: "lax",
// Don't set httpOnly so the client-side setLocale() can update it too
httpOnly: false,
// 1 year TTL
maxAge: 60 * 60 * 24 * 365,
});
return redirectResponse;
}
// ── CSP / Security headers ─────────────────────────────────────────────
const nonce = Buffer.from(crypto.randomUUID()).toString("base64");
const isDev = process.env.NODE_ENV === "development";
const cspDirectives = {
"default-src": ["'self'"],
"script-src": [
"'self'",
`'nonce-${nonce}'`,
"'strict-dynamic'",
isDev ? "'unsafe-eval'" : "",
].filter(Boolean),
"style-src": ["'self'", `'nonce-${nonce}'`, "'unsafe-inline'"],
"img-src": ["'self'", "blob:", "data:", "https://*"],
"font-src": ["'self'"],
"manifest-src": ["'self'"],
"object-src": ["'none'"],
"base-uri": ["'self'"],
"form-action": ["'self'"],
"frame-ancestors": ["'none'"],
"report-uri": ["/api/csp-report"],
"connect-src": [
"'self'",
"https://*.stellar.org",
"https://*.soroban-rpc.com",
"https://*.vercel-analytics.com",
isDev ? "ws://localhost:*" : "",
].filter(Boolean),
"upgrade-insecure-requests": [],
};
const cspString = Object.entries(cspDirectives)
.map(([key, values]) => {
if (values.length === 0) return key;
return `${key} ${values.join(" ")}`;
})
.join("; ");
const requestHeaders = new Headers(request.headers);
requestHeaders.set("x-nonce", nonce);
const response = NextResponse.next({
request: { headers: requestHeaders },
});
response.headers.set("Content-Security-Policy", cspString);
response.headers.set("X-Content-Type-Options", "nosniff");
response.headers.set("X-Frame-Options", "DENY");
response.headers.set("Referrer-Policy", "strict-origin-when-cross-origin");
response.headers.set(
"Permissions-Policy",
"camera=(), microphone=(), geolocation=()",
);
if (!isDev) {
response.headers.set(
"Strict-Transport-Security",
"max-age=31536000; includeSubDomains; preload",
);
}
return response;
}
export const config = {
matcher: [
{
source:
"/((?!api|_next/static|_next/image|.*\\.(?:ico|png|jpg|jpeg|svg|webp|gif|woff2?|ttf|otf|map)).*)",
missing: [
{ type: "header", key: "next-router-prefetch" },
{ type: "header", key: "purpose", value: "prefetch" },
],
},
],
};