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+ } ;
0 commit comments