Adds support for `customCollectEndpoint`

This commit is contained in:
rohandebsarkar 2022-07-03 09:30:23 +05:30
parent 43f89a1cdc
commit ae48ff0e68
No known key found for this signature in database
GPG Key ID: 3DA3229799D1A181
1 changed files with 15 additions and 1 deletions

View File

@ -1,5 +1,19 @@
import { NextResponse } from 'next/server';
function customCollectEndpoint(req) {
const collectEndpoint = process.env.API_COLLECT_ENDPOINT;
if (collectEndpoint) {
const url = req.nextUrl.clone();
const { pathname } = url;
if (pathname.endsWith(collectEndpoint)) {
url.pathname = '/api/collect';
return NextResponse.rewrite(url);
}
}
}
function customScriptName(req) {
const scriptName = process.env.TRACKER_SCRIPT_NAME;
@ -24,7 +38,7 @@ function forceSSL(req, res) {
}
export function middleware(req) {
const fns = [customScriptName];
const fns = [customCollectEndpoint, customScriptName];
for (const fn of fns) {
const res = fn(req);