Skip to content

Commit 26d716b

Browse files
committed
fix: eliminate FOUC for whitelabeling across all pages
1 parent e6bfaa2 commit 26d716b

8 files changed

Lines changed: 241 additions & 43 deletions

File tree

‎apps/dokploy/components/proprietary/whitelabeling/whitelabeling-provider.tsx‎

Lines changed: 0 additions & 31 deletions
This file was deleted.

‎apps/dokploy/pages/_app.tsx‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import "@/styles/globals.css";
33
import type { NextPage } from "next";
44
import type { AppProps } from "next/app";
55
import { Inter } from "next/font/google";
6-
import Head from "next/head";
76
import { ThemeProvider } from "next-themes";
87
import NextTopLoader from "nextjs-toploader";
98
import type { ReactElement, ReactNode } from "react";
109
import { SearchCommand } from "@/components/dashboard/search-command";
11-
import { WhitelabelingProvider } from "@/components/proprietary/whitelabeling/whitelabeling-provider";
1210
import { Toaster } from "@/components/ui/sonner";
1311
import { TooltipProvider } from "@/components/ui/tooltip";
1412
import { api } from "@/utils/api";
@@ -39,9 +37,6 @@ const MyApp = ({
3937
}
4038
`}
4139
</style>
42-
<Head>
43-
<title>Dokploy</title>
44-
</Head>
4540
<TooltipProvider>
4641
<ThemeProvider
4742
attribute="class"
@@ -51,7 +46,6 @@ const MyApp = ({
5146
forcedTheme={Component.theme}
5247
>
5348
<NextTopLoader color="hsl(var(--sidebar-ring))" />
54-
<WhitelabelingProvider />
5549
<Toaster richColors />
5650
<SearchCommand />
5751
{getLayout(<Component {...pageProps} />)}

‎apps/dokploy/pages/_document.tsx‎

Lines changed: 145 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,104 @@
1-
import { Head, Html, Main, NextScript } from "next/document";
1+
import { getPublicWhitelabelingConfig } from "@dokploy/server";
2+
import NextDocument, {
3+
type DocumentContext,
4+
type DocumentInitialProps,
5+
Head,
6+
Html,
7+
Main,
8+
NextScript,
9+
} from "next/document";
210

3-
export default function Document() {
11+
interface WhitelabelingDocumentProps {
12+
metaTitle: string | null;
13+
faviconHref: string | null;
14+
customCss: string | null;
15+
}
16+
17+
// Cache the resolved favicon (inlined as a data URI) so we don't re-fetch the
18+
// remote image on every server render. Keyed by the configured favicon URL.
19+
const FAVICON_CACHE_TTL = 60 * 60 * 1000; // 1 hour
20+
21+
const SETTINGS_CACHE_TTL = 60 * 1000; // 1 minute
22+
23+
declare global {
24+
var __SETTINGS_CACHE: {
25+
data: {
26+
metaTitle: string | null;
27+
faviconHref: string | null;
28+
customCss: string | null;
29+
};
30+
expiresAt: number;
31+
} | null;
32+
var __FAVICON_CACHE: Map<string, { href: string; expiresAt: number }>;
33+
}
34+
35+
const faviconCache =
36+
globalThis.__FAVICON_CACHE ||
37+
new Map<string, { href: string; expiresAt: number }>();
38+
globalThis.__FAVICON_CACHE = faviconCache;
39+
40+
/**
41+
* Resolve the favicon to an inline data URI so it is present in the initial
42+
* HTML and renders without a network round-trip (no flash of the default
43+
* favicon). Falls back to the raw URL if the image can't be fetched.
44+
*/
45+
async function resolveFaviconHref(
46+
faviconUrl: string | null | undefined,
47+
): Promise<string | null> {
48+
if (!faviconUrl) return null;
49+
50+
const cached = faviconCache.get(faviconUrl);
51+
if (cached && cached.expiresAt > Date.now()) {
52+
return cached.href;
53+
}
54+
55+
// Default to the raw URL so the custom favicon still loads if inlining fails.
56+
let href = faviconUrl;
57+
try {
58+
const controller = new AbortController();
59+
const timeout = setTimeout(() => controller.abort(), 3000);
60+
const response = await fetch(faviconUrl, { signal: controller.signal });
61+
clearTimeout(timeout);
62+
63+
if (response.ok) {
64+
const buffer = Buffer.from(await response.arrayBuffer());
65+
// Avoid embedding very large images directly in the HTML.
66+
if (buffer.byteLength <= 512 * 1024) {
67+
const contentType = response.headers.get("content-type") || "image/png";
68+
href = `data:${contentType};base64,${buffer.toString("base64")}`;
69+
}
70+
}
71+
} catch {
72+
// Keep the raw URL fallback.
73+
}
74+
75+
faviconCache.set(faviconUrl, {
76+
href,
77+
expiresAt: Date.now() + FAVICON_CACHE_TTL,
78+
});
79+
return href;
80+
}
81+
82+
export default function Document({
83+
metaTitle,
84+
faviconHref,
85+
customCss,
86+
}: WhitelabelingDocumentProps) {
87+
const title = metaTitle || "Dokploy";
488
return (
589
<Html lang="en" className="font-sans">
690
<Head>
7-
<link rel="icon" href="/icon.svg" />
91+
{/* Rendered on the server so the correct branding is present on first
92+
paint (and for social scrapers), avoiding a flash of / fallback to
93+
the default Dokploy branding. */}
94+
<title>{title}</title>
95+
<link rel="icon" href={faviconHref || "/icon.svg"} />
96+
{customCss && (
97+
<style
98+
id="whitelabeling-styles"
99+
dangerouslySetInnerHTML={{ __html: customCss }}
100+
/>
101+
)}
8102
</Head>
9103
<body className="flex h-full w-full flex-col font-sans">
10104
<Main />
@@ -13,3 +107,51 @@ export default function Document() {
13107
</Html>
14108
);
15109
}
110+
111+
Document.getInitialProps = async (
112+
ctx: DocumentContext,
113+
): Promise<DocumentInitialProps & WhitelabelingDocumentProps> => {
114+
const initialProps = await NextDocument.getInitialProps(ctx);
115+
116+
let metaTitle: string | null = null;
117+
let faviconHref: string | null = null;
118+
let customCss: string | null = null;
119+
120+
if (
121+
globalThis.__SETTINGS_CACHE &&
122+
globalThis.__SETTINGS_CACHE.expiresAt > Date.now() &&
123+
globalThis.__SETTINGS_CACHE.data
124+
) {
125+
return {
126+
...initialProps,
127+
...globalThis.__SETTINGS_CACHE.data,
128+
};
129+
}
130+
131+
try {
132+
const config = await getPublicWhitelabelingConfig();
133+
if (config) {
134+
metaTitle = config.metaTitle;
135+
customCss = config.customCss;
136+
faviconHref = await resolveFaviconHref(config.faviconUrl);
137+
}
138+
} catch {
139+
// Fall back to defaults if settings can't be read (e.g. DB not ready)
140+
}
141+
142+
globalThis.__SETTINGS_CACHE = {
143+
data: {
144+
metaTitle,
145+
faviconHref,
146+
customCss,
147+
},
148+
expiresAt: Date.now() + SETTINGS_CACHE_TTL,
149+
};
150+
151+
return {
152+
...initialProps,
153+
metaTitle,
154+
faviconHref,
155+
customCss,
156+
};
157+
};

‎apps/dokploy/pages/index.tsx‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import {
55
} from "@dokploy/server";
66
import { validateRequest } from "@dokploy/server/lib/auth";
77
import { standardSchemaResolver as zodResolver } from "@hookform/resolvers/standard-schema";
8+
import { createServerSideHelpers } from "@trpc/react-query/server";
89
import { REGEXP_ONLY_DIGITS } from "input-otp";
910
import type { GetServerSidePropsContext } from "next";
1011
import Link from "next/link";
1112
import { useRouter } from "next/router";
1213
import { type ReactElement, useState } from "react";
1314
import { useForm } from "react-hook-form";
1415
import { toast } from "sonner";
16+
import superjson from "superjson";
1517
import { z } from "zod";
1618
import { OnboardingLayout } from "@/components/layouts/onboarding-layout";
1719
import { SignInWithGithub } from "@/components/proprietary/auth/sign-in-with-github";
@@ -45,6 +47,7 @@ import {
4547
} from "@/components/ui/input-otp";
4648
import { Label } from "@/components/ui/label";
4749
import { authClient } from "@/lib/auth-client";
50+
import { appRouter } from "@/server/api/root";
4851
import { api } from "@/utils/api";
4952
import { useWhitelabelingPublic } from "@/utils/hooks/use-whitelabeling";
5053

@@ -107,7 +110,6 @@ export default function Home({ IS_CLOUD, enforceSSO }: Props) {
107110
return;
108111
}
109112

110-
// @ts-ignore
111113
if (data?.twoFactorRedirect as boolean) {
112114
setTwoFactorCode("");
113115
setIsTwoFactor(true);
@@ -430,6 +432,21 @@ Home.getLayout = (page: ReactElement) => {
430432
return <OnboardingLayout>{page}</OnboardingLayout>;
431433
};
432434
export async function getServerSideProps(context: GetServerSidePropsContext) {
435+
const helpers = createServerSideHelpers({
436+
router: appRouter,
437+
ctx: {
438+
req: context.req as any,
439+
res: context.res as any,
440+
db: null as any,
441+
session: null as any,
442+
user: null as any,
443+
},
444+
transformer: superjson,
445+
});
446+
// Prefetch the public branding so the login/onboarding logo and app name
447+
// render correctly on the server (no flash of default branding).
448+
await helpers.whitelabeling.getPublic.prefetch();
449+
433450
if (IS_CLOUD) {
434451
try {
435452
const { user } = await validateRequest(context.req);
@@ -445,6 +462,7 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
445462

446463
return {
447464
props: {
465+
trpcState: helpers.dehydrate(),
448466
IS_CLOUD: IS_CLOUD,
449467
enforceSSO: false,
450468
},
@@ -476,6 +494,7 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
476494

477495
return {
478496
props: {
497+
trpcState: helpers.dehydrate(),
479498
hasAdmin,
480499
enforceSSO: webServerSettings?.enforceSSO ?? false,
481500
},

‎apps/dokploy/pages/invitation.tsx‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { getUserByToken, IS_CLOUD } from "@dokploy/server";
22
import { standardSchemaResolver as zodResolver } from "@hookform/resolvers/standard-schema";
3+
import { createServerSideHelpers } from "@trpc/react-query/server";
34
import type { GetServerSidePropsContext } from "next";
45
import Link from "next/link";
56
import { useRouter } from "next/router";
67
import { type ReactElement, useEffect } from "react";
78
import { useForm } from "react-hook-form";
89
import { toast } from "sonner";
10+
import superjson from "superjson";
911
import { z } from "zod";
1012
import { OnboardingLayout } from "@/components/layouts/onboarding-layout";
1113
import { AlertBlock } from "@/components/shared/alert-block";
@@ -22,6 +24,7 @@ import {
2224
} from "@/components/ui/form";
2325
import { Input } from "@/components/ui/input";
2426
import { authClient } from "@/lib/auth-client";
27+
import { appRouter } from "@/server/api/root";
2528
import { api } from "@/utils/api";
2629
import { useWhitelabelingPublic } from "@/utils/hooks/use-whitelabeling";
2730

@@ -326,6 +329,21 @@ Invitation.getLayout = (page: ReactElement) => {
326329
return <OnboardingLayout>{page}</OnboardingLayout>;
327330
};
328331
export async function getServerSideProps(ctx: GetServerSidePropsContext) {
332+
const helpers = createServerSideHelpers({
333+
router: appRouter,
334+
ctx: {
335+
req: ctx.req as any,
336+
res: ctx.res as any,
337+
db: null as any,
338+
session: null as any,
339+
user: null as any,
340+
},
341+
transformer: superjson,
342+
});
343+
// Prefetch the public branding so the invitation logo and app name render
344+
// correctly on the server (no flash of default branding).
345+
await helpers.whitelabeling.getPublic.prefetch();
346+
329347
const { query } = ctx;
330348

331349
const token = query.token;
@@ -354,6 +372,7 @@ export async function getServerSideProps(ctx: GetServerSidePropsContext) {
354372
if (invitation.userAlreadyExists) {
355373
return {
356374
props: {
375+
trpcState: helpers.dehydrate(),
357376
isCloud: IS_CLOUD,
358377
token: token,
359378
invitation: invitation,
@@ -373,6 +392,7 @@ export async function getServerSideProps(ctx: GetServerSidePropsContext) {
373392

374393
return {
375394
props: {
395+
trpcState: helpers.dehydrate(),
376396
isCloud: IS_CLOUD,
377397
token: token,
378398
invitation: invitation,

0 commit comments

Comments
 (0)