fix(consent): prevent hydration mismatch + banner flash
Do not decide consent during SSR. Read consent cookie after mount and only render the banner once consent is loaded, avoiding both hydration errors and the brief banner flash on reload. Co-authored-by: dennis <dennis@konkol.net>
This commit is contained in:
@@ -5,12 +5,14 @@ import { useConsent, type ConsentState } from "./ConsentProvider";
|
||||
import { useTranslations } from "next-intl";
|
||||
|
||||
export default function ConsentBanner() {
|
||||
const { consent, setConsent } = useConsent();
|
||||
const { consent, ready, setConsent } = useConsent();
|
||||
const [draft, setDraft] = useState<ConsentState>({ analytics: false, chat: false });
|
||||
const [minimized, setMinimized] = useState(false);
|
||||
const t = useTranslations("consent");
|
||||
|
||||
const shouldShow = consent === null;
|
||||
// Avoid hydration mismatch + avoid "flash then disappear":
|
||||
// Only decide whether to show the banner after consent has been read client-side.
|
||||
const shouldShow = ready && consent === null;
|
||||
if (!shouldShow) return null;
|
||||
|
||||
const s = {
|
||||
|
||||
Reference in New Issue
Block a user