From dbc19e4ffdab3f8123112fb07cfb1cf43542da03 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Thu, 20 Apr 2023 01:12:43 -0700 Subject: [PATCH] Allow multiple tracker names. --- docker/middleware.js | 3 ++- next.config.js | 14 +++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/docker/middleware.js b/docker/middleware.js index 408c22a6..fa6cbd99 100644 --- a/docker/middleware.js +++ b/docker/middleware.js @@ -24,8 +24,9 @@ function customScriptName(req) { if (scriptName) { const url = req.nextUrl.clone(); const { pathname } = url; + const names = scriptName.split(',').map(name => name.trim()); - if (pathname.endsWith(scriptName)) { + if (names.find(name => pathname.endsWith(name))) { url.pathname = '/script.js'; return NextResponse.rewrite(url); } diff --git a/next.config.js b/next.config.js index 1132cb73..303ff64f 100644 --- a/next.config.js +++ b/next.config.js @@ -45,12 +45,16 @@ if (process.env.COLLECT_API_ENDPOINT) { } if (process.env.TRACKER_SCRIPT_NAME) { - const match = process.env.TRACKER_SCRIPT_NAME?.match(/\/?(\w+)(\.js)?/); + const names = process.env.TRACKER_SCRIPT_NAME?.split(',').map(name => name.trim()); - if (match) { - rewrites.push({ - source: `/${match[0]}.js`, - destination: '/script.js', + if (names) { + names.forEach(name => { + const slash = name.substring(0, 1) === '/' ? '' : '/'; + + rewrites.push({ + source: `${slash}${name}`, + destination: '/script.js', + }); }); } }