Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Refactor Sidebar components to use Suspense for improved loading behavior

Bill Yang2c54b0ebb2df
diff --git a/client/src/app/[site]/components/Sidebar/MobileSidebar.tsx b/client/src/app/[site]/components/Sidebar/MobileSidebar.tsx
index d41b97ed7..71bcb310d 100644
--- a/client/src/app/[site]/components/Sidebar/MobileSidebar.tsx
+++ b/client/src/app/[site]/components/Sidebar/MobileSidebar.tsx
@@ -11,8 +11,9 @@ import { VisuallyHidden } from "radix-ui";
import { Favicon } from "../../../../components/Favicon";
import { AppSidebar } from "../../../../components/AppSidebar";
import { useEmbedablePage } from "../../utils";
+import { Suspense } from "react";
-export function MobileSidebar() {
+function MobileSidebarContent() {
const pathname = usePathname();
const { data: site } = useGetSite(Number(pathname.split("/")[1]));
@@ -41,3 +42,11 @@ export function MobileSidebar() {
</div>
);
}
+
+export function MobileSidebar() {
+ return (
+ <Suspense fallback={null}>
+ <MobileSidebarContent />
+ </Suspense>
+ );
+}
diff --git a/client/src/app/[site]/components/Sidebar/Sidebar.tsx b/client/src/app/[site]/components/Sidebar/Sidebar.tsx
index 72f702f2d..208c169f1 100644
--- a/client/src/app/[site]/components/Sidebar/Sidebar.tsx
+++ b/client/src/app/[site]/components/Sidebar/Sidebar.tsx
@@ -25,8 +25,9 @@ import { authClient } from "../../../../lib/auth";
import { IS_CLOUD } from "../../../../lib/const";
import { SiteSelector } from "./SiteSelector";
import { useEmbedablePage } from "../../utils";
+import { Suspense } from "react";
-export function Sidebar() {
+function SidebarContent() {
const session = authClient.useSession();
const pathname = usePathname();
const embed = useEmbedablePage();
@@ -170,3 +171,11 @@ export function Sidebar() {
</div>
);
}
+
+export function Sidebar() {
+ return (
+ <Suspense fallback={null}>
+ <SidebarContent />
+ </Suspense>
+ );
+}
diff --git a/client/src/app/[site]/components/Sidebar/SiteSelector.tsx b/client/src/app/[site]/components/Sidebar/SiteSelector.tsx
index 203ed46ca..fb2a5b919 100644
--- a/client/src/app/[site]/components/Sidebar/SiteSelector.tsx
+++ b/client/src/app/[site]/components/Sidebar/SiteSelector.tsx
@@ -1,6 +1,6 @@
import { ChevronDown, Plus } from "lucide-react";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
-import { useState } from "react";
+import { useState, Suspense } from "react";
import { useGetSite, useGetSitesFromOrg } from "../../../../api/admin/sites";
import { Favicon } from "../../../../components/Favicon";
import { Button } from "../../../../components/ui/button";
@@ -150,7 +150,7 @@ function SiteSelectorContent({ onSiteSelect }: { onSiteSelect: () => void }) {
);
}
-export function SiteSelector() {
+function SiteSelectorWrapper() {
const { site: currentSite } = useStore();
const { data: site } = useGetSite(currentSite);
const [open, setOpen] = useState(false);
@@ -173,7 +173,17 @@ export function SiteSelector() {
</button>
)}
</PopoverTrigger>
- <SiteSelectorContent onSiteSelect={() => setOpen(false)} />
+ <Suspense fallback={null}>
+ <SiteSelectorContent onSiteSelect={() => setOpen(false)} />
+ </Suspense>
</Popover>
);
}
+
+export function SiteSelector() {
+ return (
+ <Suspense fallback={null}>
+ <SiteSelectorWrapper />
+ </Suspense>
+ );
+}
diff --git a/client/src/components/AppSidebar.tsx b/client/src/components/AppSidebar.tsx
index 69f242cf3..5f0c62783 100644
--- a/client/src/components/AppSidebar.tsx
+++ b/client/src/components/AppSidebar.tsx
@@ -4,12 +4,12 @@ import { BarChart, ShieldUser, User } from "lucide-react";
import Image from "next/image";
import Link from "next/link";
import { usePathname } from "next/navigation";
-import { useState } from "react";
+import { useState, Suspense } from "react";
import { useAdminPermission } from "../app/admin/hooks/useAdminPermission";
import { cn } from "../lib/utils";
import { useEmbedablePage } from "../app/[site]/utils";
-export function AppSidebar() {
+function AppSidebarContent() {
const pathname = usePathname();
const { isAdmin } = useAdminPermission();
const [isExpanded, setIsExpanded] = useState(false);
@@ -65,6 +65,14 @@ export function AppSidebar() {
);
}
+export function AppSidebar() {
+ return (
+ <Suspense fallback={null}>
+ <AppSidebarContent />
+ </Suspense>
+ );
+}
+
function SidebarLink({
active = false,
href,