Browse Source

perf(routing): resolve entry route and intro rendering directly in SSR

master
mortezaei 7 days ago
parent
commit
b5eb0f0866
  1. 39
      src/app/[lang]/page.tsx

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

@ -7,6 +7,10 @@ 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";
@ -30,10 +34,41 @@ export default async function LocaleEntryPage({
redirect(localizePath(cachedEntryPath, lang));
}
// 1. If not authenticated, render <Intro /> instantly on the server (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));
}
return (
<>
{!hasToken ? <Intro /> : null}
<EntryRouteResolver anonymousEntryVisible={!hasToken} />
<EntryRouteResolver anonymousEntryVisible={false} />
</>
);
}
Loading…
Cancel
Save