Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
Fix last-visited tracker: exclude Q&A and /view routes, harden against corrupted saves
Marc Seitzc60751c9c96d
diff --git a/lib/hooks/use-last-visited.ts b/lib/hooks/use-last-visited.tsindex 299d6db2..baaafc9e 100644--- a/lib/hooks/use-last-visited.ts+++ b/lib/hooks/use-last-visited.ts@@ -15,24 +15,53 @@ type LastVisited = { path: string; ts: number };// Paths that should never be remembered as a "last visited" destination, nor// act as the page we redirect away from on entry. The default landing page-// (/dashboard) and the onboarding flow (/welcome) are intentionally excluded.+// (/dashboard), the onboarding flow (/welcome), and shared viewer links+// (/view) are intentionally excluded.const TRACK_DENYLIST = ["/dashboard","/welcome","/login","/register","/auth",+ "/view",+];++// Route segments that opt a whole subtree out of tracking. The dataroom Q&A+// pages (/datarooms/[id]/conversations...) rewrite the URL with+// history.replaceState behind the router's back, so router.asPath goes stale+// there and the saved value would not match what the user actually sees.+const TRACK_DENY_SEGMENTS = ["conversations"];++// Pages that don't count as "being inside the app" yet for entry handling:+// rendering any page outside this list marks the tab session as entered.+const ENTRY_NEUTRAL_PATHS = [+ "/dashboard",+ "/welcome",+ "/login",+ "/register",+ "/auth",+ "/view",];function getPathname(path: string): string {return path.split(/[?#]/)[0];}+function matchesPathPrefix(pathname: string, prefixes: string[]): boolean {+ return prefixes.some(+ (prefix) => pathname === prefix || pathname.startsWith(`${prefix}/`),+ );+}+function isTrackablePath(path: string): boolean {const pathname = getPathname(path);- return !TRACK_DENYLIST.some(- (denied) => pathname === denied || pathname.startsWith(`${denied}/`),- );+ // Next.js exposes the unresolved route template (e.g. /datarooms/[id]) as+ // asPath while a statically-optimized page hydrates; never treat those as+ // real destinations.+ if (pathname.includes("[")) return false;+ if (matchesPathPrefix(pathname, TRACK_DENYLIST)) return false;+ const segments = pathname.split("/");+ return !TRACK_DENY_SEGMENTS.some((denied) => segments.includes(denied));}export function saveLastVisited(path: string) {@@ -53,6 +82,11 @@ export function getLastVisited(): LastVisited | null {const parsed = JSON.parse(raw) as LastVisited;if (!parsed?.path || typeof parsed.ts !== "number") return null;if (Date.now() - parsed.ts > MAX_AGE_MS) return null;+ // Older versions stored route templates and now-denylisted paths (e.g.+ // /datarooms/<id>/conversations/); drop those instead of redirecting.+ if (!parsed.path.startsWith("/") || !isTrackablePath(parsed.path)) {+ return null;+ }return parsed;} catch {return null;@@ -94,10 +128,24 @@ export function useTrackLastVisited() {const router = useRouter();useEffect(() => {+ // Before isReady, asPath on statically-optimized dynamic routes is still+ // the unresolved template (e.g. /datarooms/[id]/documents) — wait for the+ // real URL.+ if (!router.isReady) return;++ const pathname = getPathname(router.asPath);+ if (!matchesPathPrefix(pathname, ENTRY_NEUTRAL_PATHS)) {+ // The user is already inside the app on a real page, so this tab+ // session's "platform entry" is settled — entering the app on a deep+ // link (e.g. a Q&A page) must not make a later manual /dashboard visit+ // bounce them back.+ markEntryHandled();+ }+if (isTrackablePath(router.asPath)) {saveLastVisited(router.asPath);}- }, [router.asPath]);+ }, [router.isReady, router.asPath]);}/**