Skip to content

Email for professionals and teams

Render the branded page title on the server (#81)

Can K.8ee28cfd0457
diff --git a/src/app/layout.tsx b/src/app/layout.tsx
index adbb7cc..ac23892 100644
--- a/src/app/layout.tsx
+++ b/src/app/layout.tsx
@@ -6,6 +6,7 @@ import { DEFAULT_LOCALE, getDirection } from "@/lib/i18n/utils";
import { getRequestLocale, loadServerMessages } from "@/lib/i18n/server";
import { sidebarBootstrapScript } from "@/components/sidebar-state-utils";
import { themeBootstrapScript } from "@/components/theme-utils";
+import { getHomeBranding } from "./home-server-utils";
import "./globals.css";
const geistSans = Geist({
@@ -18,34 +19,38 @@ const geistMono = Geist_Mono({
subsets: ["latin"],
});
-export const metadata: Metadata = {
- title: "Mailflare",
- description: "Multi-tenant email on Cloudflare",
- icons: { icon: "/api/branding/icon", apple: "/icon-192.png" },
- appleWebApp: {
- capable: true,
- title: "Mailflare",
- statusBarStyle: "default",
- },
- robots: {
- index: false,
- follow: false,
- noarchive: true,
- nosnippet: true,
- noimageindex: true,
- googleBot: {
+export async function generateMetadata(): Promise<Metadata> {
+ const { appName } = await getHomeBranding();
+ return {
+ title: appName,
+ description: "Multi-tenant email on Cloudflare",
+ icons: { icon: "/api/branding/icon", apple: "/icon-192.png" },
+ appleWebApp: {
+ capable: true,
+ title: appName,
+ statusBarStyle: "default",
+ },
+ robots: {
index: false,
follow: false,
noarchive: true,
nosnippet: true,
noimageindex: true,
+ googleBot: {
+ index: false,
+ follow: false,
+ noarchive: true,
+ nosnippet: true,
+ noimageindex: true,
+ },
},
- },
-};
+ };
+}
export default async function RootLayout({ children }: Readonly<{ children: React.ReactNode }>) {
const locale = await getRequestLocale();
const messages = locale === DEFAULT_LOCALE ? undefined : await loadServerMessages(locale);
+ const branding = await getHomeBranding();
return (
<html lang={locale} dir={getDirection(locale)} suppressHydrationWarning>
<head>
@@ -54,7 +59,7 @@ export default async function RootLayout({ children }: Readonly<{ children: Reac
<link rel="icon" href="/api/branding/icon"></link>
</head>
<body className={`${geistSans.variable} ${geistMono.variable} antialiased`}>
- <LanguageProvider initialLocale={locale} initialMessages={messages}><Providers>{children}</Providers></LanguageProvider>
+ <LanguageProvider initialLocale={locale} initialMessages={messages}><Providers initialBranding={branding}>{children}</Providers></LanguageProvider>
</body>
</html>
);
diff --git a/src/components/branding-provider.tsx b/src/components/branding-provider.tsx
index a965be4..ac09b04 100644
--- a/src/components/branding-provider.tsx
+++ b/src/components/branding-provider.tsx
@@ -1,13 +1,14 @@
"use client";
import { createContext, useContext, useEffect, useState } from "react";
+import type { Branding } from "@/lib/branding/types";
import type { BrandingContextValue } from "./branding-provider-types";
import { DEFAULT_BRANDING, fetchBranding } from "./branding-provider-utils";
const BrandingContext = createContext<BrandingContextValue | null>(null);
-export function BrandingProvider({ children }: { children: React.ReactNode }) {
- const [branding, setBranding] = useState(DEFAULT_BRANDING);
+export function BrandingProvider({ children, initialBranding }: { children: React.ReactNode; initialBranding?: Branding }) {
+ const [branding, setBranding] = useState(initialBranding ?? DEFAULT_BRANDING);
const [iconVersion, setIconVersion] = useState(0);
async function refreshBranding() {
diff --git a/src/components/messages/message-folder-page.tsx b/src/components/messages/message-folder-page.tsx
index 77568a2..033b575 100644
--- a/src/components/messages/message-folder-page.tsx
+++ b/src/components/messages/message-folder-page.tsx
@@ -342,6 +342,13 @@ export function MessageFolderPage({
setSelectedMessages([]);
}, [offset]);
+ useEffect(() => {
+ const originalTitle = document.title;
+ return () => {
+ document.title = originalTitle;
+ };
+ }, []);
+
useEffect(() => {
if (mailboxesLoading) return;
document.title = formatEmailPageTitle({
diff --git a/src/components/providers.tsx b/src/components/providers.tsx
index 8029a02..7a631d7 100644
--- a/src/components/providers.tsx
+++ b/src/components/providers.tsx
@@ -4,6 +4,7 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { useEffect, useState } from "react";
import { clearMailboxClientState } from "@/components/mailbox-provider-utils";
import { BrandingProvider } from "@/components/branding-provider";
+import type { Branding } from "@/lib/branding/types";
import { NewMessagePopup } from "@/components/new-message-popup";
import { PwaServiceWorker } from "@/components/pwa-service-worker";
import { ThemeSync } from "@/components/theme-sync";
@@ -13,7 +14,7 @@ import { clearMessageDetailCache } from "@/lib/messages/detail-cache";
import { clearCurrentUserCache } from "@/hooks/use-current-user";
import { AUTH_SESSION_CHANGED_EVENT } from "@/lib/auth/client";
-export function Providers({ children }: { children: React.ReactNode }) {
+export function Providers({ children, initialBranding }: { children: React.ReactNode; initialBranding?: Branding }) {
const realtime = useMessagePolling();
const [client] = useState(
@@ -45,7 +46,7 @@ export function Providers({ children }: { children: React.ReactNode }) {
return (
<QueryClientProvider client={client}>
- <BrandingProvider>
+ <BrandingProvider initialBranding={initialBranding}>
<PwaServiceWorker />
{children}
<ThemeSync />
diff --git a/tests/i18n.test.mjs b/tests/i18n.test.mjs
index 3cafa27..30c17b3 100644
--- a/tests/i18n.test.mjs
+++ b/tests/i18n.test.mjs
@@ -64,13 +64,15 @@ async function bundleI18n(outfile, extraLocale = false) {
xx: { label: "Testlandic", dir: "rtl" },`),
loader: "ts",
}));
- builder.onResolve({ filter: /^(next\/headers|next\/font\/google|@\/components\/providers|@\/lib\/cloudflare)$/ }, (args) => ({ path: args.path, namespace: "stub" }));
+ builder.onResolve({ filter: /^(next\/headers|next\/font\/google|@\/components\/providers|@\/lib\/cloudflare|\.\/home-server-utils)$/ }, (args) => ({ path: args.path, namespace: "stub" }));
builder.onLoad({ filter: /.*/, namespace: "stub" }, ({ path }) => ({ contents: path === "next/headers"
? 'export async function cookies() { return { get(name) { return name === "mailflare-locale" && globalThis.testLocaleCookie !== undefined ? { value: globalThis.testLocaleCookie } : undefined; } }; }'
: path === "next/font/google"
? 'export const Geist = () => ({ variable: "sans" }); export const Geist_Mono = () => ({ variable: "mono" });'
- : path === "@/lib/cloudflare"
- ? "export function getEnv() { return { ASSETS: { fetch: globalThis.readTestAsset } }; }"
+ : path === "./home-server-utils"
+ ? 'export async function getHomeBranding() { return { appName: "Mailflare", hasCustomIcon: false, canCustomizeBranding: false }; }'
+ : path === "@/lib/cloudflare"
+ ? "export function getEnv() { return { ASSETS: { fetch: globalThis.readTestAsset } }; }"
: 'export function Providers({ children }) { return children; }' }));
builder.onLoad({ filter: /\.css$/ }, () => ({ contents: "", loader: "js" }));
},