Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Added middleware for docker.

Mike Caoac8d8bbd1e6b
diff --git a/Dockerfile b/Dockerfile
index 3fc32e0bc..bdc678dac 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -12,6 +12,7 @@ RUN yarn install --frozen-lockfile
FROM node:18-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
+COPY docker/middleware.js .
COPY . .
ARG DATABASE_TYPE
diff --git a/docker/middleware.js b/docker/middleware.js
new file mode 100644
index 000000000..408c22a60
--- /dev/null
+++ b/docker/middleware.js
@@ -0,0 +1,46 @@
+import { NextResponse } from 'next/server';
+
+export const config = {
+ matcher: '/:path*',
+};
+
+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/send';
+ return NextResponse.rewrite(url);
+ }
+ }
+}
+
+function customScriptName(req) {
+ const scriptName = process.env.TRACKER_SCRIPT_NAME;
+
+ if (scriptName) {
+ const url = req.nextUrl.clone();
+ const { pathname } = url;
+
+ if (pathname.endsWith(scriptName)) {
+ url.pathname = '/script.js';
+ return NextResponse.rewrite(url);
+ }
+ }
+}
+
+export default function middleware(req) {
+ const fns = [customCollectEndpoint, customScriptName];
+
+ for (const fn of fns) {
+ const res = fn(req);
+ if (res) {
+ return res;
+ }
+ }
+
+ return NextResponse.next();
+}
diff --git a/next.config.js b/next.config.js
index f45d7d819..1132cb734 100644
--- a/next.config.js
+++ b/next.config.js
@@ -44,6 +44,17 @@ if (process.env.COLLECT_API_ENDPOINT) {
});
}
+if (process.env.TRACKER_SCRIPT_NAME) {
+ const match = process.env.TRACKER_SCRIPT_NAME?.match(/\/?(\w+)(\.js)?/);
+
+ if (match) {
+ rewrites.push({
+ source: `/${match[0]}.js`,
+ destination: '/script.js',
+ });
+ }
+}
+
const redirects = [];
if (process.env.CLOUD_MODE) {
diff --git a/package.json b/package.json
index 59fcc2e0d..a2285965e 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "umami",
- "version": "2.0.0",
+ "version": "2.1.0",
"description": "A simple, fast, privacy-focused alternative to Google Analytics.",
"author": "Mike Cao <[email protected]>",
"license": "MIT",