umami/docker/middleware.ts
Mike Cao c71e9b5707 Merge branch 'dev' into jajaja
# Conflicts:
#	package.json
#	yarn.lock
2025-03-31 23:31:09 -05:00

59 lines
1.4 KiB
TypeScript

import { NextResponse } from 'next/server';
export const config = {
matcher: '/:path*',
};
const apiHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': 'GET, DELETE, POST, PUT',
'Access-Control-Max-Age': process.env.CORS_MAX_AGE || '86400',
};
const trackerHeaders = {
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'public, max-age=86400, must-revalidate',
};
function customCollectEndpoint(req) {
const collectEndpoint = process.env.COLLECT_API_ENDPOINT;
if (collectEndpoint) {
const url = req.nextUrl.clone();
const { pathname } = url;
if (pathname.endsWith(collectEndpoint)) {
url.pathname = '/api/send';
return NextResponse.rewrite(url, { headers: apiHeaders });
}
}
}
function customScriptName(req) {
const scriptName = process.env.TRACKER_SCRIPT_NAME;
if (scriptName) {
const url = req.nextUrl.clone();
const { pathname } = url;
const names = scriptName.split(',').map(name => name.trim().replace(/^\/+/, ''));
if (names.find(name => pathname.endsWith(name))) {
url.pathname = '/script.js';
return NextResponse.rewrite(url, { headers: trackerHeaders });
}
}
}
export function middleware(req) {
const fns = [customCollectEndpoint, customScriptName];
for (const fn of fns) {
const res = fn(req);
if (res) {
return res;
}
}
return NextResponse.next();
}