Skip to content

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

chore: cleanup middleware

Marc Seitz53355404f074
diff --git a/lib/middleware/domain.ts b/lib/middleware/domain.ts
index f49f1a43..07da83a3 100644
--- a/lib/middleware/domain.ts
+++ b/lib/middleware/domain.ts
@@ -1,32 +1,35 @@
import { NextRequest, NextResponse } from "next/server";
-import { BLOCKED_PATHNAMES, PAPERMARK_HEADERS } from "@/lib/constants";
+import { BLOCKED_PATHNAMES } from "@/lib/constants";
export default async function DomainMiddleware(req: NextRequest) {
const path = req.nextUrl.pathname;
const host = req.headers.get("host");
- // clone the URL so we can modify it
- const url = req.nextUrl.clone();
-
- // if there's a path and it's not "/" then we need to check if it's a custom domain
- if (path !== "/") {
- if (BLOCKED_PATHNAMES.includes(path) || path.includes(".")) {
- url.pathname = "/404";
- return NextResponse.rewrite(url, { status: 404 });
- }
- // Subdomain available, rewriting
- // >>> Rewriting: ${path} to /view/domains/${host}${path}`
- url.pathname = `/view/domains/${host}${path}`;
- const response = NextResponse.rewrite(url, PAPERMARK_HEADERS);
-
- // Add X-Robots-Tag header for custom domain routes
- response.headers.set("X-Robots-Tag", "noindex");
- return response;
- } else {
- // redirect plain custom domain to papermark.io, eventually to it's own landing page
+ // If it's the root path, redirect to papermark.io/home
+ if (path === "/") {
return NextResponse.redirect(
new URL("https://www.papermark.io/home", req.url),
);
}
+
+ const url = req.nextUrl.clone();
+
+ // Check for blocked pathnames
+ if (BLOCKED_PATHNAMES.includes(path) || path.includes(".")) {
+ url.pathname = "/404";
+ return NextResponse.rewrite(url, { status: 404 });
+ }
+
+ // Rewrite the URL to the correct page component for custom domains
+ // Rewrite to the pages/view/domains/[domain]/[slug] route
+ url.pathname = `/view/domains/${host}/${path}`;
+
+ return NextResponse.rewrite(url, {
+ headers: {
+ "X-Robots-Tag": "noindex",
+ "X-Powered-By":
+ "Papermark.io - Document sharing infrastructure for the modern web",
+ },
+ });
}
diff --git a/middleware.ts b/middleware.ts
index 107f21e1..ef8933d4 100644
--- a/middleware.ts
+++ b/middleware.ts
@@ -19,6 +19,18 @@ function isAnalyticsPath(path: string) {
return pattern.test(path);
}
+function isCustomDomain(host: string) {
+ return (
+ (process.env.NODE_ENV === "development" && host?.includes(".local")) ||
+ (process.env.NODE_ENV !== "development" &&
+ !(
+ host?.includes("localhost") ||
+ host?.includes("papermark.io") ||
+ host?.endsWith(".vercel.app")
+ ))
+ );
+}
+
export const config = {
matcher: [
/*
@@ -46,18 +58,12 @@ export default async function middleware(req: NextRequest, ev: NextFetchEvent) {
return IncomingWebhookMiddleware(req);
}
- if (
- (process.env.NODE_ENV === "development" && host?.includes(".local")) ||
- (process.env.NODE_ENV !== "development" &&
- !(
- host?.includes("localhost") ||
- host?.includes("papermark.io") ||
- host?.endsWith(".vercel.app")
- ))
- ) {
+ // For custom domains, we need to handle them differently
+ if (isCustomDomain(host || "")) {
return DomainMiddleware(req);
}
+ // Handle standard papermark.io paths
if (
!path.startsWith("/view/") &&
!path.startsWith("/verify") &&
@@ -66,13 +72,13 @@ export default async function middleware(req: NextRequest, ev: NextFetchEvent) {
return AppMiddleware(req);
}
- const url = req.nextUrl.clone();
-
+ // Check for blocked pathnames in view routes
if (
path.startsWith("/view/") &&
(BLOCKED_PATHNAMES.some((blockedPath) => path.includes(blockedPath)) ||
path.includes("."))
) {
+ const url = req.nextUrl.clone();
url.pathname = "/404";
return NextResponse.rewrite(url, { status: 404 });
}