Skip to content

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

fix(home): persist time filters in the URL

Bill Yang0ed18c56c8d2
diff --git a/client/src/app/(home)/page.tsx b/client/src/app/(home)/page.tsx
index ea383d1e5..e8dee7d44 100644
--- a/client/src/app/(home)/page.tsx
+++ b/client/src/app/(home)/page.tsx
@@ -23,6 +23,7 @@ import { Pagination } from "../../components/pagination";
import { useSetPageTitle } from "../../hooks/useSetPageTitle";
import { authClient } from "../../lib/auth";
import { canGoBack, canGoForward, goBack, goForward, useStore } from "../../lib/store";
+import { useSyncStateWithUrl } from "../../lib/urlParams";
import { AddSite } from "../components/AddSite";
import { SiteCards } from "./SiteCards";
@@ -32,6 +33,7 @@ const PAGE_SIZE = 20;
export default function Home() {
const t = useExtracted();
useSetPageTitle("Home");
+ useSyncStateWithUrl();
const { width } = useWindowSize();
const isDesktop = width !== null && width >= 768;
diff --git a/client/src/lib/urlParams.test.tsx b/client/src/lib/urlParams.test.tsx
new file mode 100644
index 000000000..7824cc5e9
--- /dev/null
+++ b/client/src/lib/urlParams.test.tsx
@@ -0,0 +1,119 @@
+import { act, cleanup, render, waitFor } from "@testing-library/react";
+import { NuqsTestingAdapter, UrlUpdateEvent } from "nuqs/adapters/testing";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { DEFAULT_COMPARISON } from "../components/DateSelector/types";
+import { useStore } from "./store";
+import { useSyncStateWithUrl } from "./urlParams";
+
+const navigation = vi.hoisted(() => ({ pathname: "/", search: "" }));
+vi.mock("next/navigation", () => ({
+ usePathname: () => navigation.pathname,
+ useSearchParams: () => new URLSearchParams(navigation.search),
+}));
+
+function Sync() {
+ useSyncStateWithUrl();
+ return null;
+}
+
+const onUrlUpdate = vi.fn(({ queryString }: UrlUpdateEvent) => {
+ navigation.search = queryString;
+});
+
+function mount(search = "", pathname = "/") {
+ navigation.pathname = pathname;
+ navigation.search = search;
+ return render(
+ <NuqsTestingAdapter searchParams={search} onUrlUpdate={onUrlUpdate} hasMemory>
+ <Sync />
+ </NuqsTestingAdapter>
+ );
+}
+
+beforeEach(() => {
+ localStorage.clear();
+ onUrlUpdate.mockClear();
+ useStore.setState({
+ site: "",
+ privateKey: null,
+ time: { mode: "day", day: "2026-09-21" },
+ previousTime: { mode: "day", day: "2026-09-20" },
+ comparison: DEFAULT_COMPARISON,
+ bucket: "hour",
+ selectedStat: "users",
+ filters: [],
+ segmentId: null,
+ timezone: "UTC",
+ });
+});
+afterEach(cleanup);
+
+describe("homepage time URL persistence", () => {
+ it("restores a rolling window and comparison without a selected Site", async () => {
+ mount("?timeMode=past-minutes&past_minutes_start=30&past_minutes_end=0&compare=none&bucket=minute");
+ await waitFor(() =>
+ expect(useStore.getState()).toMatchObject({
+ time: { mode: "past-minutes", pastMinutesStart: 30, pastMinutesEnd: 0 },
+ comparison: { mode: "none" },
+ previousTime: null,
+ bucket: "minute",
+ })
+ );
+ });
+
+ it("writes changed time filters and restores them after a refresh", async () => {
+ const view = mount();
+ await waitFor(() => expect(onUrlUpdate).toHaveBeenCalled());
+ const time = {
+ mode: "range" as const,
+ startDate: "2026-09-18",
+ endDate: "2026-09-19",
+ startTime: "10:30",
+ endTime: "14:45",
+ };
+ act(() => {
+ useStore.getState().setTime(time);
+ useStore.getState().setComparison({ mode: "none" });
+ });
+ await waitFor(() => {
+ const params = new URLSearchParams(navigation.search);
+ expect(params.get("timeMode")).toBe("range");
+ expect(params.get("startTime")).toBe("10:30");
+ expect(params.get("endTime")).toBe("14:45");
+ expect(params.get("compare")).toBe("none");
+ });
+ const savedSearch = navigation.search;
+ view.unmount();
+ useStore.getState().setTime({ mode: "all-time" });
+ useStore.getState().setComparison(DEFAULT_COMPARISON);
+ mount(savedSearch);
+ await waitFor(() => {
+ expect(useStore.getState().time).toEqual(time);
+ expect(useStore.getState().previousTime).toBeNull();
+ });
+ });
+
+ it("restores the homepage URL after visiting a Site without copying its dimension filters", async () => {
+ useStore.setState({
+ site: "42",
+ selectedStat: "bounce_rate",
+ filters: [{ parameter: "country", type: "equals", value: ["US"] }],
+ segmentId: 7,
+ });
+ mount("?wellKnown=last-30-minutes&campaign=kept");
+ await waitFor(() => expect(onUrlUpdate).toHaveBeenCalled());
+ expect(useStore.getState().time).toMatchObject({ mode: "past-minutes", pastMinutesStart: 30 });
+ const params = new URLSearchParams(navigation.search);
+ expect(params.get("wellKnown")).toBe("last-30-minutes");
+ expect(params.get("campaign")).toBe("kept");
+ for (const key of ["stat", "filters", "segment"]) expect(params.has(key)).toBe(false);
+ });
+
+ it("still waits for the correct Site context on Site dashboards", async () => {
+ mount("?timeMode=past-minutes&past_minutes_start=60&past_minutes_end=0", "/42/main");
+ expect(onUrlUpdate).not.toHaveBeenCalled();
+ expect(useStore.getState().time.mode).toBe("day");
+ act(() => useStore.setState({ site: "42" }));
+ await waitFor(() => expect(useStore.getState().time).toMatchObject({ pastMinutesStart: 60 }));
+ });
+});
diff --git a/client/src/lib/urlParams.ts b/client/src/lib/urlParams.ts
index 748dbd3b3..528edf523 100644
--- a/client/src/lib/urlParams.ts
+++ b/client/src/lib/urlParams.ts
@@ -31,7 +31,9 @@ export const useSyncStateWithUrl = () => {
} = useStore();
const routeContext = React.useMemo(() => getSiteRouteContext(pathname), [pathname]);
- const shouldSyncUrl = isSyncedAnalyticsRoute(routeContext.route);
+ const isHomepage = pathname === "/";
+ const shouldSyncUrl = isHomepage || isSyncedAnalyticsRoute(routeContext.route);
+ const isRouteReady = isHomepage || site === routeContext.siteId;
const hydrationKey = shouldSyncUrl
? `${routeContext.siteId ?? ""}:${routeContext.privateKey ?? ""}:${routeContext.route ?? ""}?${searchParams.toString()}`
: null;
@@ -49,9 +51,9 @@ export const useSyncStateWithUrl = () => {
shallow: true,
});
- // Initialize from URL params after site is set
+ // Site dashboards wait for their context; the homepage has no selected Site.
useEffect(() => {
- if (!hydrationKey || site !== routeContext.siteId || hydratedUrlKey === hydrationKey) return;
+ if (!hydrationKey || !isRouteReady || hydratedUrlKey === hydrationKey) return;
// The comparison is restored before the period so the window it resolves
// to is the shared link's, not the default period's.
@@ -71,21 +73,19 @@ export const useSyncStateWithUrl = () => {
setBucket(urlParams.bucket);
}
- if (urlParams.stat) {
- setSelectedStat(urlParams.stat);
- } else {
- setSelectedStat("users");
- }
+ if (!isHomepage) {
+ setSelectedStat(urlParams.stat ?? "users");
- setFilters(urlParams.filters ?? []);
- setSegmentId(urlParams.segment ?? null);
+ setFilters(urlParams.filters ?? []);
+ setSegmentId(urlParams.segment ?? null);
+ }
setHydratedUrlKey(hydrationKey);
}, [
hydrationKey,
hydratedUrlKey,
- routeContext.siteId,
- site,
+ isHomepage,
+ isRouteReady,
setTime,
setBucket,
setSelectedStat,
@@ -97,7 +97,7 @@ export const useSyncStateWithUrl = () => {
// Update URL when state changes
useEffect(() => {
- if (!hydrationKey || hydratedUrlKey !== hydrationKey || site !== routeContext.siteId) return;
+ if (!hydrationKey || hydratedUrlKey !== hydrationKey || !isRouteReady) return;
// Build params object to update - values, not parsers
const newParams: Record<string, any> = {
@@ -107,9 +107,11 @@ export const useSyncStateWithUrl = () => {
startDateTime: null,
endDateTime: null,
bucket,
- stat: selectedStat,
- filters: filters.length > 0 ? filters : null,
- segment: segmentId,
+ ...(!isHomepage && {
+ stat: selectedStat,
+ filters: filters.length > 0 ? filters : null,
+ segment: segmentId,
+ }),
};
// Note: embed params are automatically preserved by nuqs
@@ -121,10 +123,10 @@ export const useSyncStateWithUrl = () => {
filters,
segmentId,
comparison,
- site,
+ isHomepage,
+ isRouteReady,
setUrlParams,
hydrationKey,
hydratedUrlKey,
- routeContext.siteId,
]);
};