Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: add posthog middleware

Marc Seitzd73f4309a2ff
diff --git a/components/providers/posthog-provider.tsx b/components/providers/posthog-provider.tsx
index a5fcf409..296dff11 100644
--- a/components/providers/posthog-provider.tsx
+++ b/components/providers/posthog-provider.tsx
@@ -18,6 +18,7 @@ export const PostHogCustomProvider = ({
if (typeof window !== "undefined" && posthogConfig) {
posthog.init(posthogConfig.key, {
api_host: posthogConfig.host,
+ ui_host: "https://eu.posthog.com",
disable_session_recording: true,
// Enable debug mode in development
loaded: (posthog) => {
diff --git a/lib/middleware/posthog.ts b/lib/middleware/posthog.ts
new file mode 100644
index 00000000..e165ada5
--- /dev/null
+++ b/lib/middleware/posthog.ts
@@ -0,0 +1,26 @@
+import { NextRequest, NextResponse } from "next/server";
+
+export default async function PostHogMiddleware(req: NextRequest) {
+ let url = req.nextUrl.clone();
+ const hostname = url.pathname.startsWith("/ingest/static/")
+ ? "eu-assets.i.posthog.com"
+ : "eu.i.posthog.com";
+ const requestHeaders = new Headers(req.headers);
+ requestHeaders.set("host", hostname);
+
+ // Handle OPTIONS method for CORS preflight
+ if (req.method === "OPTIONS") {
+ return new NextResponse("", {
+ status: 200,
+ });
+ }
+
+ url.protocol = "https";
+ url.hostname = hostname;
+ url.port = "443";
+ url.pathname = url.pathname.replace(/^\/ingest/, "");
+
+ return NextResponse.rewrite(url, {
+ headers: requestHeaders,
+ });
+}
diff --git a/middleware.ts b/middleware.ts
index 68243f84..0ff33116 100644
--- a/middleware.ts
+++ b/middleware.ts
@@ -2,6 +2,17 @@ import { NextFetchEvent, NextRequest, NextResponse } from "next/server";
import AppMiddleware from "@/lib/middleware/app";
import DomainMiddleware from "@/lib/middleware/domain";
import { BLOCKED_PATHNAMES } from "./lib/constants";
+import PostHogMiddleware from "./lib/middleware/posthog";
+
+function isAnalyticsPath(path: string) {
+ // Create a regular expression
+ // ^ - asserts position at start of the line
+ // /ingest/ - matches the literal string "/ingest/"
+ // .* - matches any character (except for line terminators) 0 or more times
+ const pattern = /^\/ingest\/.*/;
+
+ return pattern.test(path);
+}
export const config = {
matcher: [
@@ -12,9 +23,8 @@ export const config = {
* 3. /_static (inside /public)
* 4. /_vercel (Vercel internals)
* 5. /favicon.ico, /sitemap.xml (static files)
- * 6. ingest (analytics)
*/
- "/((?!api/|_next/|_static|_vercel|ingest|favicon.ico|sitemap.xml).*)",
+ "/((?!api/|_next/|_static|_vercel|favicon.ico|sitemap.xml).*)",
],
};
@@ -22,6 +32,10 @@ export default async function middleware(req: NextRequest, ev: NextFetchEvent) {
const path = req.nextUrl.pathname;
const host = req.headers.get("host");
+ if (isAnalyticsPath(path)) {
+ return PostHogMiddleware(req);
+ }
+
if (
(process.env.NODE_ENV === "development" && host?.includes(".local")) ||
(process.env.NODE_ENV !== "development" &&
diff --git a/next.config.mjs b/next.config.mjs
index 855ad120..e93ffa60 100644
--- a/next.config.mjs
+++ b/next.config.mjs
@@ -64,14 +64,7 @@ const nextConfig = {
],
},
transpilePackages: ["@trigger.dev/react"],
- async rewrites() {
- return [
- {
- source: "/ingest/:path*",
- destination: "https://eu.posthog.com/:path*",
- },
- ];
- },
+ skipTrailingSlashRedirect: true,
experimental: {
outputFileTracingIncludes: {
"/api/mupdf/*": ["./node_modules/mupdf/lib/*.wasm"],