refactor(tracker-script-name): move from middleware to next.config rewrites
parent
c0a9ceae2f
commit
453373b356
|
@ -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*',
|
|
||||||
};
|
|
|
@ -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 = [];
|
const redirects = [];
|
||||||
|
|
||||||
if (process.env.CLOUD_MODE) {
|
if (process.env.CLOUD_MODE) {
|
||||||
|
|
Loading…
Reference in New Issue