diff --git a/middleware.ts b/middleware.ts deleted file mode 100644 index d07d713d..00000000 --- a/middleware.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { NextResponse } from 'next/server'; -import type { NextRequest } from 'next/server'; - -export async function middleware(req: NextRequest) { - const scriptName = process.env.TRACKER_SCRIPT_NAME; - - if (scriptName) { - const url = req.nextUrl.clone(); - const { pathname } = url; - const names = scriptName.split(',').map(name => name.trim() + '.js'); - - if (names.find(name => pathname.endsWith(name))) { - url.pathname = '/script.js'; - return NextResponse.rewrite(url); - } - } -} - -export const config = { - matcher: '/:path*', -}; diff --git a/next.config.js b/next.config.js index f45d7d81..042f2477 100644 --- a/next.config.js +++ b/next.config.js @@ -44,6 +44,17 @@ if (process.env.COLLECT_API_ENDPOINT) { }); } +if (process.env.TRACKER_SCRIPT_NAME) { + const scriptNames = process.env.TRACKER_SCRIPT_NAME.split(','); + + rewrites.push( + ...scriptNames.map(scriptName => ({ + source: `/${scriptName.trim()}.js`, + destination: '/script.js', + })), + ); +} + const redirects = []; if (process.env.CLOUD_MODE) {