Merge pull request #1257 from rohandebsarkar/custom-endpoint
Adds suport custom collect API endpointpull/1279/head
commit
eb538259fa
|
@ -101,6 +101,7 @@
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@formatjs/cli": "^4.2.29",
|
"@formatjs/cli": "^4.2.29",
|
||||||
"@rollup/plugin-buble": "^0.21.3",
|
"@rollup/plugin-buble": "^0.21.3",
|
||||||
|
"@rollup/plugin-replace": "^4.0.0",
|
||||||
"@svgr/webpack": "^6.2.1",
|
"@svgr/webpack": "^6.2.1",
|
||||||
"cross-env": "^7.0.3",
|
"cross-env": "^7.0.3",
|
||||||
"eslint": "^7.32.0",
|
"eslint": "^7.32.0",
|
||||||
|
|
|
@ -1,5 +1,19 @@
|
||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
|
|
||||||
|
function customCollectEndpoint(req) {
|
||||||
|
const collectEndpoint = process.env.COLLECT_API_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) {
|
function customScriptName(req) {
|
||||||
const scriptName = process.env.TRACKER_SCRIPT_NAME;
|
const scriptName = process.env.TRACKER_SCRIPT_NAME;
|
||||||
|
|
||||||
|
@ -24,7 +38,7 @@ function forceSSL(req, res) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function middleware(req) {
|
export function middleware(req) {
|
||||||
const fns = [customScriptName];
|
const fns = [customCollectEndpoint, customScriptName];
|
||||||
|
|
||||||
for (const fn of fns) {
|
for (const fn of fns) {
|
||||||
const res = fn(req);
|
const res = fn(req);
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import buble from '@rollup/plugin-buble';
|
import buble from '@rollup/plugin-buble';
|
||||||
|
import replace from '@rollup/plugin-replace';
|
||||||
import { terser } from 'rollup-plugin-terser';
|
import { terser } from 'rollup-plugin-terser';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
@ -7,5 +8,12 @@ export default {
|
||||||
file: 'public/umami.js',
|
file: 'public/umami.js',
|
||||||
format: 'iife',
|
format: 'iife',
|
||||||
},
|
},
|
||||||
plugins: [buble({ objectAssign: true }), terser({ compress: { evaluate: false } })],
|
plugins: [
|
||||||
|
replace({
|
||||||
|
'/api/collect': process.env.COLLECT_API_ENDPOINT || '/api/collect',
|
||||||
|
delimiters: ['', ''],
|
||||||
|
}),
|
||||||
|
buble({ objectAssign: true }),
|
||||||
|
terser({ compress: { evaluate: false } }),
|
||||||
|
],
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue