fix(web): treat hosted app as static

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-04-26 23:34:48 -07:00
1 parent 008fc1c628
commit b0cf85f69a
8 files changed
+141 -14

No files matched your search

+1
View File
@@ -22,3 +22,4 @@ apps/web/src/components/__screenshots__
__screenshots__/
.tanstack
squashfs-root/
.vercel
+14
View File
@@ -3,6 +3,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import {
buildHostedPairingUrl,
hasHostedPairingRequest,
isHostedStaticApp,
readHostedPairingRequest,
} from "./hostedPairing";
@@ -49,4 +50,17 @@ describe("hostedPairing", () => {
false,
);
});
it("detects the hosted static app only when no backend URL is configured", () => {
vi.stubEnv("VITE_HOSTED_APP_URL", "https://preview.t3.codes");
vi.stubEnv("VITE_HTTP_URL", "");
vi.stubEnv("VITE_WS_URL", "");
expect(isHostedStaticApp(new URL("https://preview.t3.codes/"))).toBe(true);
expect(isHostedStaticApp(new URL("https://preview.t3.codes/pair"))).toBe(true);
expect(isHostedStaticApp(new URL("https://backend.example.com/"))).toBe(false);
vi.stubEnv("VITE_HTTP_URL", "https://backend.example.com");
expect(isHostedStaticApp(new URL("https://preview.t3.codes/"))).toBe(false);
});
});
+21
View File
@@ -12,6 +12,27 @@ function configuredHostedAppUrl(): string {
return import.meta.env.VITE_HOSTED_APP_URL?.trim() || DEFAULT_HOSTED_APP_URL;
}
function configuredBackendUrl(): string {
return import.meta.env.VITE_HTTP_URL?.trim() || import.meta.env.VITE_WS_URL?.trim() || "";
}
function originFromUrl(value: string): string | null {
try {
return new URL(value).origin;
} catch {
return null;
}
}
export function isHostedStaticApp(url: URL = new URL(window.location.href)): boolean {
if (configuredBackendUrl()) {
return false;
}
const hostedOrigin = originFromUrl(configuredHostedAppUrl());
return hostedOrigin !== null && url.origin === hostedOrigin;
}
export function readHostedPairingRequest(url: URL = new URL(window.location.href)) {
const host = url.searchParams.get("host")?.trim() ?? "";
const token = getPairingTokenFromUrl(url)?.trim() ?? "";
+30 -13
View File
@@ -58,7 +58,7 @@ import {
resolveInitialServerAuthGateState,
updatePrimaryEnvironmentDescriptor,
} from "../environments/primary";
import { hasHostedPairingRequest } from "../hostedPairing";
import { hasHostedPairingRequest, isHostedStaticApp } from "../hostedPairing";
export const Route = createRootRouteWithContext<{
queryClient: QueryClient;
@@ -72,6 +72,15 @@ export const Route = createRootRouteWithContext<{
};
}
if (isHostedStaticApp(new URL(window.location.href))) {
await waitForSavedEnvironmentRegistryHydration();
return {
authGateState: {
status: "hosted-static",
} as const,
};
}
try {
const [, authGateState] = await Promise.all([
ensurePrimaryEnvironmentReady(),
@@ -103,6 +112,7 @@ export const Route = createRootRouteWithContext<{
function RootRouteView() {
const pathname = useLocation({ select: (location) => location.pathname });
const { authGateState } = Route.useRouteContext();
const primaryEnvironmentAuthenticated = authGateState.status === "authenticated";
useEffect(() => {
const frame = window.requestAnimationFrame(() => {
@@ -120,23 +130,30 @@ function RootRouteView() {
if (authGateState.status !== "authenticated" && authGateState.status !== "hosted-static") {
return <Outlet />;
}
const appShell = (
<CommandPalette>
<AppSidebarLayout>
<Outlet />
</AppSidebarLayout>
</CommandPalette>
);
return (
<ToastProvider>
<AnchoredToastProvider>
<AuthenticatedTracingBootstrap />
<ServerStateBootstrap />
{primaryEnvironmentAuthenticated ? <AuthenticatedTracingBootstrap /> : null}
{primaryEnvironmentAuthenticated ? <ServerStateBootstrap /> : null}
<EnvironmentConnectionManagerBootstrap />
<HostedStaticEnvironmentBootstrap />
<EventRouter />
<WebSocketConnectionCoordinator />
<SlowRpcAckToastCoordinator />
<WebSocketConnectionSurface>
<CommandPalette>
<AppSidebarLayout>
<Outlet />
</AppSidebarLayout>
</CommandPalette>
</WebSocketConnectionSurface>
{primaryEnvironmentAuthenticated ? <EventRouter /> : null}
{primaryEnvironmentAuthenticated ? <WebSocketConnectionCoordinator /> : null}
{primaryEnvironmentAuthenticated ? <SlowRpcAckToastCoordinator /> : null}
{primaryEnvironmentAuthenticated ? (
<WebSocketConnectionSurface>{appShell}</WebSocketConnectionSurface>
) : (
appShell
)}
</AnchoredToastProvider>
</ToastProvider>
);
+55
View File
@@ -1,11 +1,66 @@
import { createFileRoute } from "@tanstack/react-router";
import { LinkIcon, PlusIcon } from "lucide-react";
import { NoActiveThreadState } from "../components/NoActiveThreadState";
import { Button } from "../components/ui/button";
import { Empty, EmptyDescription, EmptyHeader, EmptyTitle } from "../components/ui/empty";
import { SidebarInset, SidebarTrigger } from "../components/ui/sidebar";
import { useSavedEnvironmentRegistryStore } from "../environments/runtime";
import { APP_DISPLAY_NAME } from "~/branding";
function ChatIndexRouteView() {
const { authGateState } = Route.useRouteContext();
const savedEnvironmentCount = useSavedEnvironmentRegistryStore(
(state) => Object.keys(state.byId).length,
);
if (authGateState.status === "hosted-static" && savedEnvironmentCount === 0) {
return <HostedStaticOnboardingState />;
}
return <NoActiveThreadState />;
}
export const Route = createFileRoute("/_chat/")({
component: ChatIndexRouteView,
});
function HostedStaticOnboardingState() {
return (
<SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none bg-background text-foreground">
<div className="flex min-h-0 min-w-0 flex-1 flex-col overflow-x-hidden bg-background">
<header className="border-b border-border px-3 py-2 sm:px-5 sm:py-3">
<div className="flex items-center gap-2">
<SidebarTrigger className="size-7 shrink-0 md:hidden" />
<span className="text-sm font-medium text-foreground md:text-muted-foreground/60">
{APP_DISPLAY_NAME}
</span>
</div>
</header>
<Empty className="flex-1">
<div className="w-full max-w-xl rounded-3xl border border-border/55 bg-card/20 px-8 py-12 shadow-sm/5">
<EmptyHeader className="max-w-none">
<div className="mx-auto mb-5 flex size-11 items-center justify-center rounded-xl border border-border/70 bg-background/70 text-muted-foreground">
<LinkIcon className="size-5" />
</div>
<EmptyTitle className="text-foreground text-xl">
Connect an environment to get started
</EmptyTitle>
<EmptyDescription className="mt-2 text-sm leading-relaxed text-muted-foreground/78">
Open a pairing link from your T3 Code desktop app or add a reachable backend
manually. Your saved environments stay in this browser.
</EmptyDescription>
<div className="mt-6 flex justify-center">
<Button render={<a href="/settings/connections" />} size="sm">
<PlusIcon className="size-4" />
Add environment
</Button>
</div>
</EmptyHeader>
</div>
</Empty>
</div>
</SidebarInset>
);
}
+3
View File
@@ -3,6 +3,9 @@
import type { DesktopBridge, LocalApi } from "@t3tools/contracts";
interface ImportMetaEnv {
readonly VITE_HTTP_URL: string;
readonly VITE_WS_URL: string;
readonly VITE_HOSTED_APP_URL: string;
readonly APP_VERSION: string;
}
+8
View File
@@ -0,0 +1,8 @@
{
"rewrites": [
{
"source": "/(.*)",
"destination": "/index.html"
}
]
}
+9 -1
View File
@@ -9,7 +9,15 @@ const port = Number(process.env.PORT ?? 5733);
const host = process.env.HOST?.trim() || "localhost";
const configuredHttpUrl = process.env.VITE_HTTP_URL?.trim();
const configuredWsUrl = process.env.VITE_WS_URL?.trim();
const configuredHostedAppUrl = process.env.VITE_HOSTED_APP_URL?.trim();
const configuredHostedAppUrl = (() => {
if (process.env.VERCEL_ENV === "production" && process.env.VERCEL_PROJECT_PRODUCTION_URL) {
return `https://${process.env.VERCEL_PROJECT_PRODUCTION_URL}`;
}
if (process.env.VERCEL_URL) {
return `https://${process.env.VERCEL_URL}`;
}
return process.env.VITE_HOSTED_APP_URL?.trim();
})();
const sourcemapEnv = process.env.T3CODE_WEB_SOURCEMAP?.trim().toLowerCase();
const buildSourcemap =