Skip to content

Commit 656f0b9

Browse files
committed
Fix TanStack Start SSR redirects
1 parent 039f9a2 commit 656f0b9

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
@@ -38,6 +38,7 @@ import type { TurnstileAction } from "@stackframe/stack-shared/dist/utils/turnst
3838
import { isRelative } from "@stackframe/stack-shared/dist/utils/urls";
3939
import { generateUuid } from "@stackframe/stack-shared/dist/utils/uuids";
4040
import * as tanstackStartServerContext from "@stackframe/tanstack-start/tanstack-start-server-context"; // THIS_LINE_PLATFORM tanstack-start
41+
import * as TanStackRouter from "@tanstack/react-router"; // THIS_LINE_PLATFORM tanstack-start
4142
import * as cookie from "cookie";
4243
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
4344
import React, { useCallback, useMemo } from "react"; // THIS_LINE_PLATFORM react-like
@@ -568,6 +569,7 @@ export class _StackClientAppImplIncomplete<HasTokenStore extends boolean, Projec
568569
this._tokenStoreInit = resolvedOptions.tokenStore;
569570
this._redirectMethod = resolvedOptions.redirectMethod || (isBrowserLike() ? "window" : "none");
570571
this._redirectMethod = resolvedOptions.redirectMethod || "nextjs"; // THIS_LINE_PLATFORM next
572+
this._redirectMethod = resolvedOptions.redirectMethod || "tanstack-start"; // THIS_LINE_PLATFORM tanstack-start
571573
this._urlOptions = resolvedOptions.urls ?? {};
572574
this._oauthScopesOnSignIn = resolvedOptions.oauthScopesOnSignIn ?? {};
573575
this._prefetchCrossDomainHandoffParamsIfNeeded();
@@ -2463,6 +2465,10 @@ export class _StackClientAppImplIncomplete<HasTokenStore extends boolean, Projec
24632465
} else if (isReactServer && this._redirectMethod === "nextjs") {
24642466
NextNavigation.redirect(options.url.toString(), options.replace ? NextNavigation.RedirectType.replace : NextNavigation.RedirectType.push);
24652467
// END_PLATFORM
2468+
// IF_PLATFORM tanstack-start
2469+
} else if (this._redirectMethod === "tanstack-start" && !isBrowserLike()) {
2470+
throw TanStackRouter.redirect({ href: options.url.toString(), replace: options.replace });
2471+
// END_PLATFORM
24662472
} else if (typeof this._redirectMethod === "object" && this._redirectMethod.navigate) {
24672473
this._redirectMethod.navigate(options.url.toString());
24682474
} else {
@@ -2487,6 +2493,10 @@ export class _StackClientAppImplIncomplete<HasTokenStore extends boolean, Projec
24872493
const router = NextNavigation.useRouter();
24882494
return (to: string) => router.push(to);
24892495
// END_PLATFORM
2496+
// IF_PLATFORM tanstack-start
2497+
} else if (this._redirectMethod === "tanstack-start") {
2498+
return (to: string) => window.location.assign(to);
2499+
// END_PLATFORM
24902500
} else {
24912501
return (to: string) => { };
24922502
}
@@ -2532,6 +2542,20 @@ export class _StackClientAppImplIncomplete<HasTokenStore extends boolean, Projec
25322542
await this._redirectIfTrusted(plan.url, options);
25332543
}
25342544

2545+
protected _redirectToHandlerDuringRender(handlerName: keyof HandlerUrls, options?: RedirectToOptions): boolean {
2546+
// IF_PLATFORM tanstack-start
2547+
if (this._redirectMethod === "tanstack-start" && !isBrowserLike()) {
2548+
const rawUrls = getUrls(this._urlOptions, { projectId: this.projectId });
2549+
const rawHandlerUrl = rawUrls[handlerName];
2550+
if (!rawHandlerUrl) {
2551+
throw new Error(`No URL for handler name ${handlerName}`);
2552+
}
2553+
throw TanStackRouter.redirect({ href: rawHandlerUrl, replace: options?.replace });
2554+
}
2555+
// END_PLATFORM
2556+
return false;
2557+
}
2558+
25352559
async redirectToSignIn(options?: RedirectToOptions) { return await this._redirectToHandler("signIn", options); }
25362560
async redirectToSignUp(options?: RedirectToOptions) { return await this._redirectToHandler("signUp", options); }
25372561
async redirectToSignOut(options?: RedirectToOptions) { return await this._redirectToHandler("signOut", options); }
@@ -2685,9 +2709,13 @@ export class _StackClientAppImplIncomplete<HasTokenStore extends boolean, Projec
26852709
switch (options?.or) {
26862710
case 'redirect': {
26872711
if (!crud?.is_anonymous && crud?.is_restricted) {
2688-
runAsynchronously(this.redirectToOnboarding({ replace: true }));
2712+
if (!this._redirectToHandlerDuringRender("onboarding", { replace: true })) {
2713+
runAsynchronously(this.redirectToOnboarding({ replace: true }));
2714+
}
26892715
} else {
2690-
runAsynchronously(this.redirectToSignIn({ replace: true }));
2716+
if (!this._redirectToHandlerDuringRender("signIn", { replace: true })) {
2717+
runAsynchronously(this.redirectToSignIn({ replace: true }));
2718+
}
26912719
}
26922720
suspend();
26932721
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)