Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🔗 fix: Hide the Viewer's Deployment Theme While a Shared Link Loads (#16608)

Marco Beretta7305d5dfa51d
diff --git a/client/src/Providers/DeploymentTheme.tsx b/client/src/Providers/DeploymentTheme.tsx
index a7882babc9..7125d8dd34 100644
--- a/client/src/Providers/DeploymentTheme.tsx
+++ b/client/src/Providers/DeploymentTheme.tsx
@@ -214,19 +214,18 @@ const DeploymentThemeOverrideContext = createContext<(override: ThemeOverride) =
/**
* Lets a route whose policy comes from another tenant paint that tenant's theme:
- * once `ready`, `theme` replaces `interface.theme` from the startup config, an
- * absent theme included, until the route unmounts. A route whose theme source
- * failed passes `ready` with no theme, so the viewer's theme does not stand in
- * for the link's. Registered in a layout effect so the wrapper re-renders in the
- * same commit, and `ThemeProvider` applies the change before that commit paints.
+ * from the commit the route mounts until it unmounts, the route's theme replaces
+ * `interface.theme` from the startup config. Until `ready` that theme is absent,
+ * so the viewer's theme never stands in for the link's while its policy loads,
+ * including after an in-app navigation that keeps the viewer's answer on hand; a
+ * route whose theme source failed passes `ready` with no theme for the same reason.
+ * Registered in a layout effect so the wrapper re-renders in the same commit, and
+ * `ThemeProvider` applies the change before that commit paints.
*/
export function useDeploymentThemeOverride(ready: boolean, theme: DeploymentThemeValue) {
const setOverride = useContext(DeploymentThemeOverrideContext);
useLayoutEffect(() => {
- if (!ready) {
- return;
- }
- setOverride({ theme });
+ setOverride({ theme: ready ? theme : undefined });
return () => setOverride(undefined);
}, [ready, theme, setOverride]);
}
diff --git a/client/src/Providers/__tests__/DeploymentTheme.spec.tsx b/client/src/Providers/__tests__/DeploymentTheme.spec.tsx
index 60257f4280..d18d9f3efa 100644
--- a/client/src/Providers/__tests__/DeploymentTheme.spec.tsx
+++ b/client/src/Providers/__tests__/DeploymentTheme.spec.tsx
@@ -84,11 +84,21 @@ function SharedRoute() {
return route ? <SharedThemeSource theme={route.theme} /> : null;
}
-function SharedThemeSource({ theme }: { theme: ConfigTheme }) {
- useDeploymentThemeOverride(true, theme);
+function SharedThemeSource({ theme, ready = true }: { theme: ConfigTheme; ready?: boolean }) {
+ useDeploymentThemeOverride(ready, theme);
return null;
}
+let showLoadingRoute: (route: { ready: boolean; theme?: ConfigTheme } | null) => void = () =>
+ undefined;
+
+/** Stands in for the share route while its own config is still loading. */
+function LoadingRoute() {
+ const [route, setRoute] = useState<{ ready: boolean; theme?: ConfigTheme } | null>(null);
+ showLoadingRoute = setRoute;
+ return route ? <SharedThemeSource theme={route.theme} ready={route.ready} /> : null;
+}
+
function renderTheme(queryClient: QueryClient, user?: Pick<TUser, 'id' | 'tenantId'>) {
return render(
<RecoilRoot initializeState={({ set }) => user && set(store.user, user as TUser)}>
@@ -96,6 +106,7 @@ function renderTheme(queryClient: QueryClient, user?: Pick<TUser, 'id' | 'tenant
<DeploymentTheme>
<LateRoute />
<SharedRoute />
+ <LoadingRoute />
<ThemeEditor />
</DeploymentTheme>
</QueryClientProvider>
@@ -360,6 +371,22 @@ describe('DeploymentTheme', () => {
expect(localStorage.getItem('theme-definition')).toBe(JSON.stringify(storedDefinition));
});
+ it('drops the viewer theme in the commit a route mounts, before its own theme is ready', async () => {
+ serveTheme('clickhouse');
+ renderTheme(queryClient);
+ await waitFor(() => expect(root().dataset.theme).toBe('clickhouse'));
+
+ act(() => showLoadingRoute({ ready: false }));
+ expect(root().dataset.theme).toBe('stored');
+
+ act(() => showLoadingRoute({ ready: true, theme: inlineTheme }));
+ expect(root().dataset.theme).toBe('acme');
+
+ act(() => showLoadingRoute(null));
+ expect(root().dataset.theme).toBe('clickhouse');
+ expect(localStorage.getItem('theme-definition')).toBe(JSON.stringify(storedDefinition));
+ });
+
it('shows the stored theme when a route supplies no deployment theme', async () => {
serveTheme('clickhouse');
renderTheme(queryClient);
diff --git a/e2e/specs/mock/scenarios/share-deployment-theme.spec.ts b/e2e/specs/mock/scenarios/share-deployment-theme.spec.ts
index 149b6762b0..6eede75dad 100644
--- a/e2e/specs/mock/scenarios/share-deployment-theme.spec.ts
+++ b/e2e/specs/mock/scenarios/share-deployment-theme.spec.ts
@@ -203,4 +203,51 @@ test.describe('deployment theme on a shared link', () => {
await page.getByTestId('messages-view').waitFor({ timeout: 20000 });
expect(await page.locator('html').getAttribute('data-theme')).toBe('clickhouse');
});
+
+ test("an in-app navigation to a shared link never paints the viewer's theme while its config loads @scenario:shared-link-in-app-navigation-skips-viewer-theme", async ({
+ page,
+ }) => {
+ test.setTimeout(120000);
+ await serveThemes(page, VIEWER_THEME, 'clickhouse');
+ const shareId = await createSharedLink(page);
+ await expectViewerTheme(page);
+ await page.route(
+ (url) => url.pathname === `/api/share/${shareId}/config`,
+ async (route) => {
+ await new Promise((resolve) => setTimeout(resolve, 3000));
+ await route.fallback();
+ },
+ );
+
+ /** Every frame from here on, sampled before it paints, with the route it showed. */
+ await page.evaluate(() => {
+ const frames: Array<{ path: string; theme: string | null }> = [];
+ (window as Window & { __shareFrames?: typeof frames }).__shareFrames = frames;
+ const sample = () => {
+ frames.push({
+ path: window.location.pathname,
+ theme: document.documentElement.getAttribute('data-theme'),
+ });
+ requestAnimationFrame(sample);
+ };
+ requestAnimationFrame(sample);
+ });
+ /** The router navigates in place, as the Shared Links entry does: the app and its
+ * startup config answer stay mounted, unlike a document load. */
+ await page.evaluate((id) => {
+ window.history.pushState({}, '', `/share/${id}`);
+ window.dispatchEvent(new PopStateEvent('popstate'));
+ }, shareId);
+ await expect(page.getByTestId('messages-view')).toBeVisible({ timeout: 20000 });
+ await expect(page.locator('html')).toHaveAttribute('data-theme', 'clickhouse');
+
+ const frames = await page.evaluate(
+ () =>
+ (window as Window & { __shareFrames?: Array<{ path: string; theme: string | null }> })
+ .__shareFrames ?? [],
+ );
+ const onShare = frames.filter((frame) => frame.path.startsWith('/share/'));
+ expect(onShare.length).toBeGreaterThan(0);
+ expect(onShare.filter((frame) => frame.theme === 'viewer')).toEqual([]);
+ });
});