2022-02-21 08:27:50 +01:00
|
|
|
import { NextResponse } from 'next/server';
|
|
|
|
|
2022-07-07 18:24:32 +02:00
|
|
|
export const config = {
|
|
|
|
matcher: '/:path*',
|
|
|
|
};
|
|
|
|
|
2022-07-03 06:00:23 +02:00
|
|
|
function customCollectEndpoint(req) {
|
2022-07-04 12:22:59 +02:00
|
|
|
const collectEndpoint = process.env.COLLECT_API_ENDPOINT;
|
2022-07-03 06:00:23 +02:00
|
|
|
|
|
|
|
if (collectEndpoint) {
|
|
|
|
const url = req.nextUrl.clone();
|
|
|
|
const { pathname } = url;
|
|
|
|
|
|
|
|
if (pathname.endsWith(collectEndpoint)) {
|
|
|
|
url.pathname = '/api/collect';
|
|
|
|
return NextResponse.rewrite(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 08:27:50 +01:00
|
|
|
function customScriptName(req) {
|
|
|
|
const scriptName = process.env.TRACKER_SCRIPT_NAME;
|
|
|
|
|
|
|
|
if (scriptName) {
|
|
|
|
const url = req.nextUrl.clone();
|
|
|
|
const { pathname } = url;
|
2022-04-27 12:05:24 +02:00
|
|
|
const names = scriptName.split(',').map(name => name.trim() + '.js');
|
2022-02-21 08:27:50 +01:00
|
|
|
|
2022-04-10 07:11:15 +02:00
|
|
|
if (names.find(name => pathname.endsWith(name))) {
|
2022-02-21 08:27:50 +01:00
|
|
|
url.pathname = '/umami.js';
|
|
|
|
return NextResponse.rewrite(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-07 18:24:32 +02:00
|
|
|
export default function middleware(req) {
|
2022-07-03 06:00:23 +02:00
|
|
|
const fns = [customCollectEndpoint, customScriptName];
|
2022-02-21 08:27:50 +01:00
|
|
|
|
|
|
|
for (const fn of fns) {
|
|
|
|
const res = fn(req);
|
|
|
|
if (res) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 08:29:47 +02:00
|
|
|
return NextResponse.next();
|
2022-02-21 08:27:50 +01:00
|
|
|
}
|