mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 17:55:08 +01:00
47 lines
952 B
JavaScript
47 lines
952 B
JavaScript
import { NextResponse } from 'next/server';
|
|
|
|
export const config = {
|
|
matcher: '/:path*',
|
|
};
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
function customScriptName(req) {
|
|
const scriptName = process.env.TRACKER_SCRIPT_NAME;
|
|
|
|
if (scriptName) {
|
|
const url = req.nextUrl.clone();
|
|
const { pathname } = url;
|
|
|
|
if (pathname.endsWith(scriptName)) {
|
|
url.pathname = '/script.js';
|
|
return NextResponse.rewrite(url);
|
|
}
|
|
}
|
|
}
|
|
|
|
export default function middleware(req) {
|
|
const fns = [customCollectEndpoint, customScriptName];
|
|
|
|
for (const fn of fns) {
|
|
const res = fn(req);
|
|
if (res) {
|
|
return res;
|
|
}
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|