Skip to content

Commit df68c76

Browse files
committed
Fix TanStack Start SSR redirects
1 parent 248dcd3 commit df68c76

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

packages/template/src/lib/stack-app/apps/implementations/client-app-impl.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import type { TurnstileAction } from "@stackframe/stack-shared/dist/utils/turnst
3737
import { isRelative } from "@stackframe/stack-shared/dist/utils/urls";
3838
import { generateUuid } from "@stackframe/stack-shared/dist/utils/uuids";
3939
import * as tanstackStartServerContext from "@stackframe/tanstack-start/tanstack-start-server-context"; // THIS_LINE_PLATFORM tanstack-start
40+
import * as TanStackRouter from "@tanstack/react-router"; // THIS_LINE_PLATFORM tanstack-start
4041
import * as cookie from "cookie";
4142
import * as NextNavigationUnscrambled from "next/navigation"; // import the entire module to get around some static compiler warnings emitted by Next.js in some cases | THIS_LINE_PLATFORM next
4243
import React, { useCallback, useMemo } from "react"; // THIS_LINE_PLATFORM react-like
@@ -561,6 +562,7 @@ export class _StackClientAppImplIncomplete<HasTokenStore extends boolean, Projec
561562
this._tokenStoreInit = resolvedOptions.tokenStore;
562563
this._redirectMethod = resolvedOptions.redirectMethod || (isBrowserLike() ? "window" : "none");
563564
this._redirectMethod = resolvedOptions.redirectMethod || "nextjs"; // THIS_LINE_PLATFORM next
565+
this._redirectMethod = resolvedOptions.redirectMethod || "tanstack-start"; // THIS_LINE_PLATFORM tanstack-start
564566
this._urlOptions = resolvedOptions.urls ?? {};
565567
this._oauthScopesOnSignIn = resolvedOptions.oauthScopesOnSignIn ?? {};
566568
this._prefetchCrossDomainHandoffParamsIfNeeded();
@@ -2456,6 +2458,10 @@ export class _StackClientAppImplIncomplete<HasTokenStore extends boolean, Projec
24562458
} else if (isReactServer && this._redirectMethod === "nextjs") {
24572459
NextNavigation.redirect(options.url.toString(), options.replace ? NextNavigation.RedirectType.replace : NextNavigation.RedirectType.push);
24582460
// END_PLATFORM
2461+
// IF_PLATFORM tanstack-start
2462+
} else if (this._redirectMethod === "tanstack-start" && !isBrowserLike()) {
2463+
throw TanStackRouter.redirect({ href: options.url.toString(), replace: options.replace });
2464+
// END_PLATFORM
24592465
} else if (typeof this._redirectMethod === "object" && this._redirectMethod.navigate) {
24602466
this._redirectMethod.navigate(options.url.toString());
24612467
} else {
@@ -2480,6 +2486,10 @@ export class _StackClientAppImplIncomplete<HasTokenStore extends boolean, Projec
24802486
const router = NextNavigation.useRouter();
24812487
return (to: string) => router.push(to);
24822488
// END_PLATFORM
2489+
// IF_PLATFORM tanstack-start
2490+
} else if (this._redirectMethod === "tanstack-start") {
2491+
return (to: string) => window.location.assign(to);
2492+
// END_PLATFORM
24832493
} else {
24842494
return (to: string) => { };
24852495
}
@@ -2525,6 +2535,20 @@ export class _StackClientAppImplIncomplete<HasTokenStore extends boolean, Projec
25252535
await this._redirectIfTrusted(plan.url, options);
25262536
}
25272537

2538+
protected _redirectToHandlerDuringRender(handlerName: keyof HandlerUrls, options?: RedirectToOptions): boolean {
2539+
// IF_PLATFORM tanstack-start
2540+
if (this._redirectMethod === "tanstack-start" && !isBrowserLike()) {
2541+
const rawUrls = getUrls(this._urlOptions, { projectId: this.projectId });
2542+
const rawHandlerUrl = rawUrls[handlerName];
2543+
if (!rawHandlerUrl) {
2544+
throw new Error(`No URL for handler name ${handlerName}`);
2545+
}
2546+
throw TanStackRouter.redirect({ href: rawHandlerUrl, replace: options?.replace });
2547+
}
2548+
// END_PLATFORM
2549+
return false;
2550+
}
2551+
25282552
async redirectToSignIn(options?: RedirectToOptions) { return await this._redirectToHandler("signIn", options); }
25292553
async redirectToSignUp(options?: RedirectToOptions) { return await this._redirectToHandler("signUp", options); }
25302554
async redirectToSignOut(options?: RedirectToOptions) { return await this._redirectToHandler("signOut", options); }
@@ -2678,9 +2702,13 @@ export class _StackClientAppImplIncomplete<HasTokenStore extends boolean, Projec
26782702
switch (options?.or) {
26792703
case 'redirect': {
26802704
if (!crud?.is_anonymous && crud?.is_restricted) {
2681-
runAsynchronously(this.redirectToOnboarding({ replace: true }));
2705+
if (!this._redirectToHandlerDuringRender("onboarding", { replace: true })) {
2706+
runAsynchronously(this.redirectToOnboarding({ replace: true }));
2707+
}
26822708
} else {
2683-
runAsynchronously(this.redirectToSignIn({ replace: true }));
2709+
if (!this._redirectToHandlerDuringRender("signIn", { replace: true })) {
2710+
runAsynchronously(this.redirectToSignIn({ replace: true }));
2711+
}
26842712
}
26852713
suspend();
26862714
throw new StackAssertionError("suspend should never return");

packages/template/src/lib/stack-app/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export type EmailConfig = {
3030

3131
export type RedirectMethod = "window"
3232
| "nextjs" // THIS_LINE_PLATFORM next
33+
| "tanstack-start" // THIS_LINE_PLATFORM tanstack-start
3334
| "none"
3435
| {
3536
useNavigate: () => (to: string) => void,

0 commit comments

Comments
 (0)