fix(middleware): bring back middleware for TRACKER_SCRIPT_NAME functionality

pull/1915/head
Slowlydev 2023-04-18 21:52:18 +02:00
parent 3eabe9b958
commit c0a9ceae2f
1 changed files with 21 additions and 0 deletions

21
middleware.ts Normal file
View File

@ -0,0 +1,21 @@
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*',
};