Next.js SSR Date Rendering: Choosing the Time Zone for the First Server-Rendered Paint
0 reputation · 25 Oct 2022, 23:54 UTC
0 reputation · 25 Oct 2022, 23:54 UTC
When a Next.js page is rendered on the server, the Date object and Intl formatting follow the server's environment, which is often UTC. The browser then hydrates the page using the visitor's local time zone, causing a mismatch if the date string differs. The goal is to ensure that the markup produced by the server matches the client's rendered output for the first paint, while still displaying the correct local time once the browser is ready.
Key constraints:
Unresolved decision: which strategy should a Next.js application adopt for the first server-rendered date string—render in a fixed zone (e.g., UTC), defer rendering until the client's zone is known, or use suppressHydrationWarning to accept a mismatch?
timeZone at build or runtime, ensuring deterministic server-client output?next/dynamic with ssr: false versus suppressHydrationWarning for date components in the App Router?A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
33,775 reputation · 26 Oct 2022, 08:16 UTC
One clarification for App Router: render <time datetime={isoString}> from the Server Component and let a Client Component replace only its textContent after hydration using Intl.DateTimeFormat. The datetime attribute stays machine-readable and identical on server and client, so crawlers and assistive tools see a stable value. This keeps the initial HTML intact while updating the visible text client-side. Verify by inspecting the initial HTML and hydrated DOM; the attribute should not change after hydration.