Skip to content

The Open Source DocuSign Alternative.

fix: react hydration errors (#3099)

Lucas Smith40472bc26c1a
diff --git a/apps/remix/app/entry.client.tsx b/apps/remix/app/entry.client.tsx
index 86e949d7a..0ffb3602e 100644
--- a/apps/remix/app/entry.client.tsx
+++ b/apps/remix/app/entry.client.tsx
@@ -3,27 +3,32 @@ import { dynamicActivate } from '@documenso/lib/utils/i18n';
import { i18n } from '@lingui/core';
import { detect, fromHtmlTag } from '@lingui/detect-locale';
import { I18nProvider } from '@lingui/react';
-import { StrictMode, startTransition, useEffect } from 'react';
+import { StrictMode, startTransition } from 'react';
import { hydrateRoot } from 'react-dom/client';
import { HydratedRouter } from 'react-router/dom';
import './utils/polyfills/promise-with-resolvers';
-function PosthogInit() {
+/**
+ * Initialised imperatively (not as a component inside `hydrateRoot`) because
+ * rendering extra client-only siblings changes the React tree structure
+ * relative to the server render in `entry.server.tsx`. That shifts every
+ * `useId` value (used by Radix for `id`/`htmlFor`/`aria-*`), causing hydration
+ * mismatches which can abort hydration entirely when the user interacts with
+ * the page early, leaving dead event handlers (broken dropdowns, native form
+ * submits).
+ */
+function initPosthog() {
const postHogConfig = extractPostHogConfig();
- useEffect(() => {
- if (postHogConfig) {
- void import('posthog-js').then(({ default: posthog }) => {
- posthog.init(postHogConfig.key, {
- api_host: postHogConfig.host,
- capture_exceptions: true,
- });
+ if (postHogConfig) {
+ void import('posthog-js').then(({ default: posthog }) => {
+ posthog.init(postHogConfig.key, {
+ api_host: postHogConfig.host,
+ capture_exceptions: true,
});
- }
- }, []);
-
- return null;
+ });
+ }
}
async function main() {
@@ -38,11 +43,11 @@ async function main() {
<I18nProvider i18n={i18n}>
<HydratedRouter />
</I18nProvider>
-
- <PosthogInit />
</StrictMode>,
);
});
+
+ void initPosthog();
}
// eslint-disable-next-line @typescript-eslint/no-floating-promises
diff --git a/apps/remix/app/root.tsx b/apps/remix/app/root.tsx
index 096b1504e..a7c29b4be 100644
--- a/apps/remix/app/root.tsx
+++ b/apps/remix/app/root.tsx
@@ -119,7 +119,11 @@ export function LayoutContent({ children }: { children: React.ReactNode }) {
const isRecipientRoute = matches.some((m) => m.id?.startsWith('routes/_recipient+'));
return (
- <html translate="no" lang={lang} data-theme={theme} className={theme ?? ''}>
+ // `suppressHydrationWarning` because `remix-themes` intentionally mutates
+ // `data-theme`/`class` on <html> before hydration (PreventFlashOnWrongTheme),
+ // so the server-rendered attributes never match the client render when the
+ // theme is resolved from the system preference. Attribute-only, one level deep.
+ <html translate="no" lang={lang} data-theme={theme} className={theme ?? ''} suppressHydrationWarning>
<head>
<meta charSet="utf-8" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
@@ -173,7 +177,11 @@ export function LayoutContent({ children }: { children: React.ReactNode }) {
<script
nonce={nonce(cspNonce)}
dangerouslySetInnerHTML={{
- __html: `window.__ENV__ = ${JSON.stringify(publicEnv)}`,
+ // `__webpack_nonce__` is read by `get-nonce` (used by
+ // react-remove-scroll / react-style-singleton inside Radix menus and
+ // dialogs) to stamp runtime-injected <style> elements. Without it the
+ // strict `style-src-elem` CSP blocks the scroll-lock styles.
+ __html: `window.__ENV__ = ${JSON.stringify(publicEnv)}; window.__webpack_nonce__ = ${JSON.stringify(cspNonce ?? '')}`,
}}
/>