Skip to content

Open source alternative to Semrush and Ahrefs

Don't flash the dashboard before redirecting to onboarding (#991)

Carter Smith2feb7e20bbd8
diff --git a/src/client/features/onboarding/useOnboardingRedirect.ts b/src/client/features/onboarding/useOnboardingRedirect.ts
index 38d44396..8b9bf8f5 100644
--- a/src/client/features/onboarding/useOnboardingRedirect.ts
+++ b/src/client/features/onboarding/useOnboardingRedirect.ts
@@ -9,6 +9,8 @@ import {
isHostedClientAuthMode,
} from "@/lib/auth-mode";
+// Returns whether the app can render: true once onboarding is known to be
+// finished, or its check failed (the app is the safer fallback).
export function useOnboardingRedirect() {
const navigate = useNavigate();
const { data: session } = useSession();
@@ -50,4 +52,10 @@ export function useOnboardingRedirect() {
isEmailVerified,
session?.user?.id,
]);
+
+ return (
+ !isHostedMode ||
+ onboardingQuery.isError ||
+ Boolean(onboardingQuery.data?.completedAt)
+ );
}
diff --git a/src/lib/auth-client.ts b/src/lib/auth-client.ts
index 7db599fa..c722e965 100644
--- a/src/lib/auth-client.ts
+++ b/src/lib/auth-client.ts
@@ -4,6 +4,7 @@ import {
inferAdditionalFields,
organizationClient,
} from "better-auth/client/plugins";
+import { clearLastProjectId } from "@/client/lib/active-project";
import { captureClientEvent, resetAnalyticsUser } from "@/client/lib/posthog";
import { userAdditionalFields } from "@/lib/auth-options";
import { orgAccessControl, orgRoles } from "@/lib/org-permissions";
@@ -27,6 +28,10 @@ export function signOutAndRedirect() {
const signInHref = getSignInHrefForLocation(window.location);
captureClientEvent("auth:sign_out");
resetAnalyticsUser();
+ // The remembered project belongs to this account. Left behind, it would
+ // open the next account straight into the app shell, past the onboarding
+ // gate in the app layout.
+ clearLastProjectId();
void authClient.signOut({
fetchOptions: {
onSuccess: () => {
diff --git a/src/routes/_app/route.tsx b/src/routes/_app/route.tsx
index 4fff2598..22aaa572 100644
--- a/src/routes/_app/route.tsx
+++ b/src/routes/_app/route.tsx
@@ -5,6 +5,8 @@ import { prefetchBillingAccount } from "@/client/features/billing/billingAccount
import { FreePlanBanner } from "@/client/features/billing/FreePlanBanner";
import { TermsUpdateBanner } from "@/client/features/auth/TermsUpdateBanner";
import { SkeletonPage } from "@/client/components/SkeletonPresets";
+import { PageLoading } from "@/client/components/Spinner";
+import { getLastProjectId } from "@/client/lib/active-project";
import { AuthenticatedAppLayout } from "@/client/layout/AppShell";
import { useOnboardingRedirect } from "@/client/features/onboarding/useOnboardingRedirect";
@@ -17,7 +19,7 @@ export const Route = createFileRoute("/_app")({
// between the two.
function AppRouteLayout() {
const authGate = useHostedAuthRouteGuard();
- useOnboardingRedirect();
+ const isOnboarded = useOnboardingRedirect();
const { projectId } = useParams({ strict: false });
// The shell paints at once, and only the page waits for the session check,
// so a full load shows the sidebar and a page skeleton instead of a spinner.
@@ -29,6 +31,13 @@ function AppRouteLayout() {
if (ready) prefetchBillingAccount();
}, [ready]);
+ // Until onboarding is known to be done, show a plain spinner, so a user on
+ // their way to /onboarding never sees the dashboard flash first. A browser
+ // that has opened a project is past onboarding, so the shell paints at once.
+ if (!isOnboarded && !getLastProjectId()) {
+ return <PageLoading />;
+ }
+
return (
<AuthenticatedAppLayout
projectId={projectId}