Files
lobehub__lobehub/apps/auth/app/shell/AuthAppShell.tsx
Innei 12fa88e49a ✨ feat(auth): split the auth surface into a prerendered micro app (#18542)
* ♻️ refactor(auth): make the auth shell safe to prerender

Three changes, all behaviour-preserving for the current client-rendered
auth SPA, that a prerendered copy of the same pages needs:

- `enableBusinessFeatures` is a build-time constant, not deployment state.
  `AuthServerConfigProvider` now reads it from `@lobechat/business-const`
  instead of the injected server config, so a render that has not received
  that config yet still resolves the right SSO provider source.
- The terms checkbox and the "last used" provider both seeded `useState`
  from `localStorage` during render. A returning user's stored value makes
  the first render depend on the browser, which cannot be prerendered; they
  now start empty and fill in from an effect.
- `LobeAnalyticsProviderWrapper` accepts the analytics config as a prop and
  guards its `window` read, so it can render outside a browser.

Also drops the `@/features/AuthShell` barrel from the `src/features/Auth`
importers: the barrel re-exports `AuthShell`, which pulls `AuthLocale` and
the full en-US dictionary into every consumer's graph.

* ✨ feat(auth): split the auth surface into a prerendered micro app

`apps/auth` renders the sign-in flow ahead of time instead of shipping an
empty `#root`. React Router framework mode with `ssr: false` + `prerender`,
so the worker carries no React at all — 7KB, it picks one prerendered
document by locale and swaps in the deployment's config.

- 18 locales x 4 routes = 72 documents, plus a SPA fallback for the routes
  that take params. One build pass per locale writes the same canonical
  paths; the non-default passes are folded into `build/client/__i18n/`.
  Passes are independent, so they run 6 at a time (3m05s -> 44s).
- Each document inlines only its own locale's dictionary; the rest load on
  demand for the language switcher, which keeps them off the first-load
  path entirely.
- `/webapi/auth/spa-config` is a new public endpoint the worker reads to
  fill `window.__SERVER_CONFIG__`. It carries only the six fields the auth
  pages actually read; a failed lookup leaves the placeholder and the page
  still renders.
- Per-route SEO: real localized titles, and `noindex, nofollow` on every
  transactional page — only /signin and /signup stay indexable, with a
  canonical URL.

The existing Next.js `/spa-auth` route is untouched, so self-hosted and
Docker deployments keep serving the client-rendered SPA. This app builds
and deploys from lobehub-cloud, which overlays `@/business/*`.

* 🐛 fix(auth): restore feature flags and track the prerender lib
2026-09-03 19:34:41 +08:00

81 lines
2.9 KiB
TypeScript

import { ModalHost } from '@lobehub/ui/base-ui';
import { memo, type PropsWithChildren } from 'react';
import BusinessAuthProvider from '@/business/client/BusinessAuthProvider';
import { LobeAnalyticsProviderWrapper } from '@/components/Analytics/LobeAnalyticsProviderWrapper';
import type { IFeatureFlags } from '@/config/featureFlags';
import { mapFeatureFlagsEnvToState } from '@/config/featureFlags';
import AuthContainer from '@/features/AuthShell/AuthContainer';
import { AuthServerConfigProvider } from '@/features/AuthShell/AuthServerConfigProvider';
import AuthThemeLite from '@/features/AuthShell/AuthThemeLite';
import { useIsHydrated } from '@/hooks/useIsHydrated';
import type { GlobalServerConfig } from '@/types/serverConfig';
import type { AnalyticsConfig } from '@/types/spaServerConfig';
import AuthLocaleProvider from './AuthLocaleProvider';
const EMPTY_ANALYTICS = {};
/** What `/webapi/auth/spa-config` serves — narrower than the Next.js shell's. */
interface AuthWorkerConfig {
analyticsConfig: AnalyticsConfig;
config: Pick<
GlobalServerConfig,
'disableEmailPassword' | 'enableEmailVerification' | 'enableMagicLink' | 'oAuthSSOProviders'
>;
enableOIDC: boolean;
featureFlags?: Partial<IFeatureFlags>;
globalCDN?: boolean;
}
// Everything here is deployment state the build cannot know, so the worker
// injects it. Reading it only after hydration keeps the first client render
// byte-identical to the prerendered document — which is why
// `enableBusinessFeatures` must not travel this way: prerendering the SSO
// buttons depends on knowing it during the render.
const useInjectedServerConfig = (): AuthWorkerConfig | undefined => {
const isHydrated = useIsHydrated();
if (!isHydrated) return undefined;
return window.__SERVER_CONFIG__ as unknown as AuthWorkerConfig | undefined;
};
interface AuthAppShellProps extends PropsWithChildren {
locale: string;
}
const AuthAppShell = memo<AuthAppShellProps>(({ children, locale }) => {
const serverConfig = useInjectedServerConfig();
return (
<AuthLocaleProvider locale={locale}>
<AuthThemeLite globalCDN={serverConfig?.globalCDN}>
<AuthServerConfigProvider
enableOIDC={serverConfig?.enableOIDC}
isMobile={false}
serverConfig={serverConfig?.config}
featureFlags={
serverConfig?.featureFlags
? mapFeatureFlagsEnvToState(serverConfig.featureFlags)
: undefined
}
>
<LobeAnalyticsProviderWrapper
analytics={serverConfig?.analyticsConfig ?? EMPTY_ANALYTICS}
>
<BusinessAuthProvider>
<AuthContainer>{children}</AuthContainer>
</BusinessAuthProvider>
</LobeAnalyticsProviderWrapper>
</AuthServerConfigProvider>
<ModalHost />
</AuthThemeLite>
</AuthLocaleProvider>
);
});
AuthAppShell.displayName = 'AuthAppShell';
export default AuthAppShell;