Skip to content

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

fix(layout): prevent sidebar hydration mismatch on first render

Marc Seitza6f870186d90
diff --git a/components/layouts/app.tsx b/components/layouts/app.tsx
index 987d7636..57b7dc4f 100644
--- a/components/layouts/app.tsx
+++ b/components/layouts/app.tsx
@@ -22,8 +22,16 @@ import {
import { BlockingModal } from "./blocking-modal";
+const DEFAULT_SIDEBAR_OPEN = true;
+
+// SSR and the first client render must return the same value to avoid a
+// hydration mismatch; the cookie is only read after hydration.
+let sidebarHydrated = false;
+
function getInitialSidebarState(isDataroom: boolean): boolean {
- if (typeof window === "undefined") return false;
+ if (typeof window === "undefined" || !sidebarHydrated) {
+ return DEFAULT_SIDEBAR_OPEN;
+ }
if (isDataroom) {
return true;
@@ -34,7 +42,7 @@ function getInitialSidebarState(isDataroom: boolean): boolean {
return mainCookie === "true";
}
- return true;
+ return DEFAULT_SIDEBAR_OPEN;
}
/**
@@ -85,12 +93,15 @@ export default function AppLayout({ children }: { children: React.ReactNode }) {
getInitialSidebarState(isDataroom),
);
- const handleSidebarOpenChange = useCallback(
- (open: boolean) => {
- setSidebarOpen(open);
- },
- [],
- );
+ // Restore the persisted sidebar state after the first render has hydrated.
+ useEffect(() => {
+ sidebarHydrated = true;
+ setSidebarOpen(getInitialSidebarState(isDataroom));
+ }, [isDataroom]);
+
+ const handleSidebarOpenChange = useCallback((open: boolean) => {
+ setSidebarOpen(open);
+ }, []);
return (
<SidebarProvider open={sidebarOpen} onOpenChange={handleSidebarOpenChange}>