/sitecore-content-sdk-nextjs-app-router:content-sdk-app-router-theme-hydration-patternfrom Sitecore Content SDK - Next.js App Router
Hydration-safe dark/light/system theme for Content SDK App Router apps on Tailwind v4. Prevents FOUC, hydration mismatches, and lint/security issues with a preflight script and useSyncExternalStore provider.
Ships in Sitecore Content SDK - Next.js App Router. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Dark+light+system theme in SSR apps has four edges that bite: FOUC, hydration mismatch, lint friction on the canonical patterns, and security tooling that blocks the standard inline-script fixes. This skill captures a working configuration that survives all four.
react-hooks/set-state-in-effect in a theme provider.{ __html: '...' } object).Five files, all hydration-safe:
public/theme-init.js — synchronous preflight (external file, NOT inline)(function () {
try {
var stored = localStorage.getItem('theme');
var theme = stored === 'light' || stored === 'dark' ? stored : 'system';
var resolved =
theme === 'system'
? window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
: theme;
document.documentElement.classList.toggle('dark', resolved === 'dark');
document.documentElement.style.colorScheme = resolved;
} catch (_) {}
})();
src/app/layout.tsx — wire it in <head><html lang="en" suppressHydrationWarning>
<head>
{/* Sync preflight runs before React hydrates — FOUC prevention.
Parser-blocking is intentional; the rule is a generic perf hint that
doesn't apply to a 5-line theme preflight. */}
{/* eslint-disable-next-line @next/next/no-sync-scripts */}
<script src="/theme-init.js"></script>
</head>
<body><ThemeProvider>{children}</ThemeProvider></body>
</html>
src/ui/theme-provider.tsx — useSyncExternalStore-basedUses module-scope subscribe/getSnapshot (NO useEffect for storage reads — that triggers the cascading-render lint). Server snapshot returns 'system' to match the preflight's default. Storage events + matchMedia trigger re-renders.
src/ui/theme-toggle.tsx — mounted-flag for active stateconst [mounted, setMounted] = useState(false);
// Canonical SSR-safe "after hydration" flag — setState-in-effect IS the
// pattern; useSyncExternalStore gives us `theme`, this flag separately
// guards client-only styling like "which button is active right now."
// eslint-disable-next-line react-hooks/set-state-in-effect
useEffect(() => setMounted(true), []);
// ...
const active = mounted && theme === opt.value;
src/app/globals.css — Tailwind v4 token model@import "tailwindcss";
@theme {
--color-canvas: #fafaf7;
--color-ink: #0c0c14;
/* ... */
}
@custom-variant dark (&:where(.dark, .dark *));
.dark {
--color-canvas: #0c0c14;
--color-ink: #f4f4ef;
/* ... */
}
:root { color-scheme: light; }
.dark { color-scheme: dark; }
{ __html: '...' } object and writes the string into the element. Security tooling blocks it (false-positive XSS warning). Use a real file in public/ referenced via <script src>. Same end-result (parser-blocking sync execution), no hook trip.theme-provider.tsx exactly. If they diverge, the first paint and the first React render will disagree. Keep them in sync; treat them as one logical unit.useSyncExternalStore for theme reads, NOT useEffect + setState. The cascading-render lint fires on the latter; the former is the React 19 canonical pattern for browser-API subscriptions.'system' always. Matches the preflight's default-when-no-localStorage. If you return anything else server-side, you'll get a hydration mismatch on the very first paint.react-hooks/set-state-in-effect on the useEffect(() => setMounted(true), []) call with an inline comment.suppressHydrationWarning only silences the <html> element's own attribute mismatch. It does NOT suppress mismatches in children. Fix child mismatches with the mounted-flag pattern..dark class to <html>, not <body>. That's where the preflight applies it and where color-scheme should sit.dark: at runtime to prefers-color-scheme unless globals.css declares @custom-variant dark, which this pattern does (content-sdk-app-router-atoms § 7).tailwind.config.js. All token + variant config goes in globals.css via @theme, @custom-variant, etc.typeof window, navigator, matchMedia) in their render body. The theme system is not the only source.<head> BEFORE <body> and is NOT marked async or defer. Both attributes break parser-blocking.useSyncExternalStore with a custom useEffect+useState because of lint friction — that path leads back to the hydration bug.If the design system says theme toggles should be hidden in some environments (e.g., embedded mode), gate ONLY the toggle's render, not the provider:
const showToggle = process.env.NEXT_PUBLIC_SHOW_THEME_TOGGLE !== 'false';
// ...
{showToggle ? <ThemeToggle /> : null}
The provider always mounts so the theme is applied; just the UI surface is conditional.
<html>'s class — .dark matches the resolved theme on first paint.npm run lint clean.content-sdk-app-router-component-scaffold — for the component file structure context.content-sdk-component-naming-traps — for where the UI infrastructure (src/ui/) should live.What SKILL.md sets; muted values are Claude Code's defaults
17 skills in this plugin
/content-sdk-app-router-atom-createWrites a Content SDK atom (beta.atoms): atom vs component, field schemas and enum tokens, renderer, catalog and registry keys, container slots, lock lifecycle, mock-MMS proof. Use when adding or changing an atom. Not wiring atoms into an app./content-sdk-app-router-atomsContent SDK Atoms (beta.atoms only): catalog, atomsConfig, ComponentRef rendering, lock CI gate, runtime CSS, Content Kit install, null-render traps. Use when wiring atoms or a low-code component renders nothing. Not code components or stable apps./content-sdk-app-router-cache-components-and-osrnpx claudepluginhub chris1415/sitecore-skills-pocs --plugin sitecore-content-sdk-nextjs-app-routerOther plugins' skills for the same job