mirror of
https://github.com/umami-software/umami.git
synced 2025-12-06 01:18:00 +01:00
Fixed docker build.
This commit is contained in:
parent
e7f565f143
commit
dbc9525c9c
5 changed files with 76 additions and 88 deletions
69
docker/middleware.ts
Normal file
69
docker/middleware.ts
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
export const config = {
|
||||
matcher: '/:path*',
|
||||
};
|
||||
|
||||
const TRACKER_NAME = '/script.js';
|
||||
const COLLECT_ENDPOINT = '/api/send';
|
||||
|
||||
const apiHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Headers': '*',
|
||||
'Access-Control-Allow-Methods': 'GET, DELETE, POST, PUT',
|
||||
'Access-Control-Max-Age': process.env.CORS_MAX_AGE || '86400',
|
||||
'Cache-Control': 'no-cache',
|
||||
};
|
||||
|
||||
const trackerHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Cache-Control': 'public, max-age=86400, must-revalidate',
|
||||
};
|
||||
|
||||
function customCollectEndpoint(request: NextRequest) {
|
||||
const collectEndpoint = process.env.COLLECT_API_ENDPOINT;
|
||||
|
||||
if (collectEndpoint) {
|
||||
const url = request.nextUrl.clone();
|
||||
|
||||
if (url.pathname.endsWith(collectEndpoint)) {
|
||||
url.pathname = COLLECT_ENDPOINT;
|
||||
return NextResponse.rewrite(url, { headers: apiHeaders });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function customScriptName(request: NextRequest) {
|
||||
const scriptName = process.env.TRACKER_SCRIPT_NAME;
|
||||
|
||||
if (scriptName) {
|
||||
const url = request.nextUrl.clone();
|
||||
const names = scriptName.split(',').map(name => name.trim().replace(/^\/+/, ''));
|
||||
|
||||
if (names.find(name => url.pathname.endsWith(name))) {
|
||||
url.pathname = TRACKER_NAME;
|
||||
return NextResponse.rewrite(url, { headers: trackerHeaders });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function customScriptUrl(request: NextRequest) {
|
||||
const scriptUrl = process.env.TRACKER_SCRIPT_URL;
|
||||
|
||||
if (scriptUrl && request.nextUrl.pathname.endsWith(TRACKER_NAME)) {
|
||||
return NextResponse.rewrite(scriptUrl, { headers: trackerHeaders });
|
||||
}
|
||||
}
|
||||
|
||||
export default function middleware(req: NextRequest) {
|
||||
const fns = [customCollectEndpoint, customScriptName, customScriptUrl];
|
||||
|
||||
for (const fn of fns) {
|
||||
const res = fn(req);
|
||||
if (res) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue