Questions
16 of 33
1Explain server-side rendering (SSR) in Next.js.
2List the benefits of Server-Side Rendering in Next.js.
3What are server components?
4How to use server components in next js?
5What is the React Server Component Payload (RSC)?
6Describe how server components are rendered.
7Describe server rendering strategies
8How can you keep Server-only Code out of the Client Environment
9What are the issues of using context provider at the root of the application?
10List dynamic functions available to server components.
11List all Dynamic APIs.
12How do we opt for Static Rendering, Dynamic Rendering and Streaming?
13Explain the difference between SSR, SSG, ISR, and CSR in Next.js. When would you choose each?
14How does getServerSideProps work internally? What is its execution context?
15What are the performance trade-offs of SSR vs SSG in a high-traffic production app?
16How does Next.js handle hydration? What is a hydration mismatch, and how do you debug it?
17What happens when getServerSideProps throws an error? How do you handle it gracefully?
18How would you architect a Next.js app that needs both personalised (SSR) and cacheable (SSG) pages at scale?
19Explain the App Router vs Pages Router SSR model differences. How does React Server Components change the SSR paradigm?
20How do React Server Components (RSC) differ from traditional SSR? Can they be used together?
21How does streaming SSR work in Next.js 13+? What role does Suspense play?
22How would you implement partial hydration or Islands Architecture in Next.js?
23Explain how Next.js handles data fetching waterfall problems in SSR and how you'd mitigate them.
24How would you implement partial hydration or Islands Architecture in Next.js?
25How do you implement caching strategies for SSR responses in Next.js? (CDN, Cache-Control, stale-while-revalidate)
26How would you reduce TTFB (Time to First Byte) in an SSR-heavy Next.js app?
27What's the difference between fetch caching in the App Router vs traditional getServerSideProps?
28How do you avoid redundant database/API calls across multiple server components on the same page?
29How do you securely handle authentication in SSR? What are the risks of passing tokens via cookies vs headers?
30How do you prevent sensitive server-side data from leaking to the client bundle?
31How would you implement role-based rendering on the server without exposing protected routes to the client?
32How do you debug SSR-only issues that don't appear in local development?
33How can we control dynamic rendering behaviour in next js?
16 / 33

How does Next.js handle hydration? What is a hydration mismatch, and how do you debug it?

Next.js pre-renders HTML on the server, then React hydrates it on the client to add interactivity; a hydration mismatch occurs when the server and client HTML differ, causing React to warn or fail

Hydration is the process where React takes over the static HTML that was pre-rendered by Next.js on the server and makes it interactive by attaching event handlers and state. Next.js pre-renders every page by default, generating HTML in advance for better performance and SEO. When the browser loads this HTML, it's immediately visible to the user. Then, React's hydration step runs in the background, attaching event listeners and reconciling the component tree with the existing DOM. In Next.js 14+ with the App Router, only Client Components actually hydrate—Server Components are rendered entirely on the server and don't send JavaScript to the client, significantly reducing the amount of hydration work required.

Understanding Hydration Mismatches
  1. 1

    Definition: A hydration mismatch occurs when the HTML generated on the server differs from what React renders during the first render on the client. React expects the initial client render to perfectly match the server HTML to be able to attach event handlers efficiently.

  2. 2

    Warning symptoms: The browser console shows warnings like 'Text content did not match server-rendered HTML' and React attempts to recover, but this recovery process can be costly for performance.

  3. 3

    Impact: Mismatches force React to discard the server HTML and re-render on the client, causing layout shifts, lost CSS, and degraded user experience.

Common Mismatch Example: Client-Only Data
Common Causes of Hydration Mismatches
  1. 1

    Browser-only APIs: Using window, localStorage, or document in component rendering without checking if code runs on the server.

  2. 2

    Dynamic data without server rendering: Fetching data client-side with useState/useEffect but having no initial server-rendered value.

  3. 3

    Time-dependent content: Using Date() or Math.random() directly in render, producing different values on server vs client.

  4. 4

    Incorrect HTML nesting: Placing block elements inside <p> tags, nested <a> tags, or other invalid HTML structures.

  5. 5

    Browser extensions modifying DOM: Some extensions inject elements or modify content, causing client HTML to differ from server.

  6. 6

    CSS Modules with lazy loading: Next.js may fail to include CSS for lazily-loaded components in the initial bundle, causing a flash of unstyled content during hydration.

  7. 7

    iOS format detection: iOS automatically converts phone numbers and dates to links, altering the HTML structure.

Next.js provides several tools to help debug hydration issues. The development server shows detailed error messages pointing to the component tree where the mismatch occurs. Using React 18.2+ ensures you get the best hydration mismatch warnings with component stack traces. The Next.js Dev Tools extension can highlight exactly which elements differ. For production debugging, you can compare the server-rendered HTML (view page source) with the client DOM (inspect element) to identify discrepancies. Look for the specific element where the content differs and trace back to its component logic.

Debugging Steps
  1. 1
    1. Read the console error carefully: The error message often includes the exact text that mismatched and points to the component.
  2. 2
    1. Check your component tree: Identify which component contains the mismatched content by looking at the stack trace.
  3. 3
    1. Compare server and client output: Run a production build locally with next build && next start to test the exact behavior.
  4. 4
    1. Isolate browser extensions: Test in incognito mode or with extensions disabled to rule out DOM modifications.
  5. 5
    1. Add suppression strategically: Use suppressHydrationWarning only for intentionally different content like timestamps.

React 18+ introduced selective hydration, allowing you to wrap less important components in Suspense to defer their hydration. This improves perceived performance by prioritizing critical UI. However, selective hydration with CSS Modules can cause Flash of Unstyled Content (FOUC) if Next.js fails to include the CSS in the initial bundle. The component's HTML is correctly server-rendered with the proper class name, but the CSS loads lazily with the component, causing a visual flash when hydration completes. This issue particularly affects Pages Router with React.lazy and Suspense, and forces developers to choose between using CSS Modules or selective hydration benefits.

Fixing Selective Hydration CSS Issues
Best Practices to Avoid Mismatches
  1. 1

    Use Server Components for data fetching: Prefer async Server Components over client-side data fetching to ensure server and client match.

  2. 2

    Wrap dynamic content in useEffect: Any code that accesses browser APIs or uses dynamic data should run in useEffect, not in render.

  3. 3

    Check HTML validation: Use tools like the W3C validator to catch invalid nesting patterns.

  4. 4

    Disable iOS format detection: Add the meta tag to prevent automatic link creation.

  5. 5

    Test with JavaScript disabled: Verify your page renders meaningful content without JS, confirming pre-rendering works.

  6. 6

    Use dynamic imports with SSR disabled: For components that must differ on client, import them with next/dynamic and ssr: false.

Complete Solution Examples

Hydration mismatch warnings are most helpful with React 18.2.0 or higher. Older React versions may not provide component stack traces or detailed mismatch information, making debugging significantly harder. Next.js recommends keeping React updated to the latest version to benefit from improved error messages and hydration features like selective hydration. The minimum React version for optimal hydration debugging is 18.2.0.

Difficulty: 5/10
Topics: hydration process, hydration mismatch, debugging strategies

Scenario Questions

0-2 years experience
  1. 1

    You added a new component that fetches data in a useEffect hook, and after deploying you see a flash of unstyled content. How would you verify whether hydration is the cause and fix it?

  2. 2

    When the browser console shows a 'Hydration mismatch' warning on a simple page, what immediate steps do you take to resolve it?

2-5 years experience
  1. 1

    You introduced a page that uses getServerSideProps and also reads a value from localStorage during render, leading to intermittent hydration mismatch errors. Walk me through how you'd debug and correct the issue.

  2. 2

    Explain how Next.js's automatic static optimization interacts with hydration, and what trade‑offs you consider when choosing between getStaticProps and getServerSideProps for a component that must stay in sync with client state.

5-8 years experience
  1. 1

    After a feature‑flag rollout, a large e‑commerce site starts showing random hydration mismatches on product pages. How would you design a strategy to detect, isolate, and prevent these mismatches at scale?

  2. 2

    Discuss the performance impact of hydrating a page with thousands of DOM nodes in Next.js. What techniques would you apply to reduce time‑to‑interactive while ensuring no mismatches occur?

  3. 3

    Your team wants to use React 18 concurrent features with Next.js. What hydration considerations and potential pitfalls should you account for in a production environment?

8+ years experience
  1. 1

    Your company is migrating a legacy React application that heavily relies on mutable global state set during client render to Next.js. How would you architect the migration to minimize hydration mismatches and keep the codebase maintainable across multiple teams?

  2. 2

    When building a shared component library consumed by several Next.js services, how would you enforce consistent hydration behavior and integrate tooling into CI to catch mismatches early?

Follow-up Questions

  • What specific console output would you look for when a mismatch occurs?
  • How does using browser‑only APIs during SSR lead to mismatches?
  • When might you choose to disable SSR for a component to avoid hydration issues?