deepsite / PREVIEW_HTML_CHANGES_FIX.md
dr-data
Fix preview component: eliminate blinking, ensure HTML updates, add smooth transitions
dcd5e1d

Preview HTML Changes Not Reflecting - FIXED

Problem Reported

The preview was showing the starter HTML code but not reflecting HTML code changes smoothly. Changes to the HTML content were not being displayed in the preview.

Root Causes Identified

1. Competing useEffect Hooks

  • Two different useEffects were trying to update displayHtml
  • Circular dependencies in the initialization useEffect [html, displayHtml]
  • Conflicting update logic causing race conditions

2. Complex Dependency Arrays

  • Main update useEffect had [html, isAiWorking, injectContentSeamlessly]
  • The injectContentSeamlessly dependency was unnecessary and problematic
  • Changes to html prop weren't reliably triggering updates

3. Iframe Content Not Syncing

  • secondaryHtml state wasn't being updated when html changed
  • Only one iframe was getting updated content
  • No forced re-renders when content changed

4. Update Detection Issues

  • Multiple conditions preventing HTML updates
  • Complex timing logic interfering with basic functionality

Fixes Applied

βœ… Fix 1: Consolidated Update Logic

// BEFORE: Two competing useEffects
useEffect(() => {
  if (html && html !== displayHtml) {
    setDisplayHtml(html);
  }
}, [html, displayHtml]); // Circular dependency!

useEffect(() => {
  // Complex update logic...
}, [html, isAiWorking, injectContentSeamlessly]);

// AFTER: Single, reliable useEffect
useEffect(() => {
  if (html !== displayHtml) {
    setDisplayHtml(html);
    setSecondaryHtml(html); // Keep both iframes in sync
    prevHtmlRef.current = html;
  }
  // Enhanced updates only during AI streaming
}, [html, isAiWorking]); // Simplified dependencies

βœ… Fix 2: Removed Circular Dependencies

  • Eliminated displayHtml from dependency arrays
  • Simplified to only depend on [html, isAiWorking]
  • Removed unnecessary injectContentSeamlessly dependency

βœ… Fix 3: Synchronized Iframe Content

// Now both iframes stay in sync
setDisplayHtml(html);
setSecondaryHtml(html);

βœ… Fix 4: Added Forced Re-renders

// Force iframe re-render when content changes
<iframe
  srcDoc={displayHtml}
  key={`primary-${displayHtml.length}`} // Forces re-render
/>
<iframe
  srcDoc={secondaryHtml || displayHtml}
  key={`secondary-${(secondaryHtml || displayHtml).length}`} // Forces re-render
/>

βœ… Fix 5: Enhanced Debug Logging

  • Added detailed logging for HTML changes
  • Track when displayHtml gets updated
  • Monitor iframe loading and content updates
  • Preview HTML content in logs for debugging

Expected Results

βœ… Immediate HTML Updates

  • Manual HTML edits should reflect in preview instantly
  • No delay or lag in displaying HTML changes
  • Starter HTML displays correctly on load

βœ… Smooth AI Updates

  • AI-generated content updates smoothly during streaming
  • Enhanced transition effects during AI work
  • Final AI output always displays correctly

βœ… Reliable State Management

  • No more competing update logic
  • Consistent iframe content synchronization
  • Proper cleanup and state management

βœ… Better Debugging

  • Console logs show HTML update flow
  • Easy to track when changes occur
  • Clear visibility into iframe loading

Technical Implementation

Key Changes Made:

  1. Single source of truth for HTML updates
  2. Immediate state updates when HTML prop changes
  3. Forced iframe re-renders via key prop
  4. Synchronized dual iframe content
  5. Simplified dependency management

Files Modified:

  • /components/editor/preview/index.tsx - Main Preview component

Testing Recommendations:

  1. βœ… Edit HTML manually in the code editor - should update preview immediately
  2. βœ… Test AI streaming updates - should show smooth progress
  3. βœ… Verify starter HTML displays on fresh load
  4. βœ… Check that final AI output always appears
  5. βœ… Test rapid HTML changes for responsiveness

The preview should now immediately and smoothly reflect all HTML code changes! πŸŽ‰