Browse Source

refactor: offload authentication route resolution to client and improve redirect logic in landing pages

master
mortezaei 7 days ago
parent
commit
64b7239ed0
  1. 34
      src/app/[lang]/page.tsx
  2. 17
      src/app/page.tsx

34
src/app/[lang]/page.tsx

@ -7,10 +7,6 @@ import {
isAuthenticatedToken,
MARRIAGE_ENTRY_PATH_COOKIE,
} from "@/lib/entry-route-cache";
import {
getSubmitPath,
hasCompletedMarriageProfileBasics,
} from "@/lib/get-submit-path";
import { localizePath } from "@/translations/config";
export const dynamic = "force-dynamic";
@ -34,38 +30,12 @@ export default async function LocaleEntryPage({
redirect(localizePath(cachedEntryPath, lang));
}
// 1. If not authenticated, render <Intro /> instantly on the server (0ms client delay)
// 1. If not authenticated, render <Intro /> instantly in SSR (0ms client delay)
if (!hasToken) {
return <Intro />;
}
// 2. If authenticated without cached route, resolve target route directly on the server
let targetPath: string | null = null;
const apiBaseUrl = process.env.NEXT_PUBLIC_API_BASE_URL;
if (apiBaseUrl) {
try {
const res = await fetch(`${apiBaseUrl}/api/marriage/profile/main/`, {
headers: {
Authorization: `Token ${token}`,
Accept: "application/json",
},
cache: "no-store",
});
if (res.ok) {
const profile = await res.json();
targetPath = hasCompletedMarriageProfileBasics(profile)
? getSubmitPath(profile)
: "/intro";
}
} catch {
// Graceful fallback to client-side resolver if server fetch fails
}
}
if (targetPath) {
redirect(localizePath(targetPath, lang));
}
// 2. If authenticated without cached route, deliver head/web_ready immediately and resolve in client
return (
<>
<EntryRouteResolver anonymousEntryVisible={false} />

17
src/app/page.tsx

@ -2,6 +2,7 @@ import { cookies, headers } from "next/headers";
import { redirect } from "next/navigation";
import {
getAuthenticatedCachedEntryPath,
isAuthenticatedToken,
MARRIAGE_ENTRY_PATH_COOKIE,
} from "@/lib/entry-route-cache";
import {
@ -36,14 +37,20 @@ export default async function RootPage() {
const token =
cookieStore.get("HABIB_TOKEN")?.value ??
cookieStore.get("habib_token")?.value;
const hasToken = isAuthenticatedToken(token);
if (!hasToken) {
redirect(`/${targetLocale}`);
}
const cachedEntryPath = getAuthenticatedCachedEntryPath(
token,
cookieStore.get(MARRIAGE_ENTRY_PATH_COOKIE)?.value,
);
redirect(
cachedEntryPath
? localizePath(cachedEntryPath, targetLocale)
: `/${targetLocale}`,
);
if (cachedEntryPath) {
redirect(localizePath(cachedEntryPath, targetLocale));
}
redirect(`/${targetLocale}`);
}
Loading…
Cancel
Save