Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(gateway): reload a page restored into a workspace it no longer holds

Petr Bauch844164667b2b
diff --git a/gateway/src/App.test.tsx b/gateway/src/App.test.tsx
index 7663a1058..52c1d023a 100644
--- a/gateway/src/App.test.tsx
+++ b/gateway/src/App.test.tsx
@@ -1,6 +1,6 @@
-import { cleanup, fireEvent, render, screen } from "@testing-library/react";
+import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { MemoryRouter } from "react-router";
-import { afterEach, describe, expect, it, vi } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { AppRoutes } from "./App";
import type { Session, TenantMembership } from "./data/api";
import { AuthProvider } from "./data/AuthContext";
@@ -174,3 +174,49 @@ describe("AppRoutes", () => {
expect(screen.queryByText("Switch Gateway")).toBeNull();
});
});
+
+describe("a page restored by back or forward", () => {
+ const originalLocation = window.location;
+ const reload = vi.fn();
+
+ beforeEach(() => {
+ Object.defineProperty(window, "location", {
+ configurable: true,
+ writable: true,
+ value: { ...originalLocation, reload },
+ });
+ });
+
+ afterEach(() => {
+ cleanup();
+ vi.unstubAllGlobals();
+ reload.mockClear();
+ Object.defineProperty(window, "location", {
+ configurable: true,
+ writable: true,
+ value: originalLocation,
+ });
+ });
+
+ async function restoreAfter(serverSession: Session) {
+ mockFetch({
+ "/auth/session": [200, session({ state: "ready", tenant: acme, tenants: [acme, beta] })],
+ });
+ renderAt("/workspace");
+ expect(await screen.findByRole("heading", { name: "Acme" })).toBeTruthy();
+ mockFetch({ "/auth/session": [200, serverSession] });
+ window.dispatchEvent(new PageTransitionEvent("pageshow", { persisted: true }));
+ await waitFor(() => expect(fetch).toHaveBeenCalled());
+ }
+
+ it("reloads when the session has moved to another workspace", async () => {
+ await restoreAfter(session({ state: "ready", tenant: beta, tenants: [acme, beta] }));
+ await waitFor(() => expect(reload).toHaveBeenCalled());
+ });
+
+ it("stays as it is in the same workspace", async () => {
+ await restoreAfter(session({ state: "ready", tenant: acme, tenants: [acme, beta] }));
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ expect(reload).not.toHaveBeenCalled();
+ });
+});
diff --git a/gateway/src/data/AuthContext.tsx b/gateway/src/data/AuthContext.tsx
index d7d7a7459..9a8b71610 100644
--- a/gateway/src/data/AuthContext.tsx
+++ b/gateway/src/data/AuthContext.tsx
@@ -1,4 +1,4 @@
-import { createContext, useCallback, useContext, useEffect, useState } from "react";
+import { createContext, useCallback, useContext, useEffect, useRef, useState } from "react";
import type { ReactNode } from "react";
import {
type Session,
@@ -45,6 +45,34 @@ export function AuthProvider({ children }: { children: ReactNode }) {
refresh().finally(() => setLoading(false));
}, [refresh]);
+ // Back or forward can restore this page from the browser's cache, as it was
+ // left. Switching workspace leaves it by a full load (see `switchTo`), so a
+ // restored page may hold another workspace's session and data, and every
+ // request it makes would name that workspace. Reload it when the session it
+ // holds is no longer the server's.
+ const heldSession = useRef<Session | null>(null);
+ useEffect(() => {
+ heldSession.current = session;
+ }, [session]);
+ useEffect(() => {
+ const onPageShow = async (event: PageTransitionEvent) => {
+ if (!event.persisted) return;
+ let current: Session | null;
+ try {
+ current = await fetchSession();
+ } catch (err) {
+ setLoadError(err instanceof Error ? err.message : "Could not load your session");
+ return;
+ }
+ const held = heldSession.current;
+ if (current?.user.id !== held?.user.id || current?.tenant?.id !== held?.tenant?.id) {
+ window.location.reload();
+ }
+ };
+ window.addEventListener("pageshow", onPageShow);
+ return () => window.removeEventListener("pageshow", onPageShow);
+ }, []);
+
const login = useCallback(
async (email: string, password: string) => {
await apiLogin(email, password);