-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathproxy.ts
More file actions
47 lines (39 loc) · 1.59 KB
/
Copy pathproxy.ts
File metadata and controls
47 lines (39 loc) · 1.59 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
import { type NextRequest, NextResponse } from "next/server";
import createIntlMiddleware from "next-intl/middleware";
import { routing } from "./i18n/routing";
import { buildCsp, buildSecurityHeaders } from "@/lib/security-headers";
const intlMiddleware = createIntlMiddleware(routing);
export function proxy(request: NextRequest) {
const nonce = crypto.randomUUID();
const isDev = process.env.NODE_ENV === "development";
const csp = buildCsp(nonce, isDev);
let intlResponse: ReturnType<typeof intlMiddleware> | null = null;
try {
intlResponse = intlMiddleware(request);
} catch {
intlResponse = null;
}
const response = intlResponse ?? NextResponse.next();
// Propagate x-nonce AND the CSP onto the request headers via the middleware
// override mechanism. Next reads `content-security-policy` off the request to
// nonce its own inline framework scripts (self.__next_f); without this an
// enforcing script-src 'nonce-…' would block those scripts and break hydration.
const existing = response.headers.get("x-middleware-override-headers");
response.headers.set(
"x-middleware-override-headers",
existing
? `${existing},x-nonce,content-security-policy`
: "x-nonce,content-security-policy"
);
response.headers.set("x-middleware-request-x-nonce", nonce);
response.headers.set("x-middleware-request-content-security-policy", csp);
for (const [name, value] of Object.entries(
buildSecurityHeaders(nonce, isDev)
)) {
response.headers.set(name, value);
}
return response;
}
export const config = {
matcher: ["/((?!api|_next|.*\\..*).*)"],
};