Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: update custom domain middleware
Marc Seitz837c73334af1
diff --git a/lib/contants.ts b/lib/contants.tsindex 14a07fff..7bf3564a 100644--- a/lib/contants.ts+++ b/lib/contants.ts@@ -8,4 +8,10 @@ export const FADE_IN_ANIMATION_SETTINGS = {export const STAGGER_CHILD_VARIANTS = {hidden: { opacity: 0, y: 20 },show: { opacity: 1, y: 0, transition: { duration: 0.4, type: "spring" } },+};++export const PAPERMARK_HEADERS = {+ headers: {+ "x-powered-by": "Papermark.io - Document sharing infrastructure for the modern web",+ },};\ No newline at end of filediff --git a/lib/middleware/domain.ts b/lib/middleware/domain.tsnew file mode 100644index 00000000..5a9c983e--- /dev/null+++ b/lib/middleware/domain.ts@@ -0,0 +1,23 @@+import { NextRequest, NextResponse } from "next/server";+import { getToken } from "next-auth/jwt";+import { PAPERMARK_HEADERS } from "../contants";++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 !== "/") {+ // Subdomain available, rewriting+ console.log(`>>> Rewriting: ${path} to /view/domains/${host}${path}`);+ url.pathname = `/view/domains/${host}${path}`;+ return NextResponse.rewrite(url, PAPERMARK_HEADERS);+ } else {+ // redirect plain custom domain to papermark.io, eventually to it's own landing page+ return NextResponse.redirect(new URL("https://www.papermark.io", req.url));+ }+}diff --git a/middleware.ts b/middleware.tsindex 2858c007..c4749c18 100644--- a/middleware.ts+++ b/middleware.ts@@ -1,5 +1,6 @@import { NextFetchEvent, NextRequest, NextResponse } from "next/server";import AppMiddleware from "@/lib/middleware/app";+import DomainMiddleware from "@/lib/middleware/domain";export const config = {matcher: [@@ -19,19 +20,18 @@ export const config = {export default async function middleware(req: NextRequest, ev: NextFetchEvent) {const path = req.nextUrl.pathname;const host = req.headers.get('host');- const url = req.nextUrl.clone();++ console.log(`>>> ENV: ${process.env.NODE_ENV}`)+ console.log(`>>> Middleware: ${path} ${host}`);if (process.env.NODE_ENV !== "development" &&!(host?.includes("papermark.io") || host?.endsWith(".vercel.app"))) {- // Subdomain available, rewriting- console.log(`>>> Rewriting: ${path} to /view/domains/${host}${path}`);- url.pathname = `/view/domains/${host}${path}`;- return NextResponse.rewrite(url);+ return DomainMiddleware(req);}- if (path !== "/" && path !== "/alternatives/docsend" && !path.startsWith("/view/")) {+ if (path !== "/" && !path.startsWith("/alternatives/") && !path.startsWith("/view/")) {return AppMiddleware(req);}diff --git a/pages/view/[linkId].tsx b/pages/view/[linkId].tsxindex 1e0b8240..3f47215c 100644--- a/pages/view/[linkId].tsx+++ b/pages/view/[linkId].tsx@@ -19,7 +19,7 @@ export type DEFAULT_ACCESS_FORM_TYPE = {password: string | null;};-type DEFAULT_DOCUMENT_VIEW_TYPE = {+export type DEFAULT_DOCUMENT_VIEW_TYPE = {viewId: string;file: string;}