Skip to content

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

chore: update robots

Marc Seitzb6733b43525f
diff --git a/app/robots.txt b/app/robots.txt
index 444c64d4..98500e91 100644
--- a/app/robots.txt
+++ b/app/robots.txt
@@ -1,5 +1,4 @@
User-Agent: *
-Disallow: /login
Disallow: /register
Disallow: /verify/
Disallow: /auth/
diff --git a/lib/middleware/app.ts b/lib/middleware/app.ts
index c05eda8a..cf74ba15 100644
--- a/lib/middleware/app.ts
+++ b/lib/middleware/app.ts
@@ -2,6 +2,36 @@ import { NextRequest, NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";
+const LOGIN_PATH = "/login";
+const DEFAULT_AUTH_REDIRECT_PATH = "/dashboard";
+
+function normalizeNextPath(nextPath: string | null): string {
+ if (!nextPath) {
+ return DEFAULT_AUTH_REDIRECT_PATH;
+ }
+
+ let normalized = nextPath;
+
+ // Handle already-encoded and double-encoded `next` values.
+ for (let i = 0; i < 3; i += 1) {
+ try {
+ const decoded = decodeURIComponent(normalized);
+ if (decoded === normalized) {
+ break;
+ }
+ normalized = decoded;
+ } catch {
+ break;
+ }
+ }
+
+ if (!normalized.startsWith("/")) {
+ return DEFAULT_AUTH_REDIRECT_PATH;
+ }
+
+ return normalized;
+}
+
export default async function AppMiddleware(req: NextRequest) {
const url = req.nextUrl;
const path = url.pathname;
@@ -17,18 +47,39 @@ export default async function AppMiddleware(req: NextRequest) {
};
// UNAUTHENTICATED if there's no token and the path isn't /login, redirect to /login
- if (!token?.email && path !== "/login") {
- const loginUrl = new URL(`/login`, req.url);
+ if (!token?.email && path !== LOGIN_PATH) {
+ const loginUrl = new URL(LOGIN_PATH, req.url);
// Append "next" parameter only if not navigating to the root
if (path !== "/") {
const nextPath =
path === "/auth/confirm-email-change" ? `${path}${url.search}` : path;
- loginUrl.searchParams.set("next", encodeURIComponent(nextPath));
+ loginUrl.searchParams.set("next", nextPath);
}
return NextResponse.redirect(loginUrl);
}
+ if (!token?.email && path === LOGIN_PATH) {
+ const rawNextPath = url.searchParams.get("next");
+
+ if (rawNextPath) {
+ const normalizedNextPath = normalizeNextPath(rawNextPath);
+ const canonicalLoginUrl = new URL(LOGIN_PATH, req.url);
+ canonicalLoginUrl.searchParams.set("next", normalizedNextPath);
+
+ if (canonicalLoginUrl.search !== url.search) {
+ return NextResponse.redirect(canonicalLoginUrl, { status: 308 });
+ }
+
+ // Keep the base /login URL indexable for now, but deindex parameterized variants.
+ const response = NextResponse.next();
+ response.headers.set("X-Robots-Tag", "noindex, nofollow");
+ return response;
+ }
+
+ return NextResponse.next();
+ }
+
// AUTHENTICATED if the user was created in the last 10 seconds, redirect to "/welcome"
if (
token?.email &&
@@ -40,11 +91,11 @@ export default async function AppMiddleware(req: NextRequest) {
return NextResponse.redirect(new URL("/welcome", req.url));
}
- // AUTHENTICATED if the path is /login, redirect to "/dashboard"
- if (token?.email && path === "/login") {
- const nextPath = url.searchParams.get("next") || "/dashboard"; // Default redirection to "/dashboard" if no next parameter
- return NextResponse.redirect(
- new URL(decodeURIComponent(nextPath), req.url),
- );
+ // AUTHENTICATED if the path is /login, redirect to the next path
+ if (token?.email && path === LOGIN_PATH) {
+ const nextPath = normalizeNextPath(url.searchParams.get("next"));
+ return NextResponse.redirect(new URL(nextPath, req.url));
}
+
+ return NextResponse.next();
}
diff --git a/middleware.ts b/middleware.ts
index 0234ff06..64b6126c 100644
--- a/middleware.ts
+++ b/middleware.ts
@@ -41,9 +41,9 @@ export const config = {
* 2. /_next/ (Next.js internals)
* 3. /_static (inside /public)
* 4. /_vercel (Vercel internals)
- * 5. /favicon.ico, /sitemap.xml (static files)
+ * 5. /favicon.ico, /sitemap.xml, /robots.txt (static files)
*/
- "/((?!api/|_next/|_static|vendor|_icons|_vercel|favicon.ico|sitemap.xml).*)",
+ "/((?!api/|_next/|_static|vendor|_icons|_vercel|favicon.ico|sitemap.xml|robots.txt).*)",
],
};
diff --git a/next.config.mjs b/next.config.mjs
index 02ff28d3..f4b0837d 100644
--- a/next.config.mjs
+++ b/next.config.mjs
@@ -91,6 +91,21 @@ const nextConfig = {
},
],
},
+ {
+ source: "/login",
+ has: [
+ {
+ type: "query",
+ key: "next",
+ },
+ ],
+ headers: [
+ {
+ key: "X-Robots-Tag",
+ value: "noindex, nofollow",
+ },
+ ],
+ },
{
// Embed routes - allow iframe embedding
source: "/view/:path*/embed",