From ae48ff0e68fcb61e6c1953fa66f4d8338263abd2 Mon Sep 17 00:00:00 2001 From: rohandebsarkar Date: Sun, 3 Jul 2022 09:30:23 +0530 Subject: [PATCH] Adds support for `customCollectEndpoint` --- pages/_middleware.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pages/_middleware.js b/pages/_middleware.js index b8c66e94..b30ff1d4 100644 --- a/pages/_middleware.js +++ b/pages/_middleware.js @@ -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);