Spaces:
Paused
Paused
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** | |
| ```tsx | |
| // 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** | |
| ```tsx | |
| // Now both iframes stay in sync | |
| setDisplayHtml(html); | |
| setSecondaryHtml(html); | |
| ``` | |
| ### β **Fix 4: Added Forced Re-renders** | |
| ```tsx | |
| // 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! π | |