Repository navigation
Expand file tree
/
Copy pathproxy.ts
More file actions
124 lines (107 loc) · 3.94 KB
/
Copy pathproxy.ts
File metadata and controls
124 lines (107 loc) · 3.94 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
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
const ALLOWED_ORIGINS = [
'https://rummerlab.com',
'https://jodierummer.com',
'https://physioshark.org',
'http://localhost:3000',
];
const CACHE_CONTROL_ASSETS = 'public, max-age=31536000, stale-while-revalidate=86400, immutable';
function getOriginFromReferer(referer: string | null): string | null {
if (!referer) return null;
try {
const url = new URL(referer);
return url.origin;
} catch {
return null;
}
}
function isAllowedOrigin(origin: string | null): boolean {
if (!origin) return false;
return ALLOWED_ORIGINS.includes(origin);
}
function getCorsHeaders(origin: string | null): Record<string, string> {
if (!origin || !isAllowedOrigin(origin)) return {};
return {
'Access-Control-Allow-Origin': origin,
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, Accept',
};
}
const CORS_HEADERS_ALLOW_ALL: Record<string, string> = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, Accept',
};
const CACHE_CONTROL_API = 'public, max-age=86400, stale-while-revalidate=3600';
function computeWeakEtag(pathname: string, search: string): string {
const value = pathname + search;
const encoded = btoa(encodeURIComponent(value));
return `W/"${encoded}"`;
}
export function proxy(request: NextRequest) {
const { pathname, search } = request.nextUrl;
const origin = request.headers.get('Origin') ?? getOriginFromReferer(request.headers.get('Referer'));
const isNextImage = pathname === '/_next/image';
const isStaticImage = pathname.startsWith('/images/');
const isAssetRoute = isStaticImage || isNextImage;
const isApiRoute = pathname.startsWith('/api/');
// OPTIONS preflight: API allows all origins; assets use allowlist
if (request.method === 'OPTIONS') {
const cors = isApiRoute
? CORS_HEADERS_ALLOW_ALL
: getCorsHeaders(origin || request.headers.get('Origin'));
return new NextResponse(null, {
status: 204,
headers: {
...cors,
'Access-Control-Max-Age': '86400',
},
});
}
// Hotlink protection for assets: block if Referer/Origin present and not allowed
if (isAssetRoute && request.method === 'GET') {
if (origin && !isAllowedOrigin(origin)) {
return new NextResponse('Forbidden', { status: 403 });
}
// Do not override Cache-Control / ETag on Next.js image optimizer (dev cache + warnings).
if (isNextImage) {
const res = NextResponse.next();
const cors = getCorsHeaders(request.headers.get('Origin'));
Object.entries(cors).forEach(([k, v]) => res.headers.set(k, v));
return res;
}
// ETag and 304 for GET static images under /images/
const etag = computeWeakEtag(pathname, search);
const ifNoneMatch = request.headers.get('If-None-Match');
const etagMatch = ifNoneMatch && (ifNoneMatch === etag || ifNoneMatch.includes(etag));
if (etagMatch) {
return new NextResponse(null, {
status: 304,
headers: {
ETag: etag,
'Cache-Control': CACHE_CONTROL_ASSETS,
},
});
}
const res = NextResponse.next();
res.headers.set('ETag', etag);
res.headers.set('Cache-Control', CACHE_CONTROL_ASSETS);
const cors = getCorsHeaders(request.headers.get('Origin'));
Object.entries(cors).forEach(([k, v]) => res.headers.set(k, v));
return res;
}
// API routes: add CORS allow-all and 1-day cache for GET
if (isApiRoute) {
const res = NextResponse.next();
Object.entries(CORS_HEADERS_ALLOW_ALL).forEach(([k, v]) => res.headers.set(k, v));
if (request.method === 'GET') {
res.headers.set('Cache-Control', CACHE_CONTROL_API);
}
return res;
}
return NextResponse.next();
}
export const config = {
matcher: ['/api/:path*', '/images/:path*', '/_next/image'],
};