mirror of
https://github.com/umami-software/umami.git
synced 2025-12-08 05:12:36 +01:00
Merge dev.
This commit is contained in:
commit
be1b2fc272
88 changed files with 4120 additions and 21010 deletions
74
scripts/set-routes-manifest.mjs
Normal file
74
scripts/set-routes-manifest.mjs
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/* eslint-disable no-console */
|
||||
import 'dotenv/config';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
const routesManifestPath = path.resolve(__dirname, '../.next/routes-manifest.json');
|
||||
const originalPath = path.resolve(__dirname, '../.next/routes-manifest-orig.json');
|
||||
const originalManifest = require(originalPath);
|
||||
|
||||
const API_PATH = '/api/:path*';
|
||||
const TRACKER_SCRIPT = '/script.js';
|
||||
|
||||
const collectApiEndpoint = process.env.COLLECT_API_ENDPOINT;
|
||||
const trackerScriptName = process.env.TRACKER_SCRIPT_NAME;
|
||||
|
||||
const headers = [];
|
||||
const rewrites = [];
|
||||
|
||||
if (collectApiEndpoint) {
|
||||
const apiRoute = originalManifest.headers.find(route => route.source === API_PATH);
|
||||
const routeRegex = new RegExp(apiRoute.regex);
|
||||
|
||||
rewrites.push({
|
||||
source: collectApiEndpoint,
|
||||
destination: '/api/send',
|
||||
});
|
||||
|
||||
if (!routeRegex.test(collectApiEndpoint)) {
|
||||
headers.push({
|
||||
source: collectApiEndpoint,
|
||||
headers: apiRoute.headers,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (trackerScriptName) {
|
||||
const trackerRoute = originalManifest.headers.find(route => route.source === TRACKER_SCRIPT);
|
||||
|
||||
const names = trackerScriptName?.split(',').map(name => name.trim());
|
||||
|
||||
if (names) {
|
||||
names.forEach(name => {
|
||||
const normalizedSource = `/${name.replace(/^\/+/, '')}`;
|
||||
|
||||
rewrites.push({
|
||||
source: normalizedSource,
|
||||
destination: TRACKER_SCRIPT,
|
||||
});
|
||||
|
||||
headers.push({
|
||||
source: normalizedSource,
|
||||
headers: trackerRoute.headers,
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const routesManifest = { ...originalManifest };
|
||||
|
||||
if (rewrites.length !== 0) {
|
||||
const { buildCustomRoute } = require('next/dist/lib/build-custom-route');
|
||||
|
||||
const builtHeaders = headers.map(header => buildCustomRoute('header', header));
|
||||
const builtRewrites = rewrites.map(rewrite => buildCustomRoute('rewrite', rewrite));
|
||||
|
||||
routesManifest.headers = [...originalManifest.headers, ...builtHeaders];
|
||||
routesManifest.rewrites = [...builtRewrites, ...originalManifest.rewrites];
|
||||
|
||||
console.log('Using updated Next.js routes manifest');
|
||||
} else {
|
||||
console.log('Using original Next.js routes manifest');
|
||||
}
|
||||
|
||||
fs.writeFileSync(routesManifestPath, JSON.stringify(routesManifest, null, 2));
|
||||
Loading…
Add table
Add a link
Reference in a new issue