-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmiddleware.page.ts
44 lines (35 loc) · 1.54 KB
/
middleware.page.ts
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
// middleware.ts
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export type ConsentState = "accepted" | "rejected" | "undecided";
export function middleware(request: NextRequest) {
// Skip middleware for special paths
if (
request.nextUrl.pathname.startsWith("/_next") ||
request.nextUrl.pathname.startsWith("/api") ||
request.nextUrl.pathname.startsWith("/studio") ||
request.nextUrl.pathname.startsWith("/favicon.ico") ||
request.nextUrl.pathname.startsWith("/robots.txt") ||
request.nextUrl.pathname.startsWith("/sitemap.xml") ||
request.nextUrl.pathname.startsWith("/js/") ||
request.nextUrl.pathname.startsWith("/proxy/")
) {
return NextResponse.next();
}
const cookieConsent = request.cookies.get("gieffektivt-cookies-accepted")?.value;
// Determine consent state
let consentState: ConsentState = "undecided";
if (cookieConsent === "true") consentState = "accepted";
if (cookieConsent === "false") consentState = "rejected";
// Clone URL and modify path
const url = request.nextUrl.clone();
// Add .page to match your pageExtensions config
url.pathname = `/${consentState}${url.pathname}`;
const response = NextResponse.rewrite(url);
// Add ETag based on the cookie consent state
// This way, the cache will only be invalidated when the consent state changes
response.headers.set("ETag", `"consent-${consentState}"`);
// Allow caching but require revalidation
response.headers.set("Cache-Control", "private, must-revalidate");
return response;
}